1 30 package com.genimen.djeneric.repository; 31 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.Comparator ; 36 37 import com.genimen.djeneric.language.Messages; 38 import com.genimen.djeneric.repository.exceptions.CanNotDeleteException; 39 import com.genimen.djeneric.repository.exceptions.DjenericException; 40 import com.genimen.djeneric.util.DjLogger; 41 42 public class DjList extends ArrayList 43 { 44 private static final long serialVersionUID = 4048790148093654840L; 45 46 DjExtent _storedType = null; 47 48 public DjList(DjExtent storedType) 49 { 50 super(); 51 _storedType = storedType; 52 } 53 54 public DjList() 55 { 56 super(); 57 } 58 59 public DjList(Collection c) 60 { 61 super(c); 62 } 63 64 public DjObject getDjenericObjectAt(int idx) 65 { 66 return (DjObject) get(idx); 67 } 68 69 public synchronized Object [] toArray() 70 { 71 DjObject[] result = new DjObject[size()]; 72 super.toArray(result); 73 return result; 74 } 75 76 public DjObject looselyMatchValue(String value) 79 { 80 for (int i = 0; i < size(); i++) 82 { 83 if (getDjenericObjectAt(i).toString().equals(value)) return getDjenericObjectAt(i); 84 } 85 for (int i = 0; i < size(); i++) 87 { 88 if (getDjenericObjectAt(i).toString().startsWith(value)) return getDjenericObjectAt(i); 89 } 90 for (int i = 0; i < size(); i++) 92 { 93 if (getDjenericObjectAt(i).toString().equalsIgnoreCase(value)) return getDjenericObjectAt(i); 94 } 95 for (int i = 0; i < size(); i++) 97 { 98 if (getDjenericObjectAt(i).toString().toLowerCase().startsWith(value.toLowerCase())) return getDjenericObjectAt(i); 99 } 100 return null; 101 } 102 103 public void sort() 104 { 105 if (getStoredType() != null) Collections.sort(this, new DjenericObjectComparator(getStoredType() 106 .getPropertySortIndices())); 107 else Collections.sort(this, new DjenericObjectComparator()); 108 } 109 110 public void sort(int[] propertyIdxList) 111 { 112 if (propertyIdxList == null || propertyIdxList.length == 0) sort(); 113 else Collections.sort(this, new DjenericObjectComparator(propertyIdxList)); 114 } 115 116 public void sort(DjExtent extent) 117 { 118 int[] sorts = extent.getPropertySortIndices(); 119 if (sorts == null || sorts.length == 0) return; 120 121 sort(sorts); 122 } 123 124 class DjenericObjectComparator implements Comparator 125 { 126 int[] _propertyIdxList = null; 127 128 public DjenericObjectComparator() 129 { 130 } 131 132 public DjenericObjectComparator(int[] propertyIdxList) 133 { 134 _propertyIdxList = propertyIdxList; 135 } 136 137 public int compare(Object o1, Object o2) 138 { 139 if (!(o1 instanceof DjObject && o1 instanceof DjObject)) return -1; 140 DjObject dv1 = (DjObject) o1; 141 DjObject dv2 = (DjObject) o2; 142 143 if (_propertyIdxList == null || _propertyIdxList.length == 0) 144 { 145 String str1 = dv1.getDescriptor(); 146 if (str1 == null) str1 = ""; 147 String str2 = dv2.getDescriptor(); 148 if (str2 == null) str2 = ""; 149 150 return str1.toLowerCase().compareTo(str2.toLowerCase()); 151 } 152 153 try 154 { 155 for (int i = 0; i < _propertyIdxList.length; i++) 157 { 158 int theIndex = Math.abs(_propertyIdxList[i]); 159 Object val1 = dv1.get(theIndex); 160 Object val2 = dv2.get(theIndex); 161 162 if (val1 instanceof Integer ) 163 { 164 int c = ((Integer ) val1).compareTo((Integer ) val2); 165 if (_propertyIdxList[i] < 0) c = -c; 166 if (c != 0) return c; 167 } 168 else if (val1 instanceof Long ) 169 { 170 int c = ((Long ) val1).compareTo((Long ) val2); 171 if (_propertyIdxList[i] < 0) c = -c; 172 if (c != 0) return c; 173 } 174 else 175 { 176 if (val1 == null && val2 != null) return -1; 177 if (val1 != null && val2 == null) return 1; 178 if (val1 == null || val2 == null) return 0; 179 180 int c = val1.toString().toLowerCase().compareTo(val2.toString().toLowerCase()); 181 if (_propertyIdxList[i] < 0) c = -c; 183 if (c != 0) return c; 184 } 185 } 186 return 0; 188 } 189 catch (DjenericException dex) 190 { 191 DjLogger.log(dex); 192 return 0; 193 } 194 } 195 } 196 197 public DjExtent getStoredType() 198 { 199 return _storedType; 200 } 201 202 public void setStoredTypeName(DjExtent storedType) 203 { 204 _storedType = storedType; 205 } 206 207 public void checkType(DjExtent someType) throws DjenericException 208 { 209 if (_storedType != null && someType != null) 210 { 211 if (!someType.isInstanceof(_storedType)) throw new DjenericException(Messages 212 .getString("DjList.wrongType", someType.getTypeName(), _storedType.getTypeName())); 213 } 214 } 215 216 public void markAllDeleted() throws CanNotDeleteException, DjenericException 217 { 218 for (int i = 0; i < size(); i++) 219 getDjenericObjectAt(i).markForDelete(); 220 clear(); 221 } 222 223 public void markAllDeletedExcluding(ArrayList lst) throws CanNotDeleteException, DjenericException 224 { 225 for (int i = 0; i < size(); i++) 226 if (!lst.contains(getDjenericObjectAt(i))) getDjenericObjectAt(i).markForDelete(); 227 } 228 229 public void purgeDeleted() 230 { 231 int i = 0; 232 while (i < size()) 233 { 234 if (getDjenericObjectAt(i).isMarkedForDelete()) remove(i); 235 else i++; 236 } 237 } 238 239 } | Popular Tags |