Debian Codegraph @

src/smetana/core/CEnumInterpretor.java

raw symbols SCIP
1 /* ========================================================================
2 * PlantUML : a free UML diagram generator
3 * ========================================================================
4 *
5 * Project Info: http://plantuml.com
6 *
7 * If you like this project or if you find it useful, you can support us at:
8 *
9 * http://plantuml.com/patreon (only 1$ per month!)
10 * http://plantuml.com/paypal
11 *
12 * This file is part of Smetana.
13 * Smetana is a partial translation of Graphviz/Dot sources from C to Java.
14 *
15 * (C) Copyright 2009-2020, Arnaud Roques
16 *
17 * This translation is distributed under the same Licence as the original C program.
18 *
19 * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
20 * LICENSE ("AGREEMENT"). [Eclipse Public License - v 1.0]
21 *
22 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
23 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
24 *
25 * You may obtain a copy of the License at
26 *
27 * http://www.eclipse.org/legal/epl-v10.html
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 *
35 */
36
37 package smetana.core;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42 public class CEnumInterpretor {
43
44 private final List<String> keys = new ArrayList<String>();
45 private String keyRef;
46 private int valueRef;
47
48 public CEnumInterpretor(Class enumClass) {
49 List<String> def = CType.getDefinition(enumClass);
50 JUtils.LOG("def1=" + def);
51 if (def.get(0).equals("typedef enum") == false) {
52 throw new IllegalArgumentException();
53 }
54 if (def.get(1).equals("{") == false) {
55 throw new IllegalArgumentException();
56 }
57 if (def.get(def.size() - 2).equals("}") == false) {
58 throw new IllegalArgumentException();
59 }
60 def = def.subList(2, def.size() - 2);
61 JUtils.LOG("def2=" + def);
62
63 for (String s1 : def) {
64 for (String s2 : s1.split(",")) {
65 s2 = s2.trim();
66 final int idx = s2.indexOf('=');
67 final String k = idx == -1 ? s2 : s2.substring(0, idx);
68 keys.add(k.trim());
69 if (idx == -1) {
70 continue;
71 }
72 if (keyRef != null) {
73 throw new IllegalStateException();
74 }
75 keyRef = k.trim();
76 valueRef = Integer.parseInt(s2.substring(idx + 1).trim());
77 }
78 }
79 JUtils.LOG("keys=" + keys);
80 }
81
82 public int valueOf(String name) {
83 JUtils.LOG("keys=" + keys);
84 final int idx = keys.indexOf(name);
85 if (idx == -1) {
86 throw new IllegalArgumentException(name + " is no enum value");
87 }
88 if (keyRef == null) {
89 return idx;
90 }
91 final int keyRefIndex = keys.indexOf(keyRef);
92 if (keyRefIndex == -1) {
93 throw new IllegalStateException();
94 }
95 return idx - keyRefIndex + valueRef;
96 }
97 }