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.internal.*;
025: import com.db4o.marshall.ReadContext;
026: import com.db4o.marshall.WriteContext;
027:
028: /**
029: * @exclude
030: */
031: public final class BooleanHandler extends PrimitiveHandler {
032:
033: static final int LENGTH = 1 + Const4.ADDED_LENGTH;
034:
035: private static final byte TRUE = (byte) 'T';
036: private static final byte FALSE = (byte) 'F';
037: private static final byte NULL = (byte) 'N';
038:
039: private static final Boolean i_primitive = new Boolean(false);
040:
041: public BooleanHandler(ObjectContainerBase stream) {
042: super (stream);
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 boolean.class;
055: }
056:
057: public Object primitiveNull() {
058: return i_primitive;
059: }
060:
061: Object read1(Buffer a_bytes) {
062: if (Deploy.debug) {
063: a_bytes.readBegin(Const4.YAPBOOLEAN);
064: }
065: byte ret = a_bytes.readByte();
066: if (Deploy.debug) {
067: a_bytes.readEnd();
068: }
069:
070: if (ret == TRUE) {
071: return new Boolean(true);
072: }
073: if (ret == FALSE) {
074: return new Boolean(false);
075: }
076:
077: return null;
078: }
079:
080: public void write(Object obj, Buffer buffer) {
081: if (Deploy.debug) {
082: buffer.writeBegin(Const4.YAPBOOLEAN);
083: }
084: buffer.writeByte(getEncodedByteValue(obj));
085: if (Deploy.debug) {
086: buffer.writeEnd();
087: }
088: }
089:
090: private byte getEncodedByteValue(Object obj) {
091: if (obj == null) {
092: return NULL;
093: }
094: if (((Boolean) obj).booleanValue()) {
095: return TRUE;
096: }
097: return FALSE;
098: }
099:
100: // Comparison_______________________
101:
102: private boolean i_compareTo;
103:
104: private boolean val(Object obj) {
105: return ((Boolean) obj).booleanValue();
106: }
107:
108: void prepareComparison1(Object obj) {
109: i_compareTo = val(obj);
110: }
111:
112: boolean isEqual1(Object obj) {
113: return obj instanceof Boolean && val(obj) == i_compareTo;
114: }
115:
116: boolean isGreater1(Object obj) {
117: if (i_compareTo) {
118: return false;
119: }
120: return obj instanceof Boolean && val(obj);
121: }
122:
123: boolean isSmaller1(Object obj) {
124: if (!i_compareTo) {
125: return false;
126: }
127: return obj instanceof Boolean && !val(obj);
128: }
129:
130: public Object read(ReadContext context) {
131: if (Deploy.debug) {
132: Debug.readBegin(context, Const4.YAPBOOLEAN);
133: }
134:
135: byte ret = context.readByte();
136:
137: if (Deploy.debug) {
138: Debug.readEnd(context);
139: }
140: if (ret == TRUE) {
141: return new Boolean(true);
142: }
143: if (ret == FALSE) {
144: return new Boolean(false);
145: }
146: return null;
147: }
148:
149: public void write(WriteContext context, Object obj) {
150: if (Deploy.debug) {
151: Debug.writeBegin(context, Const4.YAPBOOLEAN);
152: }
153: context.writeByte(getEncodedByteValue(obj));
154: if (Deploy.debug) {
155: Debug.writeEnd(context);
156: }
157: }
158:
159: public Object nullRepresentationInUntypedArrays() {
160: return null;
161: }
162:
163: }
|