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.Coercion4;
025: import com.db4o.internal.Buffer;
026: import com.db4o.internal.ObjectContainerBase;
027: import com.db4o.internal.StatefulBuffer;
028: import com.db4o.internal.marshall.MarshallerFamily;
029: import com.db4o.marshall.ReadContext;
030: import com.db4o.marshall.WriteContext;
031: import com.db4o.reflect.ReflectClass;
032:
033: public class FloatHandler extends IntHandler {
034:
035: private static final Float i_primitive = new Float(0);
036:
037: public FloatHandler(ObjectContainerBase stream) {
038: super (stream);
039: }
040:
041: public Object coerce(ReflectClass claxx, Object obj) {
042: return Coercion4.toFloat(obj);
043: }
044:
045: public Object defaultValue() {
046: return i_primitive;
047: }
048:
049: protected Class primitiveJavaClass() {
050: return float.class;
051: }
052:
053: public Object primitiveNull() {
054: return i_primitive;
055: }
056:
057: public Object read(MarshallerFamily mf, StatefulBuffer writer,
058: boolean redirect) throws CorruptionException {
059: return mf._primitive.readFloat(writer);
060: }
061:
062: Object read1(Buffer a_bytes) {
063: return primitiveMarshaller().readFloat(a_bytes);
064: }
065:
066: public void write(Object a_object, Buffer a_bytes) {
067: writeInt(Float.floatToIntBits(((Float) a_object).floatValue()),
068: a_bytes);
069: }
070:
071: // Comparison_______________________
072:
073: private float i_compareTo;
074:
075: private float valu(Object obj) {
076: return ((Float) obj).floatValue();
077: }
078:
079: void prepareComparison1(Object obj) {
080: i_compareTo = valu(obj);
081: }
082:
083: boolean isEqual1(Object obj) {
084: return obj instanceof Float && valu(obj) == i_compareTo;
085: }
086:
087: boolean isGreater1(Object obj) {
088: return obj instanceof Float && valu(obj) > i_compareTo;
089: }
090:
091: boolean isSmaller1(Object obj) {
092: return obj instanceof Float && valu(obj) < i_compareTo;
093: }
094:
095: public Object read(ReadContext context) {
096: return new Float(Float.intBitsToFloat(context.readInt()));
097: }
098:
099: public void write(WriteContext context, Object obj) {
100: context.writeInt(Float.floatToIntBits(((Float) obj)
101: .floatValue()));
102: }
103: }
|