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