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.common.util.CheckArg;
32 import org.modeshape.graph.property.Name;
33 import org.modeshape.graph.property.Path;
34 import org.modeshape.graph.property.Property;
35 import org.modeshape.graph.property.basic.BasicSingleValueProperty;
36
37
38
39
40
41
42
43 @Immutable
44 final class LocationWithProperty extends LocationWithProperties {
45
46 private static final long serialVersionUID = 1L;
47
48
49
50
51
52
53 LocationWithProperty( Property idProperty ) {
54 super(Collections.singletonList(idProperty));
55 assert idProperty != null;
56 assert !idProperty.isEmpty();
57 }
58
59
60
61
62
63
64 @Override
65 public final Property getIdProperty( Name name ) {
66 CheckArg.isNotNull(name, "name");
67 Property property = getIdProperties().get(0);
68 return property.getName().equals(name) ? property : null;
69 }
70
71
72
73
74
75
76 @Override
77 public UUID getUuid() {
78 Property property = getIdProperties().get(0);
79 if (ModeShapeLexicon.UUID.equals(property.getName())) {
80 Object value = property.getFirstValue();
81 if (value instanceof UUID) return (UUID)value;
82 if (value instanceof String) return UUID.fromString((String)value);
83 }
84 return null;
85 }
86
87
88
89
90
91
92 @Override
93 public Location with( Property newIdProperty ) {
94 if (newIdProperty == null || newIdProperty.isEmpty()) return this;
95 Property idProperty = getIdProperties().get(0);
96 if (newIdProperty.getName().equals(idProperty.getName())) {
97 return Location.create(newIdProperty);
98 }
99 List<Property> newIdProperties = new ArrayList<Property>(getIdProperties().size() + 1);
100 newIdProperties.add(newIdProperty);
101 newIdProperties.addAll(getIdProperties());
102 return Location.create(newIdProperties);
103 }
104
105
106
107
108
109
110 @Override
111 public Location with( Path newPath ) {
112 if (newPath == null) return this;
113 Property idProperty = getIdProperties().get(0);
114 return Location.create(newPath, idProperty);
115 }
116
117
118
119
120
121
122 @Override
123 public Location with( UUID uuid ) {
124 if (uuid == null) return this;
125 Property idProperty = getIdProperties().get(0);
126 if (ModeShapeLexicon.UUID.equals(idProperty.getName())) {
127 return Location.create(uuid);
128 }
129 List<Property> newIdProperties = new ArrayList<Property>(getIdProperties().size() + 1);
130 newIdProperties.add(new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid));
131 newIdProperties.addAll(getIdProperties());
132 return Location.create(newIdProperties);
133 }
134 }