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.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.marshall.MarshallerFamily;
027: import com.db4o.marshall.ReadContext;
028: import com.db4o.marshall.WriteContext;
029: import com.db4o.reflect.ReflectClass;
030:
031: public class ShortHandler extends PrimitiveHandler {
032:
033: static final int LENGTH = Const4.SHORT_BYTES + Const4.ADDED_LENGTH;
034:
035: private static final Short i_primitive = new Short((short) 0);
036:
037: public ShortHandler(ObjectContainerBase stream) {
038: super (stream);
039: }
040:
041: public Object coerce(ReflectClass claxx, Object obj) {
042: return Coercion4.toShort(obj);
043: }
044:
045: public Object defaultValue() {
046: return i_primitive;
047: }
048:
049: public int linkLength() {
050: return LENGTH;
051: }
052:
053: protected Class primitiveJavaClass() {
054: return short.class;
055: }
056:
057: public Object primitiveNull() {
058: return i_primitive;
059: }
060:
061: public Object read(MarshallerFamily mf, StatefulBuffer buffer,
062: boolean redirect) throws CorruptionException {
063:
064: return mf._primitive.readShort(buffer);
065: }
066:
067: Object read1(Buffer buffer) {
068: return primitiveMarshaller().readShort(buffer);
069: }
070:
071: public void write(Object a_object, Buffer a_bytes) {
072: writeShort(((Short) a_object).shortValue(), a_bytes);
073: }
074:
075: static final void writeShort(int a_short, Buffer a_bytes) {
076: if (Deploy.debug) {
077: a_bytes.writeBegin(Const4.YAPSHORT);
078: }
079: for (int i = 0; i < Const4.SHORT_BYTES; i++) {
080: a_bytes._buffer[a_bytes._offset++] = (byte) (a_short >> ((Const4.SHORT_BYTES - 1 - i) * 8));
081: }
082: if (Deploy.debug) {
083: a_bytes.writeEnd();
084: }
085: }
086:
087: // Comparison_______________________
088:
089: private short i_compareTo;
090:
091: private short val(Object obj) {
092: return ((Short) obj).shortValue();
093: }
094:
095: void prepareComparison1(Object obj) {
096: i_compareTo = val(obj);
097: }
098:
099: boolean isEqual1(Object obj) {
100: return obj instanceof Short && val(obj) == i_compareTo;
101: }
102:
103: boolean isGreater1(Object obj) {
104: return obj instanceof Short && val(obj) > i_compareTo;
105: }
106:
107: boolean isSmaller1(Object obj) {
108: return obj instanceof Short && val(obj) < i_compareTo;
109: }
110:
111: public Object read(ReadContext context) {
112: if (Deploy.debug) {
113: Debug.readBegin(context, Const4.YAPSHORT);
114: }
115: int value = ((context.readByte() & 0xff) << 8)
116: + (context.readByte() & 0xff);
117: if (Deploy.debug) {
118: Debug.readEnd(context);
119: }
120: return new Short((short) value);
121: }
122:
123: public void write(WriteContext context, Object obj) {
124: if (Deploy.debug) {
125: Debug.writeBegin(context, Const4.YAPSHORT);
126: }
127: int shortValue = ((Short) obj).shortValue();
128: context.writeBytes(new byte[] { (byte) (shortValue >> 8),
129: (byte) shortValue });
130: if (Deploy.debug) {
131: Debug.writeEnd(context);
132: }
133: }
134:
135: }
|