001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.graph.properties.basic;
023
024 import java.util.Arrays;
025 import java.util.Iterator;
026 import net.jcip.annotations.Immutable;
027 import org.jboss.dna.graph.properties.Name;
028 import org.jboss.dna.graph.properties.Property;
029 import org.jboss.dna.graph.properties.ValueComparators;
030
031 /**
032 * @author Randall Hauch
033 */
034 @Immutable
035 public abstract class BasicProperty implements Property {
036
037 private final Name name;
038
039 /**
040 * @param name
041 */
042 public BasicProperty( Name name ) {
043 this.name = name;
044 }
045
046 /**
047 * {@inheritDoc}
048 */
049 public Name getName() {
050 return name;
051 }
052
053 /**
054 * {@inheritDoc}
055 */
056 public Iterator<?> getValues() {
057 return iterator();
058 }
059
060 /**
061 * {@inheritDoc}
062 */
063 public Object[] getValuesAsArray() {
064 if (size() == 0) return null;
065 Object[] results = new Object[size()];
066 Iterator<?> iter = iterator();
067 int index = 0;
068 while (iter.hasNext()) {
069 Object value = iter.next();
070 results[index++] = value;
071 }
072 return results;
073 }
074
075 /**
076 * {@inheritDoc}
077 */
078 public int compareTo( Property o ) {
079 return 0;
080 }
081
082 /**
083 * {@inheritDoc}
084 */
085 @Override
086 public int hashCode() {
087 return name.hashCode();
088 }
089
090 /**
091 * {@inheritDoc}
092 */
093 @Override
094 public boolean equals( Object obj ) {
095 if (this == obj) return true;
096 if (obj instanceof Property) {
097 Property that = (Property)obj;
098 if (!this.getName().equals(that.getName())) return false;
099 if (this.size() != that.size()) return false;
100 Iterator<?> thisIter = iterator();
101 Iterator<?> thatIter = that.iterator();
102 while (thisIter.hasNext()) { // && thatIter.hasNext()
103 Object thisValue = thisIter.next();
104 Object thatValue = thatIter.next();
105 if (ValueComparators.OBJECT_COMPARATOR.compare(thisValue, thatValue) != 0) return false;
106 }
107 return true;
108 }
109 return false;
110 }
111
112 /**
113 * {@inheritDoc}
114 *
115 * @see java.lang.Object#toString()
116 */
117 @Override
118 public String toString() {
119 StringBuilder sb = new StringBuilder();
120 sb.append(getName());
121 sb.append(" = ");
122 if (isSingle()) {
123 sb.append(getValues().next());
124 } else {
125 sb.append(Arrays.asList(getValuesAsArray()));
126 }
127 return sb.toString();
128 }
129 }