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 * Unless otherwise indicated, all code in ModeShape is licensed
10 * 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.Collections;
27 import java.util.List;
28 import java.util.UUID;
29 import net.jcip.annotations.Immutable;
30 import org.modeshape.common.util.HashCode;
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 * Special location type optimized for use when only a {@link UUID} is specified. This class should never be directly instantiated
37 * by users of the ModeShape framework. Instead, use @{link Location#create} to create the correct location.
38 *
39 * @see Location
40 */
41 @Immutable
42 final class LocationWithUuid extends Location {
43
44 private static final long serialVersionUID = 1L;
45
46 private final UUID uuid;
47 private final int hashCode;
48 private final List<Property> properties;
49
50 LocationWithUuid( UUID uuid ) {
51 assert uuid != null;
52 this.uuid = uuid;
53 Property uuidProperty = new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid);
54 this.properties = Collections.singletonList(uuidProperty);
55 this.hashCode = HashCode.compute(null, this.properties);
56 }
57
58 /**
59 * {@inheritDoc}
60 *
61 * @see org.modeshape.graph.Location#getIdProperties()
62 */
63 @Override
64 public List<Property> getIdProperties() {
65 return properties;
66 }
67
68 /**
69 * {@inheritDoc}
70 *
71 * @see org.modeshape.graph.Location#getPath()
72 */
73 @Override
74 public Path getPath() {
75 return null;
76 }
77
78 /**
79 * {@inheritDoc}
80 *
81 * @see Location#hasIdProperties()
82 */
83 @Override
84 public final boolean hasIdProperties() {
85 return true;
86 }
87
88 /**
89 * {@inheritDoc}
90 *
91 * @see org.modeshape.graph.Location#hasPath()
92 */
93 @Override
94 public boolean hasPath() {
95 return false;
96 }
97
98 /**
99 * {@inheritDoc}
100 *
101 * @see org.modeshape.graph.Location#getUuid()
102 */
103 @Override
104 public UUID getUuid() {
105 return this.uuid;
106 }
107
108 /**
109 * {@inheritDoc}
110 *
111 * @see org.modeshape.graph.Location#with(org.modeshape.graph.property.Property)
112 */
113 @Override
114 public Location with( Property newIdProperty ) {
115 return Location.create(getIdProperties().get(0), newIdProperty);
116 }
117
118 /**
119 * {@inheritDoc}
120 *
121 * @see org.modeshape.graph.Location#with(org.modeshape.graph.property.Path)
122 */
123 @Override
124 public Location with( Path newPath ) {
125 return Location.create(newPath, uuid);
126 }
127
128 /**
129 * {@inheritDoc}
130 *
131 * @see org.modeshape.graph.Location#with(java.util.UUID)
132 */
133 @Override
134 public Location with( UUID uuid ) {
135 return Location.create(uuid);
136 }
137
138 /**
139 * {@inheritDoc}
140 *
141 * @see org.modeshape.graph.Location#hashCode()
142 */
143 @Override
144 public int hashCode() {
145 return hashCode;
146 }
147 }