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 java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.marshall.*;
029: import com.db4o.marshall.*;
030: import com.db4o.reflect.*;
031:
032: /**
033: * Shared (java/.net) logic for Date handling.
034: */
035: public abstract class DateHandlerBase extends LongHandler {
036:
037: public DateHandlerBase(ObjectContainerBase container) {
038: super (container);
039: }
040:
041: public Object coerce(ReflectClass claxx, Object obj) {
042: return Handlers4.handlerCanHold(this , claxx) ? obj
043: : No4.INSTANCE;
044: }
045:
046: public abstract Object copyValue(Object from, Object to);
047:
048: public abstract Object defaultValue();
049:
050: public abstract Object primitiveNull();
051:
052: public abstract Object nullRepresentationInUntypedArrays();
053:
054: protected Class primitiveJavaClass() {
055: return null;
056: }
057:
058: public Object read(MarshallerFamily mf, StatefulBuffer writer,
059: boolean redirect) throws CorruptionException {
060: return mf._primitive.readDate(writer);
061: }
062:
063: Object read1(Buffer a_bytes) {
064: return primitiveMarshaller().readDate(a_bytes);
065: }
066:
067: public void write(Object a_object, Buffer a_bytes) {
068: // TODO: This is a temporary fix to prevent exceptions with
069: // Marshaller.LEGACY.
070: if (a_object == null) {
071: a_object = new Date(0);
072: }
073: a_bytes.writeLong(((Date) a_object).getTime());
074: }
075:
076: public static String now() {
077: return Platform4.format(new Date(), true);
078: }
079:
080: long val(Object obj) {
081: return ((Date) obj).getTime();
082: }
083:
084: boolean isEqual1(Object obj) {
085: return obj instanceof Date && val(obj) == currentLong();
086: }
087:
088: boolean isGreater1(Object obj) {
089: return obj instanceof Date && val(obj) > currentLong();
090: }
091:
092: boolean isSmaller1(Object obj) {
093: return obj instanceof Date && val(obj) < currentLong();
094: }
095:
096: public Object read(ReadContext context) {
097: long milliseconds = ((Long) super .read(context)).longValue();
098: return new Date(milliseconds);
099: }
100:
101: public void write(WriteContext context, Object obj) {
102: long milliseconds = ((Date) obj).getTime();
103: super .write(context, new Long(milliseconds));
104: }
105: }
|