View Javadoc

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.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   * An abstract {@link Property} implementation.
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       * @param name
48       */
49      public BasicProperty( Name name ) {
50          this.name = name;
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public Name getName() {
57          return name;
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      public Iterator<?> getValues() {
64          return iterator();
65      }
66  
67      /**
68       * {@inheritDoc}
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       * {@inheritDoc}
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()) { // && thatIter.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      * {@inheritDoc}
104      */
105     @Override
106     public int hashCode() {
107         return name.hashCode();
108     }
109 
110     /**
111      * {@inheritDoc}
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()) { // && thatIter.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      * {@inheritDoc}
134      */
135     public String getString() {
136         return getString(null, null, null);
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public String getString( TextEncoder encoder ) {
143         return getString(null, encoder, null);
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     public String getString( NamespaceRegistry namespaceRegistry ) {
150         return getString(namespaceRegistry, null, null);
151     }
152 
153     /**
154      * {@inheritDoc}
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      * {@inheritDoc}
164      * 
165      * @see org.modeshape.graph.property.Path#getString(org.modeshape.graph.property.NamespaceRegistry,
166      *      org.modeshape.common.text.TextEncoder, org.modeshape.common.text.TextEncoder)
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      * {@inheritDoc}
201      * 
202      * @see java.lang.Object#toString()
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 }