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