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: public final class CharHandler extends PrimitiveHandler {
029:
030: static final int LENGTH = Const4.CHAR_BYTES + Const4.ADDED_LENGTH;
031:
032: private static final Character i_primitive = new Character((char) 0);
033:
034: public CharHandler(ObjectContainerBase stream) {
035: super (stream);
036: }
037:
038: public Object defaultValue() {
039: return i_primitive;
040: }
041:
042: public int linkLength() {
043: return LENGTH;
044: }
045:
046: protected Class primitiveJavaClass() {
047: return char.class;
048: }
049:
050: public Object primitiveNull() {
051: return i_primitive;
052: }
053:
054: Object read1(Buffer a_bytes) {
055: if (Deploy.debug) {
056: a_bytes.readBegin(Const4.YAPCHAR);
057: }
058: byte b1 = a_bytes.readByte();
059: byte b2 = a_bytes.readByte();
060: if (Deploy.debug) {
061: a_bytes.readEnd();
062: }
063: char ret = (char) ((b1 & 0xff) | ((b2 & 0xff) << 8));
064: return new Character(ret);
065: }
066:
067: public void write(Object a_object, Buffer a_bytes) {
068: if (Deploy.debug) {
069: a_bytes.writeBegin(Const4.YAPCHAR);
070: }
071: char char_ = ((Character) a_object).charValue();
072: a_bytes.writeByte((byte) (char_ & 0xff));
073: a_bytes.writeByte((byte) (char_ >> 8));
074: if (Deploy.debug) {
075: a_bytes.writeEnd();
076: }
077: }
078:
079: // Comparison_______________________
080:
081: private char i_compareTo;
082:
083: private char val(Object obj) {
084: return ((Character) obj).charValue();
085: }
086:
087: void prepareComparison1(Object obj) {
088: i_compareTo = val(obj);
089: }
090:
091: boolean isEqual1(Object obj) {
092: return obj instanceof Character && val(obj) == i_compareTo;
093: }
094:
095: boolean isGreater1(Object obj) {
096: return obj instanceof Character && val(obj) > i_compareTo;
097: }
098:
099: boolean isSmaller1(Object obj) {
100: return obj instanceof Character && val(obj) < i_compareTo;
101: }
102:
103: public Object read(ReadContext context) {
104: if (Deploy.debug) {
105: Debug.readBegin(context, Const4.YAPCHAR);
106: }
107:
108: byte b1 = context.readByte();
109: byte b2 = context.readByte();
110: char charValue = (char) ((b1 & 0xff) | ((b2 & 0xff) << 8));
111:
112: if (Deploy.debug) {
113: Debug.readEnd(context);
114: }
115:
116: return new Character(charValue);
117: }
118:
119: public void write(WriteContext context, Object obj) {
120: if (Deploy.debug) {
121: Debug.writeBegin(context, Const4.YAPCHAR);
122: }
123:
124: char charValue = ((Character) obj).charValue();
125:
126: context.writeBytes(new byte[] { (byte) (charValue & 0xff),
127: (byte) (charValue >> 8) });
128:
129: if (Deploy.debug) {
130: Debug.writeEnd(context);
131: }
132: }
133:
134: }
|