1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.core;
16
17 /**
18 * Extension of {@link EntityObject} that implements the {@link Comparable}
19 * interface from JDK. Thus, entities subclassing this class can be sorted
20 * according to a natural order when retrieved from database or applicative
21 * caches.
22 * <br/>
23 * This class has a generic implementation of <code>compareTo(Object o)</code>
24 * method and delegates comparison criterion retrieval to the abstract
25 * <code>getStringForComparison()</code> method. Subclasses should only implement
26 * this latest, returning the string representation of attribute used for comparison.
27 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28 * @version $Revision: 1.2 $
29 */
30 public abstract class SortableEntityObject extends EntityObject implements Comparable{
31
32
33
34 /**
35 * Get the comparision criterion as a string. Subclasses should only
36 * implement this method.
37 * @return The string representation of comparison and sort criterion
38 */
39 public abstract String getStringForComparison();
40
41
42
43
44 /**
45 * Compares this object with the specified object for order. Returns a
46 * negative integer, zero, or a positive integer as this object is less
47 * than, equal to, or greater than the specified object.<p>
48 * <p/>
49 * In the foregoing description, the notation
50 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
51 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
52 * <tt>0</tt>, or <tt>1</tt> according to whether the value of <i>expression</i>
53 * is negative, zero or positive.
54 * <p/>
55 * The implementor must ensure <tt>sgn(x.compareTo(y)) ==
56 * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
57 * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
58 * <tt>y.compareTo(x)</tt> throws an exception.)<p>
59 * <p/>
60 * The implementor must also ensure that the relation is transitive:
61 * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
62 * <tt>x.compareTo(z)>0</tt>.<p>
63 * <p/>
64 * Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt>
65 * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
66 * all <tt>z</tt>.<p>
67 * <p/>
68 * It is strongly recommended, but <i>not</i> strictly required that
69 * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
70 * class that implements the <tt>Comparable</tt> interface and violates
71 * this condition should clearly indicate this fact. The recommended
72 * language is "Note: this class has a natural ordering that is
73 * inconsistent with equals."
74 * @param o the Object to be compared.
75 * @return a negative integer, zero, or a positive integer as this object
76 * is less than, equal to, or greater than the specified object.
77 * @throws ClassCastException if the specified object's type prevents it
78 * from being compared to this Object.
79 */
80 public int compareTo(Object o){
81 SortableEntityObject otherEntity = (SortableEntityObject)o;
82 int comparison = getStringForComparison().compareToIgnoreCase(otherEntity.getStringForComparison());
83 return comparison;
84 }
85 }