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.net;
022:
023: import com.db4o.internal.*;
024: import com.db4o.internal.handlers.*;
025: import com.db4o.reflect.*;
026: import com.db4o.reflect.generic.*;
027:
028: /**
029: * @exclude
030: * @sharpen.ignore
031: */
032: public abstract class NetSimpleTypeHandler extends NetTypeHandler
033: implements GenericConverter {
034:
035: private final String _name;
036: private final int _typeID;
037: private final int _byteCount;
038:
039: public NetSimpleTypeHandler(ObjectContainerBase stream, int typeID,
040: int byteCount) {
041: super (stream);
042: _name = dotNetClassName();
043: _typeID = typeID;
044: _byteCount = byteCount;
045: }
046:
047: public ReflectClass classReflector() {
048: if (_classReflector != null) {
049: return _classReflector;
050: }
051: _classReflector = _stream.reflector().forName(_name);
052: return _classReflector;
053: }
054:
055: public Object defaultValue() {
056: return new byte[_byteCount];
057: }
058:
059: public String getName() {
060: return _name;
061: }
062:
063: public int typeID() {
064: return _typeID;
065: }
066:
067: public void write(Object obj, byte[] bytes, int offset) {
068: byte[] objBytes = bytesFor(obj);
069: System.arraycopy(objBytes, 0, bytes, offset, objBytes.length);
070: }
071:
072: public Object read(byte[] bytes, int offset) {
073: byte[] ret = new byte[_byteCount];
074: System.arraycopy(bytes, offset, ret, 0, ret.length);
075: GenericObject go = new GenericObject(
076: (GenericClass) classReflector());
077: go.set(0, ret);
078: return go;
079: }
080:
081: GenericObject genericObject(Object obj) {
082: if (obj != null) {
083: return (GenericObject) obj;
084: }
085: GenericObject go = new GenericObject(
086: (GenericClass) classReflector());
087: go.set(0, defaultValue());
088: return go;
089: }
090:
091: byte[] genericObjectBytes(Object obj) {
092: GenericObject go = genericObject(obj);
093: return (byte[]) go.get(0);
094: }
095:
096: byte[] bytesFor(Object obj) {
097: if (obj instanceof byte[]) {
098: return (byte[]) obj;
099: }
100: return genericObjectBytes(obj);
101: }
102:
103: public int compare(Object compare, Object with) {
104: byte[] byteCompare = bytesFor(compare);
105: byte[] byteWith = bytesFor(with);
106: int min = byteCompare.length < byteWith.length ? byteCompare.length
107: : byteWith.length;
108: for (int i = 0; i < min; i++) {
109: if (byteCompare[i] != byteWith[i]) {
110: return byteWith[i] - byteCompare[i];
111: }
112:
113: }
114: return byteWith.length - byteCompare.length;
115: }
116:
117: public boolean isEqual(Object cmp, Object with) {
118: return compare(cmp, with) == 0;
119: }
120:
121: public String toString(GenericObject obj) {
122: return toString((byte[]) obj.get(0));
123: }
124:
125: /** @param bytes */
126: public String toString(byte[] bytes) {
127: return "";
128: }
129:
130: }
|