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.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.*;
030:
031: /**
032: * @exclude
033: */
034: public class DoubleHandler extends LongHandler {
035:
036: private static final Double DEFAULT_VALUE = new Double(0);
037:
038: public DoubleHandler(ObjectContainerBase stream) {
039: super (stream);
040: }
041:
042: public Object coerce(ReflectClass claxx, Object obj) {
043: return Coercion4.toDouble(obj);
044: }
045:
046: public Object defaultValue() {
047: return DEFAULT_VALUE;
048: }
049:
050: protected Class primitiveJavaClass() {
051: return double.class;
052: }
053:
054: public Object primitiveNull() {
055: return DEFAULT_VALUE;
056: }
057:
058: public Object read(MarshallerFamily mf, StatefulBuffer buffer,
059: boolean redirect) throws CorruptionException {
060: return mf._primitive.readDouble(buffer);
061: }
062:
063: Object read1(Buffer buffer) {
064: return primitiveMarshaller().readDouble(buffer);
065: }
066:
067: public void write(Object a_object, Buffer a_bytes) {
068: a_bytes.writeLong(Platform4.doubleToLong(((Double) a_object)
069: .doubleValue()));
070: }
071:
072: // Comparison_______________________
073:
074: private double i_compareToDouble;
075:
076: private double dval(Object obj) {
077: return ((Double) obj).doubleValue();
078: }
079:
080: void prepareComparison1(Object obj) {
081: i_compareToDouble = dval(obj);
082: }
083:
084: boolean isEqual1(Object obj) {
085: return obj instanceof Double && dval(obj) == i_compareToDouble;
086: }
087:
088: boolean isGreater1(Object obj) {
089: return obj instanceof Double && dval(obj) > i_compareToDouble;
090: }
091:
092: boolean isSmaller1(Object obj) {
093: return obj instanceof Double && dval(obj) < i_compareToDouble;
094: }
095:
096: public Object read(ReadContext context) {
097: Long l = (Long) super .read(context);
098: return new Double(Platform4.longToDouble(l.longValue()));
099: }
100:
101: public void write(WriteContext context, Object obj) {
102: context.writeLong(Platform4.doubleToLong(((Double) obj)
103: .doubleValue()));
104: }
105: }
|