View Javadoc

1   package net.sourceforge.argval.version;
2   
3   
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import net.sourceforge.argval.ArgumentValidation;
8   import net.sourceforge.argval.collection.CollectionUtil;
9   import net.sourceforge.argval.collection.CollectionUtilConfig;
10  import net.sourceforge.argval.impl.ArgumentValidationImpl;
11  
12  import org.apache.commons.lang.builder.EqualsBuilder;
13  import org.apache.commons.lang.builder.HashCodeBuilder;
14  
15  
16  /**
17   * An implementation of {@link VersionNumber}.
18   * 
19   * @author <a  href="http://sourceforge.net/users/verhagent/">T. Verhagen</a>
20   */
21  public class VersionNumberImpl implements VersionNumber {
22  	/** The list containing the version number parts. */
23      private final List<String> versionList;
24      
25      /**
26       * Constructs a VersionNumber. The given List should not be <code>null</code> and at least 
27       * contain one element. No empty or <code>null</code> elements are expected. 
28       * 
29       * @param versionList - the List of String instances that build up the version number.
30       * @throws IllegalArgumentException If argument is null, contains nulls, or is empty.
31       */
32      public VersionNumberImpl(final List<String> versionList) {
33          super();
34          ArgumentValidation argVal = new ArgumentValidationImpl();
35          if (argVal.isValidCollectionWhenNoNulls("versionList", versionList)) {
36          	argVal.isValidCollectionWhenMinElements("versionList", versionList, 1);
37          }
38          if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException();
39          
40          this.versionList = new ArrayList<String>(versionList);
41      }
42  
43      public List<String> getPartitionedVersion() {
44          return new ArrayList<String>(this.versionList);
45      }
46  
47      public void accept(VersionNumberVisitor visitor) {
48          visitor.visit(this);
49      }
50  
51      public String getAsText(String separator) {
52          return CollectionUtil.toString(getPartitionedVersion(), 
53                  createCollectionUtilConfig(separator));
54      }
55      
56      private static CollectionUtilConfig createCollectionUtilConfig(String arrayValueSeparator) {
57          String arrayPrefix = null; 
58          String arrayPostfix = null; 
59  //        String arrayValueSeparator = null; 
60          String arrayValuePrefix = null;
61          String arrayValuePostfix = null;
62          String arrayKeyPrefix = null;
63          String arrayKeyPostfix = null;
64          String keyValueSeparator = null;;
65          String nullValue = null;
66          boolean isExtraSpacesActive = false;
67          return new CollectionUtilConfig(arrayPrefix, arrayPostfix, arrayValueSeparator, 
68                  arrayValuePrefix, arrayValuePostfix, arrayKeyPrefix, arrayKeyPostfix, 
69                  keyValueSeparator, nullValue, isExtraSpacesActive);
70      }
71  
72      public String toString() {
73          return CollectionUtil.toString(getPartitionedVersion());
74      }
75      
76      //@Override
77      public int hashCode() {
78      	HashCodeBuilder builder = new HashCodeBuilder(13, 17);
79      	for (int index = 0; index < versionList.size(); index++) {
80      		builder.append(versionList.get(index));
81      	}
82      	return builder.toHashCode();
83      }
84      
85      //@Override
86      public boolean equals(Object obj) {
87      	if (obj instanceof VersionNumberImpl == false) {
88  			return false;
89      	}
90      	if (this == obj) {
91  			return true;
92  	    }
93      	VersionNumberImpl other = (VersionNumberImpl)obj;
94      	if (versionList == null && other.versionList == null) {
95      		return true;
96      	}
97      	if (versionList == null || other.versionList == null 
98      			|| versionList.size() != other.versionList.size()) {
99      		return false;
100     	}
101     	EqualsBuilder builder = new EqualsBuilder();
102     	for (int index = 0; index < versionList.size(); index++) {
103     		builder.append(versionList.get(index), other.versionList.get(index));
104     	}
105     	return builder.isEquals();
106     }
107     
108 }