1 23 24 package org.objectweb.jorm.facility.naming.basidir; 25 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.naming.api.PName; 28 import org.objectweb.jorm.naming.api.PNamingContext; 29 import org.objectweb.jorm.naming.api.PExceptionNaming; 30 import org.objectweb.jorm.naming.api.PNameGetter; 31 import org.objectweb.jorm.naming.api.PNameCoder; 32 import org.objectweb.jorm.naming.api.PNameManager; 33 import org.objectweb.jorm.naming.api.PolymorphicPName; 34 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext; 35 import org.objectweb.jorm.naming.lib.BasicPolymorphicPName; 36 import org.objectweb.jorm.naming.lib.BasicPName; 37 import org.objectweb.jorm.api.PException; 38 39 import java.util.Date ; 40 import java.text.DateFormat ; 41 import java.math.BigInteger ; 42 import java.math.BigDecimal ; 43 44 48 public class BasidName extends BasicPolymorphicPName implements PNameGetter { 49 protected Object o = null; 50 protected long p = 0; 51 52 public BasidName(Object o, BasidBinder binder) throws PExceptionNaming { 53 this(o, binder, binder.codingType); 54 } 55 56 public BasidName(Object o, PNameManager binder, int codingType) throws PExceptionNaming { 57 this.pnc = binder; 58 if (o instanceof BasidName) { 59 this.o = ((BasidName) o).o; 60 this.p = ((BasidName) o).p; 61 62 } else if (o instanceof PName) { 63 switch (codingType) { 64 case PNamingContext.CTBYTE: 65 p = ((PName) o).encodeByte(); 66 break; 67 case PNamingContext.CTOBYTE: 68 o = ((PName) o).encodeObyte(); 69 break; 70 case PNamingContext.CTCHAR: 71 p = ((PName) o).encodeChar(); 72 break; 73 case PNamingContext.CTOCHAR: 74 o = ((PName) o).encodeOchar(); 75 break; 76 case PNamingContext.CTINT: 77 p = ((PName) o).encodeInt(); 78 break; 79 case PNamingContext.CTOINT: 80 o = ((PName) o).encodeOint(); 81 break; 82 case PNamingContext.CTLONG: 83 p = ((PName) o).encodeLong(); 84 break; 85 case PNamingContext.CTOLONG: 86 o = ((PName) o).encodeOlong(); 87 break; 88 case PNamingContext.CTSHORT: 89 p = ((PName) o).encodeShort(); 90 break; 91 case PNamingContext.CTOSHORT: 92 o = ((PName) o).encodeOshort(); 93 break; 94 case PNamingContext.CTDATE: 95 o = ((PName) o).encodeDate(); 96 break; 97 case PNamingContext.CTSTRING: 98 o = ((PName) o).encodeString(); 99 break; 100 case PNamingContext.CTBYTEARRAY: 101 o = ((PName) o).encode(); 102 break; 103 case PNamingContext.CTCHARARRAY: 104 o = ((PName) o).encodeCharArray(); 105 break; 106 default: 107 throw new PExceptionNaming( 108 "BasidName does not support this coding type"); 109 } 110 111 } else if (o instanceof PNameGetter) { 112 try { 113 switch (codingType) { 114 case PNameCoder.CTBYTE: 115 p = ((PNameGetter) o).pngetByteField(null, null); 116 break; 117 case PNameCoder.CTOBYTE: 118 o = ((PNameGetter) o).pngetObyteField(null, null); 119 break; 120 case PNameCoder.CTCHAR: 121 p = ((PNameGetter) o).pngetCharField(null, null); 122 break; 123 case PNameCoder.CTOCHAR: 124 o = ((PNameGetter) o).pngetOcharField(null, null); 125 break; 126 case PNameCoder.CTINT: 127 p = ((PNameGetter) o).pngetIntField(null, null); 128 break; 129 case PNameCoder.CTOINT: 130 o = ((PNameGetter) o).pngetOintField(null, null); 131 break; 132 case PNameCoder.CTLONG: 133 p = ((PNameGetter) o).pngetLongField(null, null); 134 break; 135 case PNameCoder.CTOLONG: 136 o = ((PNameGetter) o).pngetOlongField(null, null); 137 break; 138 case PNameCoder.CTSHORT: 139 p = ((PNameGetter) o).pngetShortField(null, null); 140 break; 141 case PNameCoder.CTOSHORT: 142 o = ((PNameGetter) o).pngetOshortField(null, null); 143 break; 144 case PNameCoder.CTDATE: 145 o = ((PNameGetter) o).pngetDateField(null, null); 146 break; 147 case PNameCoder.CTSTRING: 148 o = ((PNameGetter) o).pngetStringField(null, null); 149 break; 150 case PNameCoder.CTBYTEARRAY: 151 o = ((PNameGetter) o).pngetByteArrayField(null, null); 152 break; 153 case PNameCoder.CTCHARARRAY: 154 o = ((PNameGetter) o).pngetCharArrayField(null, null); 155 break; 156 default: 157 throw new PExceptionNaming( 158 "BasidName does not support this coding type"); 159 } 160 } catch (PExceptionNaming e) { 161 throw e; 162 } catch (PException e) { 163 throw new PExceptionNaming(e); 164 } 165 } else { 166 switch (codingType) { 167 case PNamingContext.CTBYTE: 168 if (o instanceof Byte ) { 169 this.p = ((Byte ) o).byteValue(); 170 } else if (o instanceof String ) { 171 this.p = Byte.parseByte((String ) o); 172 } else if (o instanceof Short ) { 173 this.p = ((Short ) o).byteValue(); 174 } else if (o instanceof Integer ) { 175 this.p = ((Integer ) o).byteValue(); 176 } else if (o instanceof Long ) { 177 this.p = ((Long ) o).byteValue(); 178 } else { 179 throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)"); 180 } 181 break; 182 case PNamingContext.CTCHAR: 183 if (o instanceof Character ) { 184 this.p = ((Character ) o).charValue(); 185 } else if (o instanceof String ) { 186 this.p = ((String ) o).charAt(0); 187 } else { 188 throw new PExceptionNaming("Cannot initalize BasidName with null value (Character object value expected)"); 189 } 190 break; 191 case PNamingContext.CTSHORT: 192 if (o instanceof Byte ) { 193 this.p = ((Byte ) o).byteValue(); 194 } else if (o instanceof String ) { 195 this.p = Short.parseShort((String ) o); 196 } else if (o instanceof Short ) { 197 this.p = ((Short ) o).shortValue(); 198 } else if (o instanceof Integer ) { 199 this.p = ((Integer ) o).shortValue(); 200 } else if (o instanceof Long ) { 201 this.p = ((Long ) o).shortValue(); 202 } else { 203 throw new PExceptionNaming("Cannot initalize BasidName with null value (Short object value expected)"); 204 } 205 break; 206 case PNamingContext.CTINT: 207 if (o instanceof Byte ) { 208 this.p = ((Byte ) o).byteValue(); 209 } else if (o instanceof String ) { 210 this.p = Integer.parseInt((String ) o); 211 } else if (o instanceof Short ) { 212 this.p = ((Short ) o).shortValue(); 213 } else if (o instanceof Integer ) { 214 this.p = ((Integer ) o).intValue(); 215 } else if (o instanceof Long ) { 216 this.p = ((Long ) o).intValue(); 217 } else { 218 throw new PExceptionNaming("Cannot initalize BasidName with null value (Integer object value expected)"); 219 } 220 break; 221 case PNamingContext.CTLONG: 222 if (o instanceof Byte ) { 223 this.p = ((Byte ) o).byteValue(); 224 } else if (o instanceof String ) { 225 this.p = Long.parseLong((String ) o); 226 } else if (o instanceof Short ) { 227 this.p = ((Short ) o).shortValue(); 228 } else if (o instanceof Integer ) { 229 this.p = ((Integer ) o).intValue(); 230 } else if (o instanceof Long ) { 231 this.p = ((Long ) o).longValue(); 232 } else { 233 throw new PExceptionNaming("Cannot initalize BasidName with null value (Long object value expected)"); 234 } 235 break; 236 case PNamingContext.CTOCHAR: 237 if (o == null || o instanceof Character ) { 238 this.o = o; 239 } else if (o instanceof String ) { 240 this.o = new Character (((String ) o).charAt(0)); 241 } else { 242 throw new PExceptionNaming("Cannot initalize BasidName with null value (Character object value expected)"); 243 } 244 break; 245 case PNamingContext.CTOBYTE: 246 if (o == null || o instanceof Byte ) { 247 this.o = o; 248 } else if (o instanceof String ) { 249 this.o = Byte.decode((String ) o); 250 } else if (o instanceof Short ) { 251 this.o = new Byte (((Short ) o).byteValue()); 252 } else if (o instanceof Integer ) { 253 this.o = new Byte (((Integer ) o).byteValue()); 254 } else if (o instanceof Long ) { 255 this.o = new Byte (((Long ) o).byteValue()); 256 } else { 257 throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)"); 258 } 259 break; 260 case PNamingContext.CTOSHORT: 261 if (o == null || o instanceof Short ) { 262 this.o = o; 263 } else if (o instanceof String ) { 264 this.o = Short.decode((String ) o); 265 } else if (o instanceof Byte ) { 266 this.o = new Short (((Byte ) o).byteValue()); 267 } else if (o instanceof Integer ) { 268 this.o = new Short (((Integer ) o).shortValue()); 269 } else if (o instanceof Long ) { 270 this.o = new Short (((Long ) o).shortValue()); 271 } else { 272 throw new PExceptionNaming("Cannot initalize BasidName with null value (Byte object value expected)"); 273 } 274 break; 275 case PNamingContext.CTOINT: 276 if (o == null || o instanceof Integer ) { 277 this.o = o; 278 } else if (o instanceof String ) { 279 this.o = Integer.decode((String ) o); 280 } else if (o instanceof Byte ) { 281 this.o = new Integer (((Byte ) o).byteValue()); 282 } else if (o instanceof Short ) { 283 this.o = new Integer (((Short ) o).shortValue()); 284 } else if (o instanceof Long ) { 285 this.o = new Integer (((Long ) o).intValue()); 286 } else { 287 throw new PExceptionNaming("Cannot initalize BasidName with null value (Integer object value expected)"); 288 } 289 break; 290 case PNamingContext.CTOLONG: 291 if (o == null || o instanceof Long ) { 292 this.o = o; 293 } else if (o instanceof String ) { 294 this.o = Long.decode((String ) o); 295 } else if (o instanceof Byte ) { 296 this.o = new Long (((Byte ) o).byteValue()); 297 } else if (o instanceof Short ) { 298 this.o = new Long (((Short ) o).shortValue()); 299 } else if (o instanceof Integer ) { 300 this.o = new Long (((Integer ) o).intValue()); 301 } else { 302 throw new PExceptionNaming("Cannot initalize BasidName with null value (Long object value expected)"); 303 } 304 break; 305 case PNamingContext.CTDATE: 306 if (o == null || o instanceof Date ) { 307 this.o = o; 308 } else if (o instanceof Long ) { 309 this.o = new Date (((Long ) o).longValue()); 310 } else if (o instanceof String ) { 311 try { 312 this.o = new Date (Long.decode((String ) o).longValue()); 313 } catch (Exception e) { 314 try { 315 this.o = DateFormat.getInstance().parse((String ) o); 316 } catch (Exception e2) { 317 throw new PExceptionNaming(e2, "Cannot initalize BasidName with null value (Date object value expected)"); 318 } 319 } 320 } else { 321 throw new PExceptionNaming("Cannot initalize BasidName with null value (Date object value expected)"); 322 } 323 default: 324 this.o = o; 325 } 326 } 327 } 328 329 public BasidName(long p, BasidBinder binder) { 330 this.pnc = binder; 331 this.p = p; 332 } 333 334 336 public boolean equals(Object obj) { 337 if (obj instanceof BasidName) { 338 BasidName pn = (BasidName) obj; 339 PolymorphicPName polymThis = (PolymorphicPName) this; 340 PolymorphicPName polymObj = (PolymorphicPName) pn; 341 if (polymThis.isPolymorphic() || polymObj.isPolymorphic()) { 343 return (o != null ? o.equals(pn.o) : p == pn.p); 344 } 345 } 346 return (this == obj) 347 || ((obj instanceof BasidName) 348 && pnc == ((BasidName) obj).pnc 349 && (o != null 350 ? o.equals(((BasidName) obj).o) 351 : p == ((BasidName) obj).p)); 352 } 353 354 public int hashCode() { 355 if (o != null) 356 return o.hashCode(); 357 return (int) p; 358 } 359 360 public String toString() { 361 BasidBinder basidBinder = null; 362 if (pnc instanceof PolymorphicPNamingContext) { 363 PolymorphicPNamingContext polymophicPNC = (PolymorphicPNamingContext) pnc; 364 365 basidBinder = (BasidBinder) polymophicPNC.getDelegatedBinder(); 366 } else { 367 basidBinder = (BasidBinder) pnc; 368 } 369 switch (basidBinder.codingType) { 370 case PNamingContext.CTBYTE: 371 return super.toString() + " / type=byte / value=" + p; 372 case PNamingContext.CTCHAR: 373 return super.toString() + " / type=char / value=" + (char) p; 374 case PNamingContext.CTINT: 375 return super.toString() + " / type=int / value=" + p; 376 case PNamingContext.CTLONG: 377 return super.toString() + " / type=long / value=" + p; 378 case PNamingContext.CTSHORT: 379 return super.toString() + " / type=short / value=" + p; 380 default: 381 return super.toString() + " / type=" + basidBinder.codingType 382 + " / value=" + o; 383 } 384 } 385 386 388 public boolean isNull() { 389 BasidBinder basidBinder = null; 390 if (pnc instanceof PolymorphicPNamingContext) { 391 PolymorphicPNamingContext polymophicPNC = (PolymorphicPNamingContext) pnc; 392 393 basidBinder = (BasidBinder) polymophicPNC.getDelegatedBinder(); 394 } else { 395 basidBinder = (BasidBinder) pnc; 396 } 397 switch (basidBinder.codingType) { 398 case PNamingContext.CTBYTE: 399 case PNamingContext.CTCHAR: 400 case PNamingContext.CTINT: 401 case PNamingContext.CTLONG: 402 case PNamingContext.CTSHORT: 403 return p == basidBinder.nullValue; 404 default: 405 return o == basidBinder.nullOValue; 406 } 407 } 408 409 public byte pngetByteField(String fn, Object ctxt) throws PException { 410 return (byte) p; 411 } 412 413 public Byte pngetObyteField(String fn, Object ctxt) throws PException { 414 return (Byte ) o; 415 } 416 417 public char pngetCharField(String fn, Object ctxt) throws PException { 418 return (char) p; 419 } 420 421 public Character pngetOcharField(String fn, Object ctxt) throws PException { 422 return (Character ) o; 423 } 424 425 public short pngetShortField(String fn, Object ctxt) throws PException { 426 return (short) p; 427 } 428 429 public Short pngetOshortField(String fn, Object ctxt) throws PException { 430 return (Short ) o; 431 } 432 433 public int pngetIntField(String fn, Object ctxt) throws PException { 434 return (int) p; 435 } 436 437 public Integer pngetOintField(String fn, Object ctxt) throws PException { 438 return (Integer ) o; 439 } 440 441 public long pngetLongField(String fn, Object ctxt) throws PException { 442 return p; 443 } 444 445 public Long pngetOlongField(String fn, Object ctxt) throws PException { 446 return (Long ) o; 447 } 448 449 public String pngetStringField(String fn, Object ctxt) throws PException { 450 return (String ) o; 451 } 452 453 public byte[] pngetByteArrayField(String fn, Object ctxt) throws PException { 454 return (byte[]) o; 455 } 456 457 public char[] pngetCharArrayField(String fn, Object ctxt) throws PException { 458 return (char[]) o; 459 } 460 461 public Date pngetDateField(String fn, Object ctxt) throws PException { 462 return (Date ) o; 463 } 464 465 public BigInteger pngetBigIntegerField(String fn, Object ctxt) throws PException { 466 return (BigInteger ) o; 467 } 468 469 public BigDecimal pngetBigDecimalField(String fn, Object ctxt) throws PException { 470 return (BigDecimal ) o; 471 } 472 } 473 | Popular Tags |