Source code: javatools/util/ArrayComparator.java
1 /*
2 * ForeignKeyComparator.java
3 *
4 * Created on 14 febbraio 2002, 20.28
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.util;
26
27 /**
28 * Compares an array of objects, object by object.
29 * @author Antonio Petrelli
30 * @version 0.0.1
31 */
32 public class ArrayComparator implements java.util.Comparator {
33
34 /** Creates new ForeignKeyComparator */
35 public ArrayComparator() {
36 }
37
38 /** Compares two arrays.
39 * @param obj The first term.
40 * @param obj1 The second term.
41 * @return Returns the compare value of the last different items, or 0 if they are equal.
42 */
43 public int compare(java.lang.Object obj, java.lang.Object obj1) {
44 Object[] aCols, bCols;
45 int i, aLength, bLength, retValue;
46
47 try {
48 aCols = (Object[]) obj;
49 bCols = (Object[]) obj1;
50 aLength = aCols.length;
51 bLength = bCols.length;
52 if (aLength < bLength)
53 return -1;
54 else if (aLength > bLength)
55 return 1;
56 else {
57 retValue = 0;
58 for (i=0; i < aLength && retValue == 0; i++) {
59 if (aCols[i] != null)
60 if (bCols[i] != null) {
61 retValue = ((Comparable) aCols[i]).compareTo((Comparable) bCols[i]);
62 }
63 else
64 return 1;
65 else
66 return -1;
67 }
68 return retValue;
69 }
70 }
71 catch (Exception e) {
72 return 0;
73 }
74 }
75
76 /** Checks if this object is equal to something.
77 * @param obj The object to compare to.
78 * @return <CODE>true</CODE>: yes, the objects are equal;
79 * <CODE>false</CODE>: no, they are not.
80 */
81 public boolean equals(java.lang.Object obj) {
82 return false;
83 }
84
85 }