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.Arrays;
27 import java.util.Iterator;
28 import net.jcip.annotations.Immutable;
29 import org.modeshape.common.text.TextEncoder;
30 import org.modeshape.common.util.CheckArg;
31 import org.modeshape.graph.property.Name;
32 import org.modeshape.graph.property.NamespaceRegistry;
33 import org.modeshape.graph.property.Path;
34 import org.modeshape.graph.property.Property;
35 import org.modeshape.graph.property.ValueComparators;
36
37
38
39
40 @Immutable
41 public abstract class BasicProperty implements Property {
42 private static final long serialVersionUID = 1L;
43
44 private final Name name;
45
46
47
48
49 public BasicProperty( Name name ) {
50 this.name = name;
51 }
52
53
54
55
56 public Name getName() {
57 return name;
58 }
59
60
61
62
63 public Iterator<?> getValues() {
64 return iterator();
65 }
66
67
68
69
70 public Object[] getValuesAsArray() {
71 if (size() == 0) return null;
72 Object[] results = new Object[size()];
73 Iterator<?> iter = iterator();
74 int index = 0;
75 while (iter.hasNext()) {
76 Object value = iter.next();
77 results[index++] = value;
78 }
79 return results;
80 }
81
82
83
84
85 public int compareTo( Property that ) {
86 if (this == that) return 0;
87 int diff = this.getName().compareTo(that.getName());
88 if (diff != 0) return diff;
89 diff = this.size() - that.size();
90 if (diff != 0) return diff;
91 Iterator<?> thisIter = iterator();
92 Iterator<?> thatIter = that.iterator();
93 while (thisIter.hasNext()) {
94 Object thisValue = thisIter.next();
95 Object thatValue = thatIter.next();
96 diff = ValueComparators.OBJECT_COMPARATOR.compare(thisValue, thatValue);
97 if (diff != 0) return diff;
98 }
99 return 0;
100 }
101
102
103
104
105 @Override
106 public int hashCode() {
107 return name.hashCode();
108 }
109
110
111
112
113 @Override
114 public boolean equals( Object obj ) {
115 if (this == obj) return true;
116 if (obj instanceof Property) {
117 Property that = (Property)obj;
118 if (!this.getName().equals(that.getName())) return false;
119 if (this.size() != that.size()) return false;
120 Iterator<?> thisIter = iterator();
121 Iterator<?> thatIter = that.iterator();
122 while (thisIter.hasNext()) {
123 Object thisValue = thisIter.next();
124 Object thatValue = thatIter.next();
125 if (ValueComparators.OBJECT_COMPARATOR.compare(thisValue, thatValue) != 0) return false;
126 }
127 return true;
128 }
129 return false;
130 }
131
132
133
134
135 public String getString() {
136 return getString(null, null, null);
137 }
138
139
140
141
142 public String getString( TextEncoder encoder ) {
143 return getString(null, encoder, null);
144 }
145
146
147
148
149 public String getString( NamespaceRegistry namespaceRegistry ) {
150 return getString(namespaceRegistry, null, null);
151 }
152
153
154
155
156 public String getString( NamespaceRegistry namespaceRegistry,
157 TextEncoder encoder ) {
158 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
159 return getString(namespaceRegistry, encoder, null);
160 }
161
162
163
164
165
166
167
168 public String getString( NamespaceRegistry namespaceRegistry,
169 TextEncoder encoder,
170 TextEncoder delimiterEncoder ) {
171 StringBuilder sb = new StringBuilder();
172 sb.append(getName().getString(namespaceRegistry, encoder, delimiterEncoder));
173 sb.append("=");
174 if (isEmpty()) {
175 sb.append("null");
176 } else {
177 if (isMultiple()) sb.append("[");
178 boolean first = true;
179 for (Object value : this) {
180 if (first) first = false;
181 else sb.append(",");
182 sb.append('"');
183 if (value instanceof Path) {
184 Path path = (Path)value;
185 sb.append(path.getString(namespaceRegistry, encoder, delimiterEncoder));
186 } else if (value instanceof Name) {
187 Name name = (Name)value;
188 sb.append(name.getString(namespaceRegistry, encoder, delimiterEncoder));
189 } else {
190 sb.append(value);
191 }
192 sb.append('"');
193 }
194 if (isMultiple()) sb.append("]");
195 }
196 return sb.toString();
197 }
198
199
200
201
202
203
204 @Override
205 public String toString() {
206 StringBuilder sb = new StringBuilder();
207 sb.append(getName());
208 sb.append(" = ");
209 if (isSingle()) {
210 sb.append(getValues().next());
211 } else if (isEmpty()) {
212 sb.append("null");
213 } else {
214 sb.append(Arrays.asList(getValuesAsArray()));
215 }
216 return sb.toString();
217 }
218 }