…lines 1–176 unchanged (176 lines)
177 * @return -ve if this is less than the other object, +ve if greater than,
178 * <code>0</code> of equal
179 * @throws ClassCastException if other is not an <code>Enum</code>
180 * @throws NullPointerException if other is <code>null</code>
181 */
182 public int compareTo(Object other) {
183 if (other == this) {
184 return 0;
185 }
186 if (other.getClass() != this.getClass()) {
187 if (other.getClass().getName().equals(this.getClass().getName())) {
188 return iValue - getValueInOtherClassLoader(other);
189 }
190 throw new ClassCastException(
191 "Different enum class '" + ClassUtils.getShortClassName(other.getClass()) + "'");
192 }
193 return iValue - ((ValuedEnum) other).iValue;
194 }
195
196 /**
197 * <p>Use reflection to return an objects value.</p>
198 *
199 * @param other the object to determine the value for
200 * @return the value
201 */
202 private int getValueInOtherClassLoader(Object other) {
203 try {
204 Method mth = other.getClass().getMethod("getValue", null);
205 Integer value = (Integer) mth.invoke(other, null);
206 return value.intValue();
207 } catch (NoSuchMethodException e) {
208 // ignore - should never happen
209 } catch (IllegalAccessException e) {
210 // ignore - should never happen
211 } catch (InvocationTargetException e) {
212 // ignore - should never happen
213 }
214 throw new IllegalStateException("This should not happen");
215 }
216
217 /**
218 * <p>Human readable description of this <code>Enum</code> item.</p>
219 *
220 * @return String in the form <code>type[name=value]</code>, for example:
221 * <code>JavaVersion[Java 1.0=100]</code>. Note that the package name is
…lines 222–232 unchanged (11 lines)