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.Deploy;
025: import com.db4o.foundation.Coercion4;
026: import com.db4o.internal.Buffer;
027: import com.db4o.internal.BufferPair;
028: import com.db4o.internal.Const4;
029: import com.db4o.internal.LatinStringIO;
030: import com.db4o.internal.ObjectContainerBase;
031: import com.db4o.internal.StatefulBuffer;
032: import com.db4o.internal.marshall.MarshallerFamily;
033: import com.db4o.marshall.ReadContext;
034: import com.db4o.marshall.WriteContext;
035: import com.db4o.reflect.ReflectClass;
036:
037: /**
038: * @exclude
039: */
040: public class IntHandler extends PrimitiveHandler {
041:
042: private static final Integer i_primitive = new Integer(0);
043:
044: public IntHandler(ObjectContainerBase container) {
045: super (container);
046: }
047:
048: public Object coerce(ReflectClass claxx, Object obj) {
049: return Coercion4.toInt(obj);
050: }
051:
052: public Object defaultValue() {
053: return i_primitive;
054: }
055:
056: protected Class primitiveJavaClass() {
057: return int.class;
058: }
059:
060: public int linkLength() {
061: return Const4.INT_LENGTH;
062: }
063:
064: public Object primitiveNull() {
065: return i_primitive;
066: }
067:
068: public Object read(MarshallerFamily mf, StatefulBuffer writer,
069: boolean redirect) throws CorruptionException {
070: return mf._primitive.readInteger(writer);
071: }
072:
073: Object read1(Buffer a_bytes) {
074: return new Integer(a_bytes.readInt());
075: }
076:
077: public void write(Object obj, Buffer writer) {
078: write(((Integer) obj).intValue(), writer);
079: }
080:
081: public void write(int intValue, Buffer writer) {
082: writeInt(intValue, writer);
083: }
084:
085: public static final void writeInt(int a_int, Buffer a_bytes) {
086: if (Deploy.debug) {
087: a_bytes.writeBegin(Const4.YAPINTEGER);
088: if (Deploy.debugLong) {
089: String l_s = " "
090: + new Integer(a_int).toString();
091: new LatinStringIO().write(a_bytes, l_s.substring(l_s
092: .length()
093: - Const4.INTEGER_BYTES));
094: } else {
095: for (int i = Const4.WRITE_LOOP; i >= 0; i -= 8) {
096: a_bytes._buffer[a_bytes._offset++] = (byte) (a_int >> i);
097: }
098: }
099: a_bytes.writeEnd();
100: } else {
101: a_bytes.writeInt(a_int);
102: }
103: }
104:
105: // Comparison_______________________
106:
107: private int i_compareTo;
108:
109: protected final int val(Object obj) {
110: return ((Integer) obj).intValue();
111: }
112:
113: public int compareTo(int other) {
114: return other - i_compareTo;
115: }
116:
117: public void prepareComparison(int i) {
118: i_compareTo = i;
119: }
120:
121: void prepareComparison1(Object obj) {
122: prepareComparison(val(obj));
123: }
124:
125: public int currentInt() {
126: return i_compareTo;
127: }
128:
129: boolean isEqual1(Object obj) {
130: return obj instanceof Integer && val(obj) == i_compareTo;
131: }
132:
133: boolean isGreater1(Object obj) {
134: return obj instanceof Integer && val(obj) > i_compareTo;
135: }
136:
137: boolean isSmaller1(Object obj) {
138: return obj instanceof Integer && val(obj) < i_compareTo;
139: }
140:
141: public void defragIndexEntry(BufferPair readers) {
142: readers.incrementIntSize();
143: }
144:
145: public Object read(ReadContext context) {
146: return new Integer(context.readInt());
147: }
148:
149: public void write(WriteContext context, Object obj) {
150: context.writeInt(((Integer) obj).intValue());
151: }
152: }
|