source: java/main/src/main/java/com/framsticks/params/FramsClass.java @ 87

Last change on this file since 87 was 87, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File size: 4.5 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.annotations.FramsClassAnnotation;
4import com.framsticks.params.annotations.ParamAnnotation;
5// import com.framsticks.util.FramsticksException;
6
7import java.util.*;
8
9import javax.annotation.Nonnull;
10import javax.annotation.concurrent.Immutable;
11
12import org.apache.log4j.Logger;
13
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 */
26@Immutable
27@FramsClassAnnotation(id = "class", name = "class")
28public final class FramsClass {
29
30        private final static Logger log = Logger.getLogger(FramsClass.class);
31
32        protected final String id;
33
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
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
49        // /** The param getId map (for fast lookup of offset based on name */
50        // protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>();
51
52        public Collection<Param> getParamEntries() {
53                return paramList;
54        }
55
56        public FramsClass(String id, String name, String description, List<Param> params, List<Group> groups) {
57
58                this.id = id;
59                this.name = name;
60                this.description = description;
61                this.groups = groups;
62                this.paramList = params;
63
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) {
72
73                        }
74                }
75
76                log.trace("created framsclass " + this);
77
78        }
79
80        @ParamAnnotation(id = "desc")
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.
91         *
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;
101                }
102                Group group = groups.get(gi);
103                return (group != null ? group.getProperty(pi) : null);
104        }
105
106        /**
107         * Gets the group name.
108         *
109         * @param gi
110         *            the offset of group
111         * @return the group name
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
119        @ParamAnnotation
120        public String getId() {
121                return id;
122        }
123
124        @ParamAnnotation
125        public String getName() {
126                return name;
127        }
128
129        public String getNiceName() {
130                return name != null ? name : id;
131        }
132
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
145        /**
146         * Gets the param entry.
147         *
148         * @param i
149         *            the offset of parameter
150         * @return the param entry
151         */
152        public <T extends Param> T getParamEntry(final int i, @Nonnull final Class<T> type) {
153                return castedParam(getParam(i), type, i);
154        }
155
156        /**
157         * Gets the param entry.
158         *
159         * @param id
160         *            the getId of parameter
161         * @return the param entry
162         */
163        public <T extends Param> T getParamEntry(@Nonnull final String id, @Nonnull final Class<T> type) {
164                return castedParam(getParam(id), type, id);
165        }
166
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
181        public int getParamCount() {
182                return paramList.size();
183        }
184
185        @Override
186        public String toString() {
187                return id + "(" + name + ")";
188        }
189
190        public static FramsClassBuilder build() {
191                return new FramsClassBuilder();
192        }
193}
Note: See TracBrowser for help on using the repository browser.