1 21 package com.db4o.handlers; 22 23 import com.db4o.*; 24 import com.db4o.reflect.*; 25 import com.db4o.reflect.generic.*; 26 27 30 public abstract class NetSimpleTypeHandler extends YapTypeAbstract implements GenericConverter{ 31 32 private final String _name; 33 private final int _typeID; 34 private final int _byteCount; 35 36 public NetSimpleTypeHandler(YapStream stream, int typeID, int byteCount) { 37 super(stream); 38 _name = dotNetClassName(); 39 _typeID = typeID; 40 _byteCount = byteCount; 41 } 42 43 public ReflectClass classReflector(){ 44 if(_classReflector != null){ 45 return _classReflector; 46 } 47 _classReflector = _stream.reflector().forName(_name); 48 return _classReflector; 49 } 50 51 public Object defaultValue() { 52 return new byte[_byteCount]; 53 } 54 55 public String getName() { 56 return _name; 57 } 58 59 public int typeID() { 60 return _typeID; 61 } 62 63 public void write(Object obj, byte[] bytes, int offset) { 64 byte[] objBytes = bytesFor(obj); 65 System.arraycopy(objBytes, 0, bytes, offset, objBytes.length); 66 } 67 68 public Object read(byte[] bytes, int offset) { 69 byte[] ret = new byte[_byteCount]; 70 System.arraycopy(bytes, offset, ret, 0, ret.length); 71 GenericObject go = new GenericObject((GenericClass)classReflector()); 72 go.set(0, ret); 73 return go; 74 } 75 76 GenericObject genericObject(Object obj) { 77 if(obj != null) { 78 return (GenericObject)obj; 79 } 80 GenericObject go = new GenericObject((GenericClass)classReflector()); 81 go.set(0, defaultValue()); 82 return go; 83 } 84 85 byte[] genericObjectBytes(Object obj) { 86 GenericObject go = genericObject(obj); 87 return (byte[])go.get(0); 88 } 89 90 byte[] bytesFor(Object obj) { 91 if(obj instanceof byte[]) { 92 return (byte[])obj; 93 } 94 return genericObjectBytes(obj); 95 } 96 97 public int compare(Object compare, Object with) { 98 byte[] byteCompare = bytesFor(compare); 99 byte[] byteWith = bytesFor(with); 100 int min = byteCompare.length < byteWith.length ? byteCompare.length : byteWith.length; 101 for(int i = 0;i < min;i++) { 102 if (byteCompare[i] != byteWith[i]) { 103 return byteWith[i] - byteCompare[i]; 104 } 105 106 } 107 return byteWith.length - byteCompare.length; 108 } 109 110 public boolean isEqual(Object cmp, Object with) { 111 return compare(cmp, with) == 0; 112 } 113 114 public String toString(GenericObject obj) { 115 return toString((byte[])obj.get(0)); 116 } 117 118 public String toString(byte[] bytes) { 119 return ""; 120 } 121 122 123 } 124 | Popular Tags |