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.property.basic;
25
26 import java.util.UUID;
27 import net.jcip.annotations.Immutable;
28 import org.modeshape.common.text.TextEncoder;
29 import org.modeshape.graph.property.Path;
30 import org.modeshape.graph.property.Reference;
31
32 /**
33 * A {@link Reference} implementation that uses a single {@link UUID} as the pointer.
34 */
35 @Immutable
36 public class UuidReference implements Reference {
37
38 /**
39 */
40 private static final long serialVersionUID = 2299467578161645109L;
41 private UUID uuid;
42
43 public UuidReference( UUID uuid ) {
44 this.uuid = uuid;
45 }
46
47 /**
48 * @return uuid
49 */
50 public UUID getUuid() {
51 return this.uuid;
52 }
53
54 /**
55 * {@inheritDoc}
56 */
57 public String getString() {
58 return this.uuid.toString();
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 public String getString( TextEncoder encoder ) {
65 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
66 return encoder.encode(getString());
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 public int compareTo( Reference that ) {
73 if (this == that) return 0;
74 if (that instanceof UuidReference) {
75 return this.uuid.compareTo(((UuidReference)that).getUuid());
76 }
77 return this.getString().compareTo(that.getString());
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 @Override
84 public boolean equals( Object obj ) {
85 if (obj == this) return true;
86 if (obj instanceof UuidReference) {
87 return this.uuid.equals(((UuidReference)obj).getUuid());
88 }
89 if (obj instanceof Reference) {
90 return this.getString().equals(((Reference)obj).getString());
91 }
92 return super.equals(obj);
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public String toString() {
100 return this.uuid.toString();
101 }
102
103 }