View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.doc;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.lang.ObjectUtils;
21  import org.apache.commons.lang.StringUtils;
22  
23  /**
24   * Stores the package and type name of a UUT type name.
25   *
26   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
27   * @version $Revision:591 $
28   */
29  public final class Type implements Serializable, Comparable<Type>
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     */
38    private static final long serialVersionUID = 1L;
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The package part of the type name.
44     *
45     * @serial
46     */
47    private final String packageName;
48  
49    /**
50     * The name of the type without the package part.
51     *
52     * @serial
53     */
54    private final String typeName;
55  
56    /**
57     * The pre-calculated hash code.
58     *
59     * @serial
60     */
61    private final int preCalcHashCode;
62  
63    // ****************************** Initializer *******************************
64  
65    // ****************************** Constructors ******************************
66  
67    /**
68     * Default constructor.
69     *
70     * @param type the type as a string.
71     * @throws IllegalArgumentException if <code>type</code> is blank or has a dot
72     *           at the end.
73     */
74    public Type(final String type)
75    {
76      checkArguments(type);
77      final int index = type.lastIndexOf('.');
78      if (index != -1)
79      {
80        packageName = type.substring(0, index);
81        typeName = type.substring(index + 1);
82      }
83      else
84      {
85        packageName = null;
86        typeName = type;
87      }
88  
89      preCalcHashCode = calculateHashCode();
90    }
91  
92    // ****************************** Inner Classes *****************************
93  
94    // ********************************* Methods ********************************
95  
96    // --- init -----------------------------------------------------------------
97  
98    private static void checkArguments(final String type)
99    {
100     if (StringUtils.isBlank(type))
101     {
102       throw new IllegalArgumentException("The type must not be blank.");
103     }
104     if (type.endsWith("."))
105     {
106       throw new IllegalArgumentException(
107           "The type is not valid. It ends with a dot: " + type);
108     }
109   }
110 
111   private int calculateHashCode()
112   {
113     int result = 17;
114     result = 37 * result + ObjectUtils.hashCode(packageName);
115     result = 37 * result + ObjectUtils.hashCode(typeName);
116 
117     return result;
118   }
119 
120   // --- get&set --------------------------------------------------------------
121 
122   /**
123    * Returns the package part of the type name.
124    *
125    * @return the package part of the type name.
126    */
127   public String getPackageName()
128   {
129     return packageName;
130   }
131 
132   /**
133    * Returns the name of the type without the package part.
134    *
135    * @return the name of the type without the package part.
136    */
137   public String getTypeName()
138   {
139     return typeName;
140   }
141 
142   // --- business -------------------------------------------------------------
143 
144   // --- object basics --------------------------------------------------------
145 
146   /**
147    * {@inheritDoc}
148    * <p>
149    * Defines the lexicographical order on packages and type names.
150    * </p>
151    *
152    * @param other the other instance to compare to.
153    */
154   @Override
155   public int compareTo(final Type other)
156   {
157     int compare = 0;
158     if (packageName != null)
159     {
160       if (other.packageName != null)
161       {
162         compare = packageName.compareTo(other.packageName);
163       }
164       else
165       {
166         compare = 1;
167       }
168     }
169     else
170     {
171       if (other.packageName != null)
172       {
173         compare = -1;
174       }
175     }
176 
177     if (compare == 0)
178     {
179       compare = typeName.compareTo(other.typeName);
180     }
181 
182     return compare;
183   }
184 
185   /**
186    * Returns the hash code of the object.
187    *
188    * @return the hash code.
189    */
190   @Override
191   public int hashCode()
192   {
193     return preCalcHashCode;
194   }
195 
196   /**
197    * Returns <code>true</code> if the given object is semantically equal to the
198    * given object, <code>false</code> otherwise.
199    *
200    * @param object the instance to compare to.
201    * @return <code>true</code> if the given object is semantically equal to the
202    *         given object, <code>false</code> otherwise.
203    */
204   @Override
205   public boolean equals(final Object object)
206   {
207     if (this == object)
208     {
209       return true;
210     }
211     else if (object == null || getClass() != object.getClass())
212     {
213       return false;
214     }
215 
216     final Type other = (Type) object;
217 
218     return (ObjectUtils.equals(packageName, other.packageName) && typeName
219         .equals(other.typeName));
220   }
221 
222   /**
223    * Returns the string representation of the object.
224    *
225    * @return the string representation of the object.
226    */
227   @Override
228   public String toString()
229   {
230     return (packageName != null ? packageName + '.' : "") + typeName;
231   }
232 }