KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > handlers > NetSimpleTypeHandler


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.handlers;
22
23 import com.db4o.*;
24 import com.db4o.reflect.*;
25 import com.db4o.reflect.generic.*;
26
27 /**
28  * @exclude
29  */

30 public abstract class NetSimpleTypeHandler extends YapTypeAbstract implements GenericConverter{
31     
32     private final String JavaDoc _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 JavaDoc defaultValue() {
52         return new byte[_byteCount];
53     }
54     
55     public String JavaDoc getName() {
56         return _name;
57     }
58     
59     public int typeID() {
60         return _typeID;
61     }
62     
63     public void write(Object JavaDoc obj, byte[] bytes, int offset) {
64         byte[] objBytes = bytesFor(obj);
65         System.arraycopy(objBytes, 0, bytes, offset, objBytes.length);
66     }
67
68     public Object JavaDoc 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 JavaDoc 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 JavaDoc obj) {
86         GenericObject go = genericObject(obj);
87         return (byte[])go.get(0);
88     }
89     
90     byte[] bytesFor(Object JavaDoc obj) {
91         if(obj instanceof byte[]) {
92             return (byte[])obj;
93         }
94         return genericObjectBytes(obj);
95     }
96     
97     public int compare(Object JavaDoc compare, Object JavaDoc 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 JavaDoc cmp, Object JavaDoc with) {
111         return compare(cmp, with) == 0;
112     }
113     
114     public String JavaDoc toString(GenericObject obj) {
115         return toString((byte[])obj.get(0));
116     }
117     
118     public String JavaDoc toString(byte[] bytes) {
119         return "";
120     }
121     
122     
123 }
124
Popular Tags