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

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

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

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