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.*;
028: import com.db4o.reflect.ReflectClass;
029:
030: /**
031: * @exclude
032: */
033: public class LongHandler extends PrimitiveHandler {
034:
035: private static final Long i_primitive = new Long(0);
036:
037: public LongHandler(ObjectContainerBase stream) {
038: super (stream);
039: }
040:
041: public Object coerce(ReflectClass claxx, Object obj) {
042: return Coercion4.toLong(obj);
043: }
044:
045: public Object defaultValue() {
046: return i_primitive;
047: }
048:
049: protected Class primitiveJavaClass() {
050: return long.class;
051: }
052:
053: public int linkLength() {
054: return Const4.LONG_LENGTH;
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: return mf._primitive.readLong(buffer);
064: }
065:
066: Object read1(Buffer a_bytes) {
067: return new Long(a_bytes.readLong());
068: }
069:
070: public void write(Object obj, Buffer buffer) {
071: writeLong(buffer, ((Long) obj).longValue());
072: }
073:
074: public static final void writeLong(WriteBuffer buffer, long val) {
075: if (Deploy.debug) {
076: Debug.writeBegin(buffer, Const4.YAPLONG);
077: }
078: if (Deploy.debug && Deploy.debugLong) {
079: String l_s = " " + val;
080: new LatinStringIO().write(buffer, l_s.substring(l_s
081: .length()
082: - Const4.LONG_BYTES));
083: } else {
084: for (int i = 0; i < Const4.LONG_BYTES; i++) {
085: buffer
086: .writeByte((byte) (val >> ((Const4.LONG_BYTES - 1 - i) * 8)));
087: }
088: }
089: if (Deploy.debug) {
090: Debug.writeEnd(buffer);
091: }
092: }
093:
094: public static final long readLong(ReadBuffer buffer) {
095: long ret = 0;
096: if (Deploy.debug) {
097: Debug.readBegin(buffer, Const4.YAPLONG);
098: }
099: if (Deploy.debug && Deploy.debugLong) {
100: ret = Long.parseLong(new LatinStringIO().read(buffer,
101: Const4.LONG_BYTES).trim());
102: } else {
103: for (int i = 0; i < Const4.LONG_BYTES; i++) {
104: ret = (ret << 8) + (buffer.readByte() & 0xff);
105: }
106: }
107: if (Deploy.debug) {
108: Debug.readEnd(buffer);
109: }
110:
111: return ret;
112: }
113:
114: // Comparison_______________________
115:
116: private long i_compareTo;
117:
118: protected final long currentLong() {
119: return i_compareTo;
120: }
121:
122: long val(Object obj) {
123: return ((Long) obj).longValue();
124: }
125:
126: void prepareComparison1(Object obj) {
127: i_compareTo = val(obj);
128: }
129:
130: boolean isEqual1(Object obj) {
131: return obj instanceof Long && val(obj) == i_compareTo;
132: }
133:
134: boolean isGreater1(Object obj) {
135: return obj instanceof Long && val(obj) > i_compareTo;
136: }
137:
138: boolean isSmaller1(Object obj) {
139: return obj instanceof Long && val(obj) < i_compareTo;
140: }
141:
142: public Object read(ReadContext context) {
143: return new Long(context.readLong());
144: }
145:
146: public void write(WriteContext context, Object obj) {
147: context.writeLong(((Long) obj).longValue());
148: }
149: }
|