| 1 10 11 package org.mmbase.module.core; 12 13 import java.util.*; 14 import org.mmbase.util.*; 15 16 import org.mmbase.util.logging.Logger; 17 import org.mmbase.util.logging.Logging; 18 19 29 public class MMContainerCompare implements CompareInterface { 30 31 private static Logger log = Logging.getLoggerInstance(MMContainerCompare.class); 32 33 public final static boolean ASC=true; 34 public final static boolean DESC=false; 35 private Vector orderfields; 36 private Vector orderdirections; 37 private Hashtable dataCompare=new Hashtable(); 38 private StringCompare strcompare=new StringCompare(); 39 private IntegerCompare intcompare=new IntegerCompare(); 40 41 public MMContainerCompare() { 42 orderfields=new Vector(); 43 orderdirections=new Vector(); 44 } 45 46 public MMContainerCompare(Vector orderfields) { 47 orderdirections=new Vector(); 48 this.orderfields=orderfields; 49 for (Enumeration e=orderfields.elements();e.hasMoreElements();) { 50 e.nextElement(); 51 orderdirections.addElement(Boolean.valueOf(ASC)); 52 } 53 } 54 55 public MMContainerCompare(Vector orderfields,Vector orderdirections) { 56 this.orderfields=orderfields; 57 this.orderdirections=orderdirections; 58 } 59 60 public void setCompare(String field,CompareInterface handler) { 61 dataCompare.put(field,handler); 62 } 63 64 public void addField(String field) { 65 addField(field,ASC); 66 } 67 68 public void addField(String field,boolean direction) { 69 orderfields.addElement(field); 70 orderdirections.addElement(Boolean.valueOf(direction)); 71 } 72 73 public boolean setField(String field,boolean direction) { 74 int i; 75 boolean rtn=false; 76 77 i=orderfields.indexOf(field); 78 if (i!=-1) { 79 orderdirections.setElementAt(Boolean.valueOf(direction),i); 80 rtn=true; 81 } else { 82 rtn=false; 83 } 84 return rtn; 85 } 86 87 public void setOrderFields(Vector orderfields) { 88 this.orderfields=orderfields; 89 } 90 91 public void setOrderDirections(Vector orderdirections) { 92 this.orderdirections=orderdirections; 93 } 94 95 public void setOrder(Vector orderfields,Vector orderdirections) { 96 this.orderfields=orderfields; 97 this.orderdirections=orderdirections; 98 } 99 100 101 106 public int compare(Object thisone,Object other) { 107 MMObjectNode thisnode,othernode; 108 int rtn=0; 109 int i; 110 String field; 111 Object thisdata; 112 Object otherdata; 113 CompareInterface comp; 114 115 thisnode=(MMObjectNode)thisone; 116 othernode=(MMObjectNode)other; 117 118 i=0; 119 for (Enumeration e=orderfields.elements();e.hasMoreElements();i++) { 120 field=(String )e.nextElement(); 121 thisdata=thisnode.getValue(field); 122 otherdata=othernode.getValue(field); 123 if (thisdata!=null) { 125 if (otherdata!=null) { 126 comp=(CompareInterface)dataCompare.get(field); 128 if (comp==null) { 129 if (thisdata instanceof String ) { 130 comp=strcompare; 131 } else if (thisdata instanceof Integer ) { 132 comp=intcompare; 133 } else { 134 log.error("no handler for field " + field); 135 comp=null; 136 rtn=0; 137 } 138 } 139 if (comp!=null) { 140 rtn=comp.compare(thisdata,otherdata); 141 } 142 } else { 143 rtn=-1; 145 } 146 } else { 147 if (otherdata!=null) { 148 rtn=1; 150 } else { 151 rtn=0; 153 } 154 } 155 if (((Boolean )orderdirections.elementAt(i)).booleanValue()==DESC) { 156 rtn=0-rtn; 157 } 158 if (rtn!=0) break; 160 } 161 return rtn; 162 } 163 } 164 | Popular Tags |