source: java/main/src/main/java/com/framsticks/core/Path.java @ 85

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 5.0 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.params.AccessInterface;
4import com.framsticks.params.CompositeParam;
5import com.framsticks.params.Param;
6import com.framsticks.util.dispatching.Dispatching;
7
8import java.util.Iterator;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.annotation.concurrent.Immutable;
13import javax.annotation.Nonnull;
14
15import org.apache.commons.collections.ListUtils;
16
17/**
18 * @author Piotr Sniegowski
19 */
20@Immutable
21public class Path {
22        // private final static Logger log = Logger.getLogger(Path.class.getName());
23
24        final LinkedList<Node> nodes = new LinkedList<Node>();
25        String textual;
26        Instance instance;
27
28        protected Object getKnownChild(AccessInterface access, CompositeParam param) {
29                Object child = access.get(param, Object.class);
30                if (child == null) {
31                        return null;
32                }
33                return (instance.registry.prepareAccess(param) != null) ? child : null;
34        }
35
36        // public Path constructPath(Instance instance, String textual) {
37        //      assert instance.isActive();
38
39        // }
40
41        Path(Instance instance, String textual) {
42                assert instance.isActive();
43                this.instance = instance;
44
45                nodes.add(instance.root);
46                Node current = instance.root;
47
48                StringBuilder b = new StringBuilder();
49                Iterator<String> i = Instance.splitPath(textual);
50                while (i.hasNext() && current.getObject() != null) {
51                        AccessInterface access = instance.registry.prepareAccess(current.getParam());
52                        if (access == null) {
53                                break;
54                        }
55                        String e = i.next();
56                        Param p = access.getParam(e);
57                        if (!(p instanceof CompositeParam)) {
58                                //entries.add(new Entry());
59                                break;
60                        }
61                        CompositeParam c = (CompositeParam)p;
62                        b.append("/").append(e);
63                        access.select(current.getObject());
64                        current = new Node(c, getKnownChild(access, c));
65                        nodes.add(current);
66                }
67                this.textual = (size() == 1) ? "/" : b.toString();
68        }
69
70        public Path(@Nonnull Instance instance, List<Node> nodes, Node node) {
71                this.instance = instance;
72                StringBuilder b = new StringBuilder();
73                boolean add = false;
74                for (Node n : nodes) {
75                        this.nodes.add(n);
76                        if (add) {
77                                b.append("/").append(n.getParam().getId());
78                        }
79                        add = true;
80                        if (n == node) {
81                                break;
82                        }
83                }
84                this.textual = (size() == 1) ? "/" : b.toString();
85        }
86
87        protected Path() {
88
89        }
90
91        public Path appendNode(Node node) {
92                assert isResolved();
93                Path result = new Path();
94                result.textual = textual + ((size() == 1) ? "" : "/") + node.getParam().getId();
95                result.instance = instance;
96                result.nodes.addAll(nodes);
97                result.nodes.add(node);
98                return result;
99        }
100
101        public Path appendParam(CompositeParam param) {
102                assert isResolved();
103                return appendNode(new Node(param, null));
104        }
105
106        public Path appendResolution(Object object) {
107                assert !isResolved();
108                Path result = new Path();
109                result.textual = textual;
110                result.instance = instance;
111                result.nodes.addAll(nodes);
112                result.nodes.add(new Node(result.nodes.pollLast().getParam(), object));
113                return result;
114        }
115
116        public final Object getTopObject() {
117                return getTop().getObject();
118        }
119
120        public final Node getTop() {
121                return nodes.getLast();
122        }
123
124        public final Node getUnder() {
125                assert nodes.size() >= 2;
126                return nodes.get(nodes.size() - 2);
127        }
128
129        public final String getTextual() {
130                return textual;
131        }
132
133        public String toString() {
134                return instance + textual + (!isResolved() ? "!" : "");
135        }
136
137        public final int size() {
138                assert Dispatching.isThreadSafe();
139                return nodes.size();
140        }
141
142        public final boolean isResolved() {
143                assert Dispatching.isThreadSafe();
144                return getTop().getObject() != null;
145        }
146
147        public final boolean isResolved(String textual) {
148                assert Dispatching.isThreadSafe();
149                return isTheSame(textual) && isResolved();
150        }
151
152        public final boolean isTheSame(String textual) {
153                assert Dispatching.isThreadSafe();
154                return this.textual.equals(textual);
155        }
156
157        public final Instance getInstance() {
158                assert Dispatching.isThreadSafe();
159                return instance;
160        }
161
162
163        /** Attach resolution at end, if available.
164         *
165         * @return Modified path, if resolution was available, this otherwise.
166         */
167        public Path tryFindResolution() {
168                assert instance.isActive();
169                assert !isResolved();
170                if (size() == 1) {
171                        return new Path(instance, "/");//appendResolution(instance.root.object);
172                }
173                Object child = getKnownChild(instance.bindAccess(getUnder()), getTop().getParam());
174                if (child == null) {
175                        return this;
176                }
177                return appendResolution(child);
178        }
179
180        public boolean matches(Path p) {
181                assert Dispatching.isThreadSafe();
182                assert instance == p.instance;
183                Iterator<Node> a = nodes.iterator();
184                Iterator<Node> b = p.nodes.iterator();
185                while (a.hasNext() && b.hasNext()) {
186                        Node an = a.next();
187                        Node bn = b.next();
188                        if (an.object != bn.object) {
189                                return false;
190                        }
191                }
192                return a.hasNext() == b.hasNext();
193        }
194
195        public String getLastElement() {
196                return getTop().getParam().getId();
197        }
198
199        public final boolean isOwner(Instance instance) {
200                return this.instance == instance;
201        }
202
203        public void setInstance(Instance instance) {
204                this.instance = instance;
205        }
206
207        @SuppressWarnings("unchecked")
208        public
209        List<Node> getNodes() {
210                return ListUtils.unmodifiableList(nodes);
211        }
212}
213
Note: See TracBrowser for help on using the repository browser.