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  
22  /**
23   * A key to attach to an entity for sorting.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public final class SortKey implements Serializable, Comparable<SortKey>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     * <p>
37     * The value of this constant is {@value}.
38     * </p>
39     */
40    private static final long serialVersionUID = 1L;
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The main key part of the sort key that controls the index key.
46     *
47     * @serial
48     */
49    private final String mainKey;
50  
51    /**
52     * The key within the main key.
53     *
54     * @serial
55     */
56    private final String indexKey;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Default constructor.
64     *
65     * @param mainKey the main key part of the sort key that controls the index
66     *          key.
67     * @param indexKey the key within the main key.
68     */
69    public SortKey(final String mainKey, final String indexKey)
70    {
71      this.mainKey = mainKey;
72      this.indexKey = indexKey;
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- get&set --------------------------------------------------------------
82  
83    /**
84     * Returns the main key part of the sort key that controls the index key.
85     *
86     * @return the main key part of the sort key that controls the index key.
87     */
88    public String getMainKey()
89    {
90      return mainKey;
91    }
92  
93    /**
94     * Returns the key within the main key.
95     *
96     * @return the key within the main key.
97     */
98    public String getIndexKey()
99    {
100     return indexKey;
101   }
102 
103   // --- business -------------------------------------------------------------
104 
105   /**
106    * Checks if the sort key is condfigured or not.
107    *
108    * @return <code>false</code> if at least one key is set to a non-
109    *         <code>null</code> value, <code>true</code> if both are
110    *         <code>null</code>.
111    */
112   public boolean isDefault()
113   {
114     return mainKey == null && indexKey == null;
115   }
116 
117   // --- object basics --------------------------------------------------------
118 
119   /**
120    * Returns the hash code of the object.
121    *
122    * @return the hash code.
123    */
124   @Override
125   public int hashCode()
126   {
127     int result = 17;
128     result = 37 * result + ObjectUtils.hashCode(mainKey);
129     result = 37 * result + ObjectUtils.hashCode(indexKey);
130 
131     return result;
132   }
133 
134   /**
135    * Returns <code>true</code> if the given object is semantically equal to the
136    * given object, <code>false</code> otherwise.
137    *
138    * @param object the instance to compare to.
139    * @return <code>true</code> if the given object is semantically equal to the
140    *         given object, <code>false</code> otherwise.
141    */
142   @Override
143   public boolean equals(final Object object)
144   {
145     if (this == object)
146     {
147       return true;
148     }
149     else if (object == null || getClass() != object.getClass())
150     {
151       return false;
152     }
153 
154     final SortKey other = (SortKey) object;
155 
156     return (ObjectUtils.equals(mainKey, other.mainKey) && ObjectUtils.equals(
157         indexKey, other.indexKey));
158   }
159 
160   /**
161    * {@inheritDoc}
162    * <p>
163    * Compares the main key before the index key. A value of <code>null</code> is
164    * smaller than any other key.
165    * </p>
166    *
167    * @see java.lang.Comparable#compareTo(java.lang.Object)
168    */
169   @Override
170   public int compareTo(final SortKey other)
171   {
172     int compare = compare(mainKey, other.mainKey);
173     if (compare == 0)
174     {
175       compare = compare(indexKey, other.indexKey);
176     }
177 
178     return compare;
179   }
180 
181   private static int compare(final String thisKey, final String otherKey)
182   {
183     if (thisKey != null && otherKey != null)
184     {
185       return thisKey.compareTo(otherKey);
186     }
187     else if (thisKey == null && otherKey != null)
188     {
189       return -1;
190     }
191     else if (otherKey == null && thisKey != null)
192     {
193       return 1;
194     }
195     else
196     {
197       return 0;
198     }
199   }
200 
201   /**
202    * {@inheritDoc}
203    *
204    * @see java.lang.Object#toString()
205    */
206   @Override
207   public String toString()
208   {
209     final StringBuilder buffer = new StringBuilder(128);
210     if (mainKey != null)
211     {
212       buffer.append(mainKey);
213     }
214     if (mainKey != null && indexKey != null)
215     {
216       buffer.append('/');
217     }
218     if (indexKey != null)
219     {
220       buffer.append(indexKey);
221     }
222     return buffer.toString();
223   }
224 }