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 net.jcip.annotations.Immutable;
27 import org.modeshape.common.text.TextEncoder;
28 import org.modeshape.common.util.CheckArg;
29 import org.modeshape.common.util.HashCode;
30 import org.modeshape.graph.property.Name;
31 import org.modeshape.graph.property.NamespaceRegistry;
32 import org.modeshape.graph.property.Path;
33
34
35
36
37 @Immutable
38 public class BasicName implements Name {
39
40 private String trimNonEmptyStrings( String value ) {
41 if (value == null) return null;
42 String trimmed = value.trim();
43 return trimmed.length() == 0 ? value : trimmed;
44 }
45
46
47
48 private static final long serialVersionUID = -1737537720336990144L;
49 private final String namespaceUri;
50 private final String localName;
51 private final int hc;
52
53 public BasicName( String namespaceUri,
54 String localName ) {
55 CheckArg.isNotEmpty(localName, "localName");
56 this.namespaceUri = namespaceUri != null ? namespaceUri.trim().intern() : "";
57 this.localName = trimNonEmptyStrings(localName);
58 this.hc = HashCode.compute(this.namespaceUri, this.localName);
59 }
60
61
62
63
64 public String getLocalName() {
65 return this.localName;
66 }
67
68
69
70
71 public String getNamespaceUri() {
72 return this.namespaceUri;
73 }
74
75
76
77
78 public String getString() {
79 return getString(Path.DEFAULT_ENCODER);
80 }
81
82
83
84
85 public String getString( TextEncoder encoder ) {
86 if (this.getNamespaceUri().length() == 0) {
87 if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
88 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
89 }
90 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
91
92 if (namespaceUri.length() > 0) {
93 return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName);
94 }
95 return encoder.encode(this.localName);
96 }
97
98
99
100
101 public String getString( NamespaceRegistry namespaceRegistry ) {
102 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
103 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
104 if (prefix != null && prefix.length() != 0) {
105 return prefix + ":" + this.localName;
106 }
107 return this.localName;
108 }
109
110
111
112
113 public String getString( NamespaceRegistry namespaceRegistry,
114 TextEncoder encoder ) {
115
116 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
117 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
118 if (prefix != null && prefix.length() != 0) {
119 return encoder.encode(prefix) + ":" + encoder.encode(this.localName);
120 }
121 return encoder.encode(this.localName);
122 }
123
124
125
126
127
128
129
130 public String getString( NamespaceRegistry namespaceRegistry,
131 TextEncoder encoder,
132 TextEncoder delimiterEncoder ) {
133 if (namespaceRegistry == null) {
134 if (this.getNamespaceUri().length() == 0) {
135 if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
136 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
137 }
138 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
139 if (delimiterEncoder != null) {
140 return delimiterEncoder.encode("{") + encoder.encode(this.namespaceUri) + delimiterEncoder.encode("}")
141 + encoder.encode(this.localName);
142 }
143 return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName);
144
145 }
146 if (encoder == null) encoder = Path.DEFAULT_ENCODER;
147 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
148 if (prefix != null && prefix.length() != 0) {
149 String delim = delimiterEncoder != null ? delimiterEncoder.encode(":") : ":";
150 return encoder.encode(prefix) + delim + encoder.encode(this.localName);
151 }
152 return encoder.encode(this.localName);
153 }
154
155
156
157
158 public int compareTo( Name that ) {
159 if (that == this) return 0;
160 int diff = this.getLocalName().compareTo(that.getLocalName());
161 if (diff != 0) return diff;
162 diff = this.getNamespaceUri().compareTo(that.getNamespaceUri());
163 return diff;
164 }
165
166
167
168
169 @Override
170 public int hashCode() {
171 return this.hc;
172 }
173
174
175
176
177 @Override
178 public boolean equals( Object obj ) {
179 if (obj == this) return true;
180 if (obj instanceof Name) {
181 Name that = (Name)obj;
182 if (!this.getLocalName().equals(that.getLocalName())) return false;
183 if (!this.getNamespaceUri().equals(that.getNamespaceUri())) return false;
184 return true;
185 }
186 return false;
187 }
188
189
190
191
192 @Override
193 public String toString() {
194 return "{" + this.namespaceUri + "}" + this.localName;
195 }
196
197 }