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.api;
005:
006: /**
007: * A physical object change action
008: */
009: public class PhysicalAction {
010: private final String field;
011: private final Object value;
012: private final int index;
013: private final boolean isReference;
014: private final int type;
015:
016: private static final int TRUE_PHYSICAL = 1;
017: private static final int ARRAY_ELEMENT = 2;
018: private static final int ENTIRE_ARRAY = 3;
019: private static final int SUB_ARRAY = 4;
020:
021: // TODO: These 3 constructors would probably be easier to use if they were static factory methods instead
022:
023: /**
024: * Construct a physical action representing a new value for an entire array
025: * @param value The new array
026: */
027: public PhysicalAction(Object value) {
028: this (null, -1, value, false, ENTIRE_ARRAY);
029: }
030:
031: /**
032: * Construct a physical action representing a new subarray
033: * @param value The new subarray
034: * @param startPos The starting position for the new subarray
035: */
036: public PhysicalAction(Object value, int startPos) {
037: this (null, startPos, value, false, SUB_ARRAY);
038: }
039:
040: /**
041: * Construct a physical action representing a single array element
042: * change.
043: * @param index The index in the array parent
044: * @param value The new value for the array element
045: * @param isReference Whether the new value is a reference
046: */
047: public PhysicalAction(int index, Object value, boolean isReference) {
048: this (null, index, value, isReference, ARRAY_ELEMENT);
049: }
050:
051: /**
052: * Construct a physical action that consists of a field, a new value, and
053: * whether the new value is a reference.
054: */
055: public PhysicalAction(String field, Object value,
056: boolean isReference) {
057: this (field, -1, value, isReference, TRUE_PHYSICAL);
058: }
059:
060: /**
061: * Main internal constructor with all parameters.
062: * @param field Field name, will be null for all but true physical
063: * @param index Array index or -1 if not an array index
064: * @param value Value
065: * @param isReference True if reference
066: * @param type Internal type flag, not publicly exposed
067: */
068: private PhysicalAction(String field, int index, Object value,
069: boolean isReference, int type) {
070: this .field = field;
071: this .index = index;
072: this .value = value;
073: this .isReference = isReference;
074: this .type = type;
075: }
076:
077: /**
078: * Get field name, only valid if this physical action is a true physical field change.
079: * @return Field name, never null
080: * @throws IllegalStateException If called on an action that returns false for {@link #isTruePhysical()}
081: */
082: public String getFieldName() {
083: if (type != TRUE_PHYSICAL) {
084: throw new IllegalStateException(String.valueOf(type));
085: }
086:
087: return this .field;
088: }
089:
090: /**
091: * Get object value
092: * @return Object value
093: */
094: public Object getObject() {
095: return this .value;
096: }
097:
098: /**
099: * Is the object a reference?
100: * @return True if reference
101: */
102: public boolean isReference() {
103: return isReference;
104: }
105:
106: /**
107: * If this is an array element, the index of the element. If this is a subarray,
108: * the starting position of the new subarray. Otherwise, an error.
109: * @return The array index
110: * @throws IllegalStateException If not an array element or subarray
111: */
112: public int getArrayIndex() {
113: if (!((type == ARRAY_ELEMENT) || (type == SUB_ARRAY))) {
114: throw new IllegalStateException(String.valueOf(type));
115: }
116: return this .index;
117: }
118:
119: /**
120: * @return True if this is a true physical field change
121: */
122: public boolean isTruePhysical() {
123: return type == TRUE_PHYSICAL;
124: }
125:
126: /**
127: * @return True if this is an array element change
128: */
129: public boolean isArrayElement() {
130: return type == ARRAY_ELEMENT;
131: }
132:
133: /**
134: * @return True if this is an entire array change
135: */
136: public boolean isEntireArray() {
137: return type == ENTIRE_ARRAY;
138: }
139:
140: /**
141: * @return True if this is a subarray change
142: */
143: public boolean isSubArray() {
144: return type == SUB_ARRAY;
145: }
146:
147: }
|