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.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
34
35 @Immutable
36 public class StringReference implements Reference {
37
38
39
40 private static final long serialVersionUID = 2299467578161645109L;
41 private
42 private
43
44 public StringReference( String id ) {
45 this.id = id;
46 this.isWeak = false;
47 }
48
49 public StringReference( String id,
50 boolean weak ) {
51 this.id = id;
52 this.isWeak = weak;
53 }
54
55
56
57
58
59
60 public boolean isWeak() {
61 return isWeak;
62 }
63
64
65
66
67 public String getString() {
68 return this.id;
69 }
70
71
72
73
74 public String getString( TextEncoder encoder ) {
75 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
76 return encoder.encode(getString());
77 }
78
79
80
81
82 public int compareTo( Reference that ) {
83 if (this == that) return 0;
84 if (this.isWeak()) {
85 if (!that.isWeak()) return -1;
86 } else {
87 if (that.isWeak()) return 1;
88 }
89 if (that instanceof StringReference) {
90 return this.id.compareTo(((StringReference)that).getString());
91 }
92 return this.getString().compareTo(that.getString());
93 }
94
95
96
97
98 @Override
99 public boolean equals( Object obj ) {
100 if (obj == this) return true;
101 if (obj instanceof Reference) {
102 Reference that = (Reference)obj;
103 return this.isWeak() == that.isWeak() && this.getString().equals(that.getString());
104 }
105 return super.equals(obj);
106 }
107
108
109
110
111 @Override
112 public String toString() {
113 return this.id;
114 }
115
116 }