1 10 package org.mmbase.util; 11 12 import java.util.*; 13 14 20 public class MultiColCompare implements CompareInterface { 21 22 25 int[] comparePosArray; 26 27 31 public MultiColCompare(int[] posArray) { 32 comparePosArray = posArray; 33 } 34 35 41 public MultiColCompare(String 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 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 69 public int compareCol(Object thisOne, Object other, int comparePosIndex) { 70 Object object1; 72 Object 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 ) 80 result = internalStringCompare(object1, object2); 81 else if(object1 instanceof Integer ) 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 101 public int compare(Object thisOne, Object other) { 102 return compareCol(thisOne, other, 0); 103 } 104 105 113 int internalIntCompare(Object thisOne, Object other) { 114 return ((Integer )thisOne).intValue()-((Integer )other).intValue(); 115 } 116 117 125 int internalStringCompare(Object thisOne, Object other) { 126 return ((String )thisOne).compareTo((String )other); 127 } 128 } 129 | Popular Tags |