001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.handlers;
022:
023: import com.db4o.CorruptionException;
024: import com.db4o.internal.*;
025:
026: /**
027: * @exclude
028: */
029: public abstract class NetTypeHandler extends PrimitiveHandler implements
030: NetType {
031:
032: public NetTypeHandler(ObjectContainerBase stream) {
033: super (stream);
034: }
035:
036: private int i_linkLength;
037:
038: private Object i_compareTo;
039:
040: public abstract int compare(Object compare, Object with);
041:
042: public String dotNetClassName() {
043: String className = this .getClass().getName();
044: int pos = className.indexOf(".Net");
045: if (pos >= 0) {
046: return "System." + className.substring(pos + 4)
047: + ", mscorlib";
048: }
049: return defaultValue().getClass().getName();
050: }
051:
052: public abstract boolean isEqual(Object compare, Object with);
053:
054: public void initialize() {
055: byte[] bytes = new byte[65];
056: for (int i = 0; i < bytes.length; i++) {
057: bytes[i] = 55; // TODO: Why 55? This is a '7'. Remove.
058: }
059: write(primitiveNull(), bytes, 0);
060: for (int i = 0; i < bytes.length; i++) {
061: if (bytes[i] == 55) {
062: i_linkLength = i;
063: break;
064: }
065: }
066: }
067:
068: public int getID() {
069: return typeID();
070: }
071:
072: // This method is needed for NetSimpleTypeHandler only during
073: // initalisation and overloaded there. No abstract declaration
074: // here, so we don't have to implement the methods on .NET.
075: public String getName() {
076: return dotNetClassName();
077: }
078:
079: public int linkLength() {
080: return i_linkLength;
081: }
082:
083: protected Class primitiveJavaClass() {
084: return null;
085: }
086:
087: public Object primitiveNull() {
088: return defaultValue();
089: }
090:
091: public abstract Object read(byte[] bytes, int offset);
092:
093: Object read1(Buffer a_bytes) throws CorruptionException {
094: int offset = a_bytes._offset;
095: Object ret = read(a_bytes._buffer, a_bytes._offset);
096: a_bytes._offset = offset + linkLength();
097: return ret;
098: }
099:
100: public abstract int typeID();
101:
102: public abstract void write(Object obj, byte[] bytes, int offset);
103:
104: public void write(Object a_object, Buffer a_bytes) {
105: int offset = a_bytes._offset;
106: if (a_object != null) {
107: write(a_object, a_bytes._buffer, a_bytes._offset);
108: }
109: a_bytes._offset = offset + linkLength();
110: }
111:
112: void prepareComparison1(Object obj) {
113: i_compareTo = obj;
114: }
115:
116: boolean isEqual1(Object obj) {
117: return isEqual(i_compareTo, obj);
118: }
119:
120: boolean isGreater1(Object obj) {
121: if (classReflector().isInstance(obj)
122: && !isEqual(i_compareTo, obj)) {
123: return compare(i_compareTo, obj) > 0;
124: }
125: return false;
126: }
127:
128: boolean isSmaller1(Object obj) {
129: if (classReflector().isInstance(obj)
130: && !isEqual(i_compareTo, obj)) {
131: return compare(i_compareTo, obj) < 0;
132: }
133: return false;
134: }
135: }
|