1 28 package com.genimen.djeneric.repository; 29 30 import java.math.BigDecimal ; 31 import java.util.Comparator ; 32 33 import com.genimen.djeneric.repository.exceptions.DjenericException; 34 import com.genimen.djeneric.util.DjLogger; 35 36 43 public class DjDomainValue 44 { 45 Object _val = null; 46 Object _high = null; 47 int _seq = 0; 48 String _description; 49 DjDomain _domain; 50 boolean _isDynamic = false; 51 52 61 public DjDomainValue(DjDomain domain, Object val, String description) throws DjenericException 62 { 63 _domain = domain; 64 if (domain == null) _val = val; 65 else _val = domain.coerse(val); 66 _description = description; 67 if (domain != null) _isDynamic = domain.isDynamic(); 68 } 69 70 80 public DjDomainValue(DjDomain domain, String val, String high, String description) throws DjenericException 81 { 82 _domain = domain; 83 if (domain == null) 84 { 85 if (high != null && high.equals("")) high = null; 87 _val = val; 88 _high = high; 89 } 90 else 91 { 92 _val = domain.coerse(val); 93 _high = domain.coerse(high); 94 } 95 _description = description; 96 } 97 98 103 public void setSeq(int seq) 104 { 105 _seq = seq; 106 } 107 108 114 public int getSeq() 115 { 116 return _seq; 117 } 118 119 125 public void setValue(Object val) throws DjenericException 126 { 127 if (_domain != null) _val = _domain.coerse(val); 128 else _val = val; 129 } 130 131 136 public void setDescription(String descr) 137 { 138 _description = descr; 139 } 140 141 146 public Object getValue() 147 { 148 return _val; 149 } 150 151 157 public void setHigh(Object high) throws DjenericException 158 { 159 if (_domain != null) high = _domain.coerse(high); 160 else if (high != null && high.equals("")) high = null; 161 162 _high = high; 163 } 164 165 170 public Object getHigh() 171 { 172 return _high; 173 } 174 175 180 public boolean isRange() 181 { 182 return _high != null; 183 } 184 185 190 public String getDescription() 191 { 192 return _description; 193 } 194 195 201 public boolean isValid(Object val) 202 { 203 if (val == null || _val == null) return true; 204 205 if (_high == null) 206 { 207 return _val.toString().equals(val.toString()); 208 } 209 210 if (!(_high instanceof String )) 211 { 212 return isValidNumericRange(val); 213 } 214 215 return val.toString().compareTo(_high.toString()) <= 0 && val.toString().compareTo(_val.toString()) >= 0; 216 } 217 218 224 protected boolean isValidNumericRange(Object val) 225 { 226 if (val == null) return false; 227 228 try 229 { 230 val = _domain.coerse(val); 231 } 232 catch (DjenericException dex) 233 { 234 DjLogger.log(dex); 235 return false; 236 } 237 238 if (val instanceof Integer ) 239 { 240 Integer i = (Integer ) val; 241 int value = ((Integer ) _val).intValue(); 242 int high = ((Integer ) _high).intValue(); 243 int chk = i.intValue(); 244 return (chk >= value) && (chk <= high); 245 } 246 if (val instanceof Long ) 247 { 248 Long i = (Long ) val; 249 long value = ((Long ) _val).longValue(); 250 long high = ((Long ) _high).longValue(); 251 long chk = i.longValue(); 252 return (chk >= value) && (chk <= high); 253 } 254 if (val instanceof BigDecimal ) 255 { 256 BigDecimal i = (BigDecimal ) val; 257 double value = ((BigDecimal ) _val).doubleValue(); 258 double high = ((BigDecimal ) _high).doubleValue(); 259 double chk = i.doubleValue(); 260 return (chk >= value) && (chk <= high); 261 } 262 if (val instanceof String ) 263 { 264 return val.toString().compareTo(_high.toString()) <= 0 && val.toString().compareTo(_val.toString()) >= 0; 265 } 266 267 return false; 268 } 269 270 275 public String toString() 276 { 277 if (_description == null || _description.trim().length() == 0) return getValue().toString(); 278 return _description; 279 } 280 281 287 public boolean equals(Object obj) 288 { 289 if (obj instanceof DjDomainValue) 290 { 291 DjDomainValue dv = (DjDomainValue) obj; 292 Object v1 = dv.getValue(); 293 Object v2 = getValue(); 294 295 if ((v1 == null && v2 != null) || (v1 != null && v2 == null)) return false; 296 if (v1 == v2) return true; 297 298 return v1.equals(v2); 299 } 300 if (obj instanceof String ) 301 { 302 return obj.equals(getValue()); 303 } 304 305 return false; 306 } 307 308 public int hashCode() 309 { 310 if (getValue() != null) return getValue().hashCode(); 311 return super.hashCode(); 312 } 313 314 public boolean isDynamic() 315 { 316 return _isDynamic; 317 } 318 319 public void setDynamic(boolean isDynamic) 320 { 321 _isDynamic = isDynamic; 322 } 323 324 } 325 326 class DomainValueComparator implements Comparator 327 { 328 329 336 public int compare(Object o1, Object o2) 337 { 338 if (!(o1 instanceof DjDomainValue) || !(o2 instanceof DjDomainValue)) return -1; 339 DjDomainValue cu1 = (DjDomainValue) o1; 340 DjDomainValue cu2 = (DjDomainValue) o2; 341 return cu1.getSeq() - cu2.getSeq(); 342 } 343 344 350 public boolean equals(Object obj) 351 { 352 return obj == this; 353 } 354 } 355 356 class DomainValueDescrComparator implements Comparator 357 { 358 359 366 public int compare(Object o1, Object o2) 367 { 368 DjDomainValue dv1 = (DjDomainValue) o1; 369 DjDomainValue dv2 = (DjDomainValue) o2; 370 371 String str1 = dv1.getDescription(); 372 String str2 = dv2.getDescription(); 373 if (str1 == null) str1 = ""; 374 if (str2 == null) str2 = ""; 375 376 return str1.compareTo(str2); 377 } 378 379 385 public boolean equals(Object obj) 386 { 387 return false; 388 } 389 } | Popular Tags |