001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.managedobject;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.SerializationUtil;
008: import com.tc.object.dna.api.DNA;
009: import com.tc.object.dna.api.DNACursor;
010: import com.tc.object.dna.api.DNAWriter;
011: import com.tc.object.dna.api.LogicalAction;
012: import com.tc.objectserver.mgmt.ManagedObjectFacade;
013: import com.tc.objectserver.mgmt.PhysicalManagedObjectFacade;
014: import com.tc.util.Assert;
015:
016: import java.io.IOException;
017: import java.io.ObjectInput;
018: import java.io.ObjectOutput;
019: import java.util.Date;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: /**
025: * ManagedObjectState for dates.
026: */
027: public class DateManagedObjectState extends LogicalManagedObjectState {
028:
029: private long referenceTime;
030: private int referenceNanoTime;
031:
032: public DateManagedObjectState(long classID) {
033: super (classID);
034: }
035:
036: protected DateManagedObjectState(ObjectInput in) throws IOException {
037: super (in);
038: }
039:
040: public void apply(ObjectID objectID, DNACursor cursor,
041: BackReferences includeIDs) throws IOException {
042: while (cursor.next()) {
043: LogicalAction action = cursor.getLogicalAction();
044: int method = action.getMethod();
045: Object[] params = action.getParameters();
046:
047: switch (method) {
048: case SerializationUtil.SET_TIME:
049: Assert.assertNotNull(params[0]);
050: referenceTime = ((Long) params[0]).longValue();
051: break;
052: case SerializationUtil.SET_NANOS:
053: Assert.assertNotNull(params[0]);
054: referenceNanoTime = ((Integer) params[0]).intValue();
055: break;
056: default:
057: throw new AssertionError("Invalid action:" + method);
058: }
059: }
060: }
061:
062: public void dehydrate(ObjectID objectID, DNAWriter writer) {
063: writer.addLogicalAction(SerializationUtil.SET_TIME,
064: new Object[] { new Long(referenceTime) });
065: writer.addLogicalAction(SerializationUtil.SET_NANOS,
066: new Object[] { new Integer(referenceNanoTime) });
067: }
068:
069: protected void addAllObjectReferencesTo(Set refs) {
070: return;
071: }
072:
073: public ManagedObjectFacade createFacade(ObjectID objectID,
074: String className, int limit) {
075: Map dataCopy = new HashMap();
076: dataCopy.put("date", new Date(referenceTime));
077:
078: return new PhysicalManagedObjectFacade(objectID,
079: ObjectID.NULL_ID, className, dataCopy, false,
080: DNA.NULL_ARRAY_SIZE, false);
081: }
082:
083: protected void basicWriteTo(ObjectOutput o) throws IOException {
084: o.writeLong(referenceTime);
085: o.writeInt(referenceNanoTime);
086: }
087:
088: static DateManagedObjectState readFrom(ObjectInput in)
089: throws IOException {
090: DateManagedObjectState state = new DateManagedObjectState(in);
091: state.referenceTime = in.readLong();
092: state.referenceNanoTime = in.readInt();
093: return state;
094: }
095:
096: protected boolean basicEquals(LogicalManagedObjectState o) {
097: DateManagedObjectState dms = (DateManagedObjectState) o;
098: return dms.referenceTime == referenceTime
099: && dms.referenceNanoTime == referenceNanoTime;
100: }
101:
102: public byte getType() {
103: return DATE_TYPE;
104: }
105: }
|