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.object.dna.impl;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.dna.api.DNA;
008: import com.tc.object.dna.api.DNACursor;
009: import com.tc.object.dna.api.DNAException;
010: import com.tc.object.dna.api.DNAEncoding;
011: import com.tc.object.dna.api.LogicalAction;
012: import com.tc.object.dna.api.PhysicalAction;
013:
014: import java.io.IOException;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: public class VersionizedDNAWrapper implements DNA {
019:
020: private final long version;
021: private final DNA dna;
022: private final boolean resetSupported;
023:
024: public VersionizedDNAWrapper(DNA dna, long version) {
025: this (dna, version, false);
026: }
027:
028: public VersionizedDNAWrapper(DNA dna, long version,
029: boolean resetSupported) {
030: this .dna = dna;
031: this .version = version;
032: this .resetSupported = resetSupported;
033: }
034:
035: public long getVersion() {
036: return version;
037: }
038:
039: public boolean hasLength() {
040: return dna.hasLength();
041: }
042:
043: public int getArraySize() {
044: return dna.getArraySize();
045: }
046:
047: public String getTypeName() {
048: return dna.getTypeName();
049: }
050:
051: public ObjectID getObjectID() throws DNAException {
052: return dna.getObjectID();
053: }
054:
055: public ObjectID getParentObjectID() throws DNAException {
056: return dna.getParentObjectID();
057: }
058:
059: public DNACursor getCursor() {
060: return (resetSupported ? new ResetableDNACursor(dna.getCursor())
061: : dna.getCursor());
062: }
063:
064: public String getDefiningLoaderDescription() {
065: return dna.getDefiningLoaderDescription();
066: }
067:
068: public boolean isDelta() {
069: return dna.isDelta();
070: }
071:
072: public String toString() {
073: return dna.toString();
074: }
075:
076: private static class ResetableDNACursor implements DNACursor {
077:
078: private final DNACursor cursor;
079: private final List actions = new ArrayList();
080: private int index = -1;
081:
082: public ResetableDNACursor(DNACursor cursor) {
083: this .cursor = cursor;
084: }
085:
086: public int getActionCount() {
087: return cursor.getActionCount();
088: }
089:
090: public boolean next() throws IOException {
091: if (++index < actions.size()) {
092: return true;
093: }
094: boolean success = cursor.next();
095: if (success) {
096: actions.add(cursor.getAction());
097: }
098: return success;
099: }
100:
101: public boolean next(DNAEncoding encoding) throws IOException,
102: ClassNotFoundException {
103: if (++index < actions.size()) {
104: return true;
105: }
106: boolean success = cursor.next(encoding);
107: if (success) {
108: actions.add(cursor.getAction());
109: }
110: return success;
111: }
112:
113: public void reset() throws UnsupportedOperationException {
114: index = -1;
115: }
116:
117: public LogicalAction getLogicalAction() {
118: return (index < actions.size() ? (LogicalAction) actions
119: .get(index) : cursor.getLogicalAction());
120: }
121:
122: public PhysicalAction getPhysicalAction() {
123: return (index < actions.size() ? (PhysicalAction) actions
124: .get(index) : cursor.getPhysicalAction());
125: }
126:
127: public Object getAction() {
128: return (index < actions.size() ? actions.get(index)
129: : cursor.getAction());
130: }
131:
132: public String toString() {
133: return cursor.toString();
134: }
135:
136: }
137: }
|