1 package org.riverock.dbrevision.db;
2
3 import java.util.Comparator;
4 import java.io.Serializable;
5
6 import org.riverock.dbrevision.annotation.schema.db.DbForeignKeyColumn;
7
8 /**
9 * @author Sergei Maslyukov
10 * Date: 28.11.2006
11 * Time: 17:31:51
12 * <p/>
13 * $Id$
14 */
15 public class DbFkComparator implements Comparator<DbForeignKeyColumn>, Serializable {
16 private static final DbFkComparator DB_FK_COMPARATOR = new DbFkComparator();
17
18 public static DbFkComparator getInstance() {
19 return DB_FK_COMPARATOR;
20 }
21
22 /**
23 * Compares its two arguments for order. Returns a negative integer,
24 * zero, or a positive integer as the first argument is less than, equal
25 * to, or greater than the second.<p>
26 * <p/>
27 * The implementor must ensure that <tt>sgn(compare(x, y)) ==
28 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
29 * implies that <tt>compare(x, y)</tt> must throw an exception if and only
30 * if <tt>compare(y, x)</tt> throws an exception.)<p>
31 * <p/>
32 * The implementor must also ensure that the relation is transitive:
33 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
34 * <tt>compare(x, z)>0</tt>.<p>
35 * <p/>
36 * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
37 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
38 * <tt>z</tt>.<p>
39 * <p/>
40 * It is generally the case, but <i>not</i> strictly required that
41 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
42 * any comparator that violates this condition should clearly indicate
43 * this fact. The recommended language is "Note: this comparator
44 * imposes orderings that are inconsistent with equals."
45 *
46 * @param o1 the first object to be compared.
47 * @param o2 the second object to be compared.
48 * @return a negative integer, zero, or a positive integer as the
49 * first argument is less than, equal to, or greater than the
50 * second.
51 * @throws ClassCastException if the arguments' types prevent them from
52 * being compared by this Comparator.
53 */
54 public int compare(DbForeignKeyColumn o1, DbForeignKeyColumn o2) {
55 if (o1.getKeySeq()<o2.getKeySeq()) {
56 return -1;
57 }
58 else if (o1.getKeySeq()==o2.getKeySeq()) {
59 return 0;
60 }
61 return 1;
62 }
63 }