source:
java/main/src/main/java/com/framsticks/params/FramsClass.java
@
96
Last change on this file since 96 was 96, checked in by , 11 years ago | |
---|---|
File size: 4.7 KB |
Rev | Line | |
---|---|---|
[77] | 1 | package com.framsticks.params; |
2 | ||
[86] | 3 | import com.framsticks.params.annotations.FramsClassAnnotation; |
4 | import com.framsticks.params.annotations.ParamAnnotation; | |
[88] | 5 | import com.framsticks.util.FramsticksException; |
[90] | 6 | import com.framsticks.util.lang.Containers; |
[86] | 7 | // import com.framsticks.util.FramsticksException; |
[84] | 8 | |
[77] | 9 | import java.util.*; |
10 | ||
[86] | 11 | import javax.annotation.Nonnull; |
[87] | 12 | import javax.annotation.concurrent.Immutable; |
[86] | 13 | |
[87] | 14 | import org.apache.log4j.Logger; |
15 | ||
[77] | 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 | */ | |
[87] | 28 | @Immutable |
[86] | 29 | @FramsClassAnnotation(id = "class", name = "class") |
[88] | 30 | public class FramsClass { |
[77] | 31 | |
[87] | 32 | private final static Logger log = Logger.getLogger(FramsClass.class); |
[77] | 33 | |
[87] | 34 | protected final String id; |
[77] | 35 | |
[87] | 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 | ||
[77] | 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 | ||
[87] | 51 | // /** The param getId map (for fast lookup of offset based on name */ |
52 | // protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>(); | |
[77] | 53 | |
[90] | 54 | @ParamAnnotation(id = "props", name = "props") |
55 | public List<Param> getParamEntries() { | |
56 | return Collections.unmodifiableList(paramList); | |
[77] | 57 | } |
58 | ||
[88] | 59 | public FramsClass(FramsClassBuilder builder) { |
[86] | 60 | |
[88] | 61 | this.id = builder.getId(); |
62 | this.name = builder.getName(); | |
63 | this.description = builder.getDescription(); | |
[90] | 64 | this.groups = Containers.build(builder.groupBuilders); |
[88] | 65 | this.paramList = builder.params; |
[77] | 66 | |
[88] | 67 | for (Param param : paramList) { |
[87] | 68 | paramEntryMap.put(param.getId(), param); |
[77] | 69 | } |
70 | ||
[87] | 71 | log.trace("created framsclass " + this); |
[77] | 72 | |
[85] | 73 | } |
74 | ||
[86] | 75 | @ParamAnnotation(id = "desc") |
[77] | 76 | public String getDescription() { |
77 | return description; | |
78 | } | |
79 | ||
80 | public int getGroupCount() { | |
81 | return groups.size(); | |
82 | } | |
83 | ||
[90] | 84 | public Group getGroup(int groupNumber) { |
85 | return Containers.getFromList(groups, groupNumber, "group", this); | |
[77] | 86 | } |
87 | ||
[90] | 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 | // } | |
[77] | 104 | |
[90] | 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 | ||
[86] | 118 | @ParamAnnotation |
[77] | 119 | public String getId() { |
120 | return id; | |
121 | } | |
122 | ||
[86] | 123 | @ParamAnnotation |
[77] | 124 | public String getName() { |
125 | return name; | |
126 | } | |
127 | ||
[84] | 128 | public String getNiceName() { |
129 | return name != null ? name : id; | |
130 | } | |
[77] | 131 | |
[96] | 132 | public @Nonnull <T extends Param> T castedParam(@Nonnull final Param param, @Nonnull final Class<T> type, Object name) { |
[86] | 133 | if (param == null) { |
[88] | 134 | // return null; |
135 | throw new FramsticksException().msg("param is missing").arg("name", name).arg("in", this); | |
[86] | 136 | } |
137 | if (!type.isInstance(param)) { | |
[88] | 138 | // return null; |
139 | throw new FramsticksException().msg("wrong type of param").arg("actual", param.getClass()).arg("requested", type).arg("in", this); | |
[86] | 140 | } |
141 | return type.cast(param); | |
142 | } | |
143 | ||
[77] | 144 | /** |
145 | * Gets the param entry. | |
[84] | 146 | * |
[77] | 147 | * @param i |
148 | * the offset of parameter | |
149 | * @return the param entry | |
150 | */ | |
[96] | 151 | public @Nonnull <T extends Param> T getParamEntry(final int i, @Nonnull final Class<T> type) { |
[86] | 152 | return castedParam(getParam(i), type, i); |
[77] | 153 | } |
154 | ||
155 | /** | |
156 | * Gets the param entry. | |
[84] | 157 | * |
[77] | 158 | * @param id |
159 | * the getId of parameter | |
160 | * @return the param entry | |
161 | */ | |
[96] | 162 | public @Nonnull <T extends Param> T getParamEntry(@Nonnull final String id, @Nonnull final Class<T> type) { |
[86] | 163 | return castedParam(getParam(id), type, id); |
[77] | 164 | } |
165 | ||
[86] | 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 | ||
[77] | 180 | public int getParamCount() { |
181 | return paramList.size(); | |
182 | } | |
183 | ||
184 | @Override | |
185 | public String toString() { | |
[87] | 186 | return id + "(" + name + ")"; |
[77] | 187 | } |
188 | ||
[87] | 189 | public static FramsClassBuilder build() { |
190 | return new FramsClassBuilder(); | |
[77] | 191 | } |
192 | } |
Note: See TracBrowser
for help on using the repository browser.