KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > MultiColCompare


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.*;
13
14 /**
15  * MultiColCompare compares two Vectors on a given list of column numbers
16  * @application SCAN
17  * @author vpro
18  * @version $Id: MultiColCompare.java,v 1.5 2005/10/05 10:44:00 michiel Exp $
19  */

20 public class MultiColCompare implements CompareInterface {
21
22     /**
23      * Array with column numbers to compare
24      */

25     int[] comparePosArray;
26
27     /**
28      * Creates a MultiColCompare.
29      * @param posArray a list of column numbers
30      */

31     public MultiColCompare(int[] posArray) {
32         comparePosArray = posArray;
33     }
34
35     /**
36      * Constructor for comparing using multiple columns, where column position order is significant.
37      * First compare is done using first colposnr, if same then 2nd colposnr is compared etc...
38      * Column position delimiter character is ':'.
39      * @param posList - a String with colpositions syntax "x:y:..." eg. "6:4".
40      */

41     public MultiColCompare(String JavaDoc posList) {
42         int count = 1;
43         for (int i=0; i<posList.length(); i++)
44             if (posList.charAt(i)==':') count++;
45         comparePosArray = new int[count];
46         String JavaDoc s;
47         for (int i=0; i<count; i++) {
48             int pos = posList.indexOf(':');
49             if (pos==-1) s=posList;
50             else {
51                 s=posList.substring(0,pos);
52                 if (pos<posList.length()-1) posList = posList.substring(pos+1);
53                 else posList = "";
54             }
55             comparePosArray[i] = Integer.parseInt(s);
56         }
57     }
58
59     /**
60      * Make the comparison between to Vectors over all columns specified in the
61      * postiton array, starting from a specified index in that array.
62      * The result is a negative value if the first object is 'smaller' than the second,
63      * a positive value if it is 'larger', and 0 if both objects are 'equal'.
64      * @param thisOne the first object to compare. should be a <code>Vector</code>.
65      * @param other the second object to compare. should be a <code>Vector</code>.
66      * @param comparePosIndex the index in the positionarray where to start comparing
67      * @return the result of the comparison
68      */

69     public int compareCol(Object JavaDoc thisOne, Object JavaDoc other, int comparePosIndex) {
70         // log.debug("compareCol: thisOne:"+thisOne+", other:"+other+", compPosIndex:"+comparePosIndex);
71
Object JavaDoc object1;
72         Object JavaDoc object2;
73         int result = 0;
74         int comparePos = comparePosArray[comparePosIndex];
75
76         object1 = ((Vector)thisOne).elementAt(comparePos);
77         object2 = ((Vector)other).elementAt(comparePos);
78
79         if(object1 instanceof String JavaDoc)
80             result = internalStringCompare(object1, object2);
81         else if(object1 instanceof Integer JavaDoc)
82             result = internalIntCompare(object1, object2);
83
84         if (result==0) {
85             comparePosIndex++;
86             if (comparePosIndex<comparePosArray.length)
87                 result = compareCol(thisOne, other, comparePosIndex);
88         }
89         return result;
90     }
91
92     /**
93      * Make the comparison between to Vectors over all colums specified in the
94      * postiton array.
95      * The result is a negative value if the first object is 'smaller' than the second,
96      * a positive value if it is 'larger', and 0 if both objects are 'equal'.
97      * @param thisOne the first object to compare. should be a <code>Vector</code>.
98      * @param other the second object to compare. should be a <code>Vector</code>.
99      * @return the result of the comparison
100      */

101     public int compare(Object JavaDoc thisOne, Object JavaDoc other) {
102         return compareCol(thisOne, other, 0);
103     }
104
105     /**
106      * Make the comparison between two Integer objects.
107      * The result is a negative value if the first object is 'smaller' than the second,
108      * a positive value if it is 'larger', and 0 if both objects are 'equal'.
109      * @param thisOne the first object to compare. should be a <code>Integer</code>.
110      * @param other the second object to compare. should be a <code>Integer</code>.
111      * @return the result of the comparison
112      */

113     int internalIntCompare(Object JavaDoc thisOne, Object JavaDoc other) {
114         return ((Integer JavaDoc)thisOne).intValue()-((Integer JavaDoc)other).intValue();
115     }
116
117     /**
118      * Make the comparison between two String objects.
119      * The result is a negative value if the first object is 'smaller' than the second,
120      * a positive value if it is 'larger', and 0 if both objects are 'equal'.
121      * @param thisOne the first object to compare. should be a <code>String</code>.
122      * @param other the second object to compare. should be a <code>String</code>.
123      * @return the result of the comparison
124      */

125     int internalStringCompare(Object JavaDoc thisOne, Object JavaDoc other) {
126         return ((String JavaDoc)thisOne).compareTo((String JavaDoc)other);
127     }
128 }
129
Popular Tags