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

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

File size: 4.7 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.annotations.FramsClassAnnotation;
4import com.framsticks.params.annotations.ParamAnnotation;
5import com.framsticks.util.FramsticksException;
6import com.framsticks.util.lang.Containers;
7// import com.framsticks.util.FramsticksException;
8
9import java.util.*;
10
11import javax.annotation.Nonnull;
12import javax.annotation.concurrent.Immutable;
13
14import 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")
30public 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 @Nonnull <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 @Nonnull <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 @Nonnull <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}
Note: See TracBrowser for help on using the repository browser.