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