1 package org.modeshape.graph.connector.path;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.UUID;
10 import net.jcip.annotations.Immutable;
11 import org.modeshape.common.text.NoOpEncoder;
12 import org.modeshape.common.text.TextEncoder;
13 import org.modeshape.graph.ExecutionContext;
14 import org.modeshape.graph.property.Name;
15 import org.modeshape.graph.property.NameFactory;
16 import org.modeshape.graph.property.Path;
17 import org.modeshape.graph.property.Property;
18 import org.modeshape.graph.property.Path.Segment;
19
20
21
22
23 @Immutable
24 public class DefaultPathNode implements PathNode {
25
26 private final static TextEncoder TO_STRING_ENCODER = new NoOpEncoder();
27
28 private final Path path;
29 private final Map<Name, Property> properties;
30 private final List<Segment> childSegments;
31 private final UUID uuid;
32 private Set<Name> uniqueChildNames;
33
34 public DefaultPathNode( Path path,
35 UUID uuid,
36 Map<Name, Property> properties,
37 List<Segment> childSegments ) {
38 super();
39 this.path = path;
40 this.uuid = uuid;
41 this.properties = properties;
42 this.childSegments = childSegments;
43 }
44
45 public DefaultPathNode( Path path,
46 UUID uuid,
47 Iterable<Property> properties,
48 List<Segment> childSegments ) {
49 super();
50 this.path = path;
51 this.uuid = uuid;
52 this.childSegments = childSegments;
53 this.properties = new HashMap<Name, Property>();
54
55 for (Property property : properties) {
56 this.properties.put(property.getName(), property);
57 }
58 }
59
60 public List<Segment> getChildSegments() {
61 return this.childSegments;
62 }
63
64 public Path getPath() {
65 return this.path;
66 }
67
68 public UUID getUuid() {
69 return this.uuid;
70 }
71
72 public Map<Name, Property> getProperties() {
73 return Collections.unmodifiableMap(this.properties);
74 }
75
76 public Property getProperty( ExecutionContext context,
77 String name ) {
78 NameFactory nameFactory = context.getValueFactories().getNameFactory();
79
80 return getProperty(nameFactory.create(name));
81 }
82
83 public Property getProperty( Name name ) {
84 return properties.get(name);
85 }
86
87 public Set<Name> getUniqueChildNames() {
88 if (this.uniqueChildNames == null) {
89 Set<Name> childNames = new HashSet<Name>(childSegments.size());
90
91 for (Segment childSegment : childSegments) {
92 childNames.add(childSegment.getName());
93 }
94
95 this.uniqueChildNames = childNames;
96 }
97
98 return null;
99 }
100
101 @Override
102 public String toString() {
103 StringBuilder buff = new StringBuilder();
104 buff.append(path.getString(TO_STRING_ENCODER)).append('(').append(properties.keySet()).append(')');
105 return buff.toString();
106 }
107
108
109 }