View Javadoc

1   /*
2    * org.riverock.dbrevision - Database revision engine
3    * For more information about DbRevision, please visit project site
4    * http://www.riverock.org
5    *
6    * Copyright (C) 2006-2006, Riverock Software, All Rights Reserved.
7    *
8    * Riverock - The Open-source Java Development Community
9    * http://www.riverock.org
10   *
11   *
12   * This library is free software; you can redistribute it and/or
13   * modify it under the terms of the GNU Lesser General Public
14   * License as published by the Free Software Foundation; either
15   * version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25   */
26  package org.riverock.dbrevision.db;
27  
28  import java.util.Comparator;
29  import java.io.Serializable;
30  
31  import org.riverock.dbrevision.annotation.schema.db.DbPrimaryKeyColumn;
32  
33  /**
34   * @author Sergei Maslyukov
35   *         Date: 28.11.2006
36   *         Time: 17:31:51
37   *         <p/>
38   *         $Id$
39   */
40  public class DbPkComparator implements Comparator<DbPrimaryKeyColumn>, Serializable {
41      private final static DbPkComparator DB_PK_COMPARATOR = new DbPkComparator();
42  
43      public static DbPkComparator getInstance() {
44          return DB_PK_COMPARATOR;
45      }
46  
47      /**
48       * Compares its two arguments for order.  Returns a negative integer,
49       * zero, or a positive integer as the first argument is less than, equal
50       * to, or greater than the second.<p>
51       * <p/>
52       * The implementor must ensure that <tt>sgn(compare(x, y)) ==
53       * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
54       * implies that <tt>compare(x, y)</tt> must throw an exception if and only
55       * if <tt>compare(y, x)</tt> throws an exception.)<p>
56       * <p/>
57       * The implementor must also ensure that the relation is transitive:
58       * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
59       * <tt>compare(x, z)&gt;0</tt>.<p>
60       * <p/>
61       * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt>
62       * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
63       * <tt>z</tt>.<p>
64       * <p/>
65       * It is generally the case, but <i>not</i> strictly required that
66       * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
67       * any comparator that violates this condition should clearly indicate
68       * this fact.  The recommended language is "Note: this comparator
69       * imposes orderings that are inconsistent with equals."
70       *
71       * @param o1 the first object to be compared.
72       * @param o2 the second object to be compared.
73       * @return a negative integer, zero, or a positive integer as the
74       *         first argument is less than, equal to, or greater than the
75       *         second.
76       * @throws ClassCastException if the arguments' types prevent them from
77       *                            being compared by this Comparator.
78       */
79      public int compare(DbPrimaryKeyColumn o1, DbPrimaryKeyColumn o2) {
80          if (o1.getKeySeq()<o2.getKeySeq()) {
81              return -1;
82          }
83          else if (o1.getKeySeq()==o2.getKeySeq()) {
84              return 0;
85          }
86          return 1;
87      }
88  }