1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph;
25
26 import java.util.List;
27 import java.util.UUID;
28 import net.jcip.annotations.Immutable;
29 import org.modeshape.common.text.TextEncoder;
30 import org.modeshape.common.util.HashCode;
31 import org.modeshape.graph.property.NamespaceRegistry;
32 import org.modeshape.graph.property.Path;
33 import org.modeshape.graph.property.Property;
34
35 /**
36 * Special location type optimized for use when only a path is specified. This class should never be directly instantiated by
37 * users of the ModeShape framework. Instead, use @{link Location#create} to create the correct location.
38 *
39 * @see Location
40 */
41 @Immutable
42 class LocationWithPath extends Location {
43
44 private static final long serialVersionUID = 1L;
45
46 private final Path path;
47 private final int hashCode;
48
49 LocationWithPath( Path path ) {
50 assert path != null;
51 this.path = path;
52
53 // Paths are immutable, so PathLocations are immutable...
54 // ... so we can cache the hash code.
55 hashCode = HashCode.compute(path);
56 }
57
58 /**
59 * {@inheritDoc}
60 *
61 * @see Location#getPath()
62 */
63 @Override
64 public final Path getPath() {
65 return path;
66 }
67
68 /**
69 * {@inheritDoc}
70 *
71 * @see Location#hasPath()
72 */
73 @Override
74 public final boolean hasPath() {
75 return true;
76 }
77
78 /**
79 * {@inheritDoc}
80 *
81 * @see Location#getIdProperties()
82 */
83 @Override
84 public List<Property> getIdProperties() {
85 return null;
86 }
87
88 /**
89 * {@inheritDoc}
90 *
91 * @see Location#hasIdProperties()
92 */
93 @Override
94 public boolean hasIdProperties() {
95 return false;
96 }
97
98 /**
99 * {@inheritDoc}
100 *
101 * @see Location#getUuid()
102 */
103 @Override
104 public UUID getUuid() {
105 return null;
106 }
107
108 /**
109 * {@inheritDoc}
110 *
111 * @see Location#hashCode()
112 */
113 @Override
114 public int hashCode() {
115 return hashCode;
116 }
117
118 /**
119 * {@inheritDoc}
120 *
121 * @see Location#getString(NamespaceRegistry, TextEncoder, TextEncoder)
122 */
123 @Override
124 public String getString( NamespaceRegistry namespaceRegistry,
125 TextEncoder encoder,
126 TextEncoder delimiterEncoder ) {
127 StringBuilder sb = new StringBuilder();
128 sb.append("{ ");
129 sb.append(path.getString(namespaceRegistry, encoder, delimiterEncoder));
130 sb.append(" }");
131 return sb.toString();
132 }
133
134 /**
135 * {@inheritDoc}
136 *
137 * @see Location#with(Property)
138 */
139 @Override
140 public Location with( Property newIdProperty ) {
141 if (newIdProperty == null || newIdProperty.isEmpty()) return this;
142 return create(path, newIdProperty);
143 }
144
145 /**
146 * {@inheritDoc}
147 *
148 * @see Location#with(Path)
149 */
150 @Override
151 public Location with( Path newPath ) {
152 if (newPath == null || path.equals(newPath)) return this;
153 return create(newPath);
154 }
155
156 /**
157 * {@inheritDoc}
158 *
159 * @see Location#with(UUID)
160 */
161 @Override
162 public Location with( UUID uuid ) {
163 if (uuid == null) return this;
164 return create(path, uuid);
165 }
166 }