1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.UUID;
30 import net.jcip.annotations.Immutable;
31 import org.modeshape.graph.property.Path;
32 import org.modeshape.graph.property.Property;
33 import org.modeshape.graph.property.basic.BasicSingleValueProperty;
34
35
36
37
38
39
40
41 @Immutable
42 class LocationWithPathAndProperties extends LocationWithPath {
43
44 private static final long serialVersionUID = 1L;
45
46 private final List<Property> idProperties;
47
48
49
50
51
52
53
54 LocationWithPathAndProperties( Path path,
55 List<Property> idProperties ) {
56 super(path);
57 assert idProperties != null;
58 assert !idProperties.isEmpty();
59 this.idProperties = Collections.unmodifiableList(idProperties);
60 }
61
62
63
64
65
66
67 @Override
68 public final List<Property> getIdProperties() {
69 return idProperties;
70 }
71
72
73
74
75
76
77 @Override
78 public UUID getUuid() {
79 Property property = getIdProperty(ModeShapeLexicon.UUID);
80 if (property != null && !property.isEmpty()) {
81 Object value = property.getFirstValue();
82 if (value instanceof UUID) return (UUID)value;
83 }
84 return null;
85 }
86
87
88
89
90
91
92 @Override
93 public boolean hasIdProperties() {
94 return idProperties.size() > 0;
95 }
96
97
98
99
100
101
102 @Override
103 public Location with( Property newIdProperty ) {
104 if (newIdProperty == null || newIdProperty.isEmpty()) return this;
105
106 List<Property> newIdProperties;
107
108 if (hasIdProperties()) {
109 newIdProperties = new ArrayList<Property>(idProperties.size() + 1);
110 for (Property property : idProperties) {
111 if (!newIdProperty.getName().equals(property.getName())) newIdProperties.add(property);
112 }
113 newIdProperties.add(newIdProperty);
114 newIdProperties = Collections.unmodifiableList(newIdProperties);
115 return create(getPath(), newIdProperties);
116 }
117 return create(getPath(), newIdProperty);
118 }
119
120
121
122
123
124
125 @Override
126 public Location with( Path newPath ) {
127 if (newPath == null || newPath.equals(this.getPath())) return this;
128 return create(newPath, idProperties);
129 }
130
131
132
133
134
135
136 @Override
137 public Location with( UUID uuid ) {
138 if (uuid == null) return this;
139 Property newProperty = new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid);
140 if (this.hasIdProperties()) {
141 Property existing = this.getIdProperty(ModeShapeLexicon.UUID);
142 if (existing != null && existing.equals(newProperty)) return this;
143 }
144
145 List<Property> newIdProperties = new ArrayList<Property>(idProperties.size() + 1);
146 newIdProperties.addAll(idProperties);
147 newIdProperties.add(newProperty);
148 return create(getPath(), newIdProperties);
149 }
150 }