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