1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
4 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
5 | import com.framsticks.util.FramsticksException; |
---|
6 | // import com.framsticks.util.FramsticksException; |
---|
7 | |
---|
8 | import java.util.*; |
---|
9 | |
---|
10 | import javax.annotation.Nonnull; |
---|
11 | import javax.annotation.concurrent.Immutable; |
---|
12 | |
---|
13 | import org.apache.log4j.Logger; |
---|
14 | |
---|
15 | /** |
---|
16 | * The class FramsClass represents the class / schema of connected parameters |
---|
17 | * (such as parameters within the class). It differs from C++ version by storing |
---|
18 | * information about the class that parameters belong to. |
---|
19 | * |
---|
20 | * Based loosely on c++ class Param located in cpp/gdk/param.* |
---|
21 | * |
---|
22 | * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please |
---|
23 | * replace name and surname with my personal data) |
---|
24 | * |
---|
25 | * @author Piotr Sniegowski |
---|
26 | */ |
---|
27 | @Immutable |
---|
28 | @FramsClassAnnotation(id = "class", name = "class") |
---|
29 | public class FramsClass { |
---|
30 | |
---|
31 | private final static Logger log = Logger.getLogger(FramsClass.class); |
---|
32 | |
---|
33 | protected final String id; |
---|
34 | |
---|
35 | protected final String name; |
---|
36 | |
---|
37 | protected final String description; |
---|
38 | |
---|
39 | protected final List<Group> groups; |
---|
40 | |
---|
41 | /** The param list (for accessing parameters by offset in O(1) time. */ |
---|
42 | protected final List<Param> paramList; |
---|
43 | |
---|
44 | /** |
---|
45 | * The param entry map <parameterId, param> (for fast accessing of parameters |
---|
46 | * by their name) |
---|
47 | */ |
---|
48 | protected Map<String, Param> paramEntryMap = new LinkedHashMap<String, Param>(); |
---|
49 | |
---|
50 | // /** The param getId map (for fast lookup of offset based on name */ |
---|
51 | // protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>(); |
---|
52 | |
---|
53 | public Collection<Param> getParamEntries() { |
---|
54 | return paramList; |
---|
55 | } |
---|
56 | |
---|
57 | public FramsClass(FramsClassBuilder builder) { |
---|
58 | |
---|
59 | this.id = builder.getId(); |
---|
60 | this.name = builder.getName(); |
---|
61 | this.description = builder.getDescription(); |
---|
62 | this.groups = builder.groups; |
---|
63 | this.paramList = builder.params; |
---|
64 | |
---|
65 | for (Param param : paramList) { |
---|
66 | paramEntryMap.put(param.getId(), param); |
---|
67 | try { |
---|
68 | Group group = groups.get(param.getGroup()); |
---|
69 | if (group != null) { |
---|
70 | group.addProperty(param); |
---|
71 | } |
---|
72 | } catch (IndexOutOfBoundsException ignored) { |
---|
73 | |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | log.trace("created framsclass " + this); |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | @ParamAnnotation(id = "desc") |
---|
82 | public String getDescription() { |
---|
83 | return description; |
---|
84 | } |
---|
85 | |
---|
86 | public int getGroupCount() { |
---|
87 | return groups.size(); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Gets the group member. |
---|
92 | * |
---|
93 | * @param gi |
---|
94 | * the offset of group |
---|
95 | * @param pi |
---|
96 | * the offset of member within a group |
---|
97 | * @return the pi-th member of group gi |
---|
98 | */ |
---|
99 | public Param getGroupMember(int gi, int pi) { |
---|
100 | if (gi < 0 || pi < 0 || gi >= groups.size()) { |
---|
101 | return null; |
---|
102 | } |
---|
103 | Group group = groups.get(gi); |
---|
104 | return (group != null ? group.getProperty(pi) : null); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Gets the group name. |
---|
109 | * |
---|
110 | * @param gi |
---|
111 | * the offset of group |
---|
112 | * @return the group name |
---|
113 | */ |
---|
114 | public String getGroupName(int gi) { |
---|
115 | if (gi < 0 || gi >= groups.size()) |
---|
116 | return null; |
---|
117 | return groups.get(gi).name; |
---|
118 | } |
---|
119 | |
---|
120 | @ParamAnnotation |
---|
121 | public String getId() { |
---|
122 | return id; |
---|
123 | } |
---|
124 | |
---|
125 | @ParamAnnotation |
---|
126 | public String getName() { |
---|
127 | return name; |
---|
128 | } |
---|
129 | |
---|
130 | public String getNiceName() { |
---|
131 | return name != null ? name : id; |
---|
132 | } |
---|
133 | |
---|
134 | public <T extends Param> T castedParam(@Nonnull final Param param, @Nonnull final Class<T> type, Object name) { |
---|
135 | if (param == null) { |
---|
136 | // return null; |
---|
137 | throw new FramsticksException().msg("param is missing").arg("name", name).arg("in", this); |
---|
138 | } |
---|
139 | if (!type.isInstance(param)) { |
---|
140 | // return null; |
---|
141 | throw new FramsticksException().msg("wrong type of param").arg("actual", param.getClass()).arg("requested", type).arg("in", this); |
---|
142 | } |
---|
143 | return type.cast(param); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Gets the param entry. |
---|
148 | * |
---|
149 | * @param i |
---|
150 | * the offset of parameter |
---|
151 | * @return the param entry |
---|
152 | */ |
---|
153 | public <T extends Param> T getParamEntry(final int i, @Nonnull final Class<T> type) { |
---|
154 | return castedParam(getParam(i), type, i); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Gets the param entry. |
---|
159 | * |
---|
160 | * @param id |
---|
161 | * the getId of parameter |
---|
162 | * @return the param entry |
---|
163 | */ |
---|
164 | public <T extends Param> T getParamEntry(@Nonnull final String id, @Nonnull final Class<T> type) { |
---|
165 | return castedParam(getParam(id), type, id); |
---|
166 | } |
---|
167 | |
---|
168 | public Param getParam(int i) { |
---|
169 | if (i < 0 || i >= paramList.size()) { |
---|
170 | return null; |
---|
171 | } |
---|
172 | return paramList.get(i); |
---|
173 | } |
---|
174 | |
---|
175 | public Param getParam(String id) { |
---|
176 | if (!paramEntryMap.containsKey(id)) { |
---|
177 | return null; |
---|
178 | } |
---|
179 | return paramEntryMap.get(id); |
---|
180 | } |
---|
181 | |
---|
182 | public int getParamCount() { |
---|
183 | return paramList.size(); |
---|
184 | } |
---|
185 | |
---|
186 | @Override |
---|
187 | public String toString() { |
---|
188 | return id + "(" + name + ")"; |
---|
189 | } |
---|
190 | |
---|
191 | public static FramsClassBuilder build() { |
---|
192 | return new FramsClassBuilder(); |
---|
193 | } |
---|
194 | } |
---|