001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/ObjectState.java,v 1.6 2004/05/19 19:59:44 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm;
021:
022: /**
023: * A single instance of ObjectState tracks the state of a
024: * persistent object.
025: */
026: class ObjectState {
027: // Bitwise stati
028: public static final byte STATUS_TRANSACTIONAL = 1;
029: public static final byte STATUS_DIRTY = 2;
030: public static final byte STATUS_PERSISTENT = 4;
031: public static final byte STATUS_NEW = 8;
032: public static final byte STATUS_DELETED = 16;
033: public static final byte STATUS_HOLLOW = 32 + 4;
034:
035: private static final String[] STATUS_NAME = new String[] {
036: "Transient", "Transient-Clean", null, "Transient-Dirty", /* 0 - 3 */
037: "Persistent-Nontransactional", "Persistent-Clean", null,
038: "Persistent-Dirty", /* 4 - 7 */
039: null, null, null, null, /* 8 - 11 */
040: null, "Persistent-New", null, null, /* 12 - 15 */
041: null, null, null, null, /* 16 - 19 */
042: null, "Persistent-Deleted", null, null, /* 20 - 23 */
043: null, null, null, null, /* 24 - 27 */
044: null, "Persistent-New-Deleted", null, null, /* 28 - 31 */
045: null, null, null, null, /* 32 - 35 */
046: "Hollow" /* 36 */
047: };
048:
049: // Pseudo-stati, corresponding to JDO terminology
050: public static final byte STATUS_TRANSIENT = 0;
051: public static final byte STATUS_TRANSIENT_CLEAN = STATUS_TRANSACTIONAL;
052: public static final byte STATUS_TRANSIENT_DIRTY = STATUS_TRANSACTIONAL
053: + STATUS_DIRTY;
054: public static final byte STATUS_PERSISTENT_NONTRANSACTIONAL = STATUS_PERSISTENT;
055: public static final byte STATUS_PERSISTENT_CLEAN = STATUS_PERSISTENT
056: + STATUS_TRANSACTIONAL;
057: public static final byte STATUS_PERSISTENT_DIRTY = STATUS_PERSISTENT
058: + STATUS_TRANSACTIONAL + STATUS_DIRTY;
059: public static final byte STATUS_PERSISTENT_NEW = STATUS_PERSISTENT
060: + STATUS_TRANSACTIONAL + STATUS_NEW;
061: public static final byte STATUS_PERSISTENT_DELETED = STATUS_PERSISTENT
062: + STATUS_TRANSACTIONAL + STATUS_DELETED;
063: public static final byte STATUS_PERSISTENT_NEW_DELETED = STATUS_PERSISTENT
064: + STATUS_TRANSACTIONAL + STATUS_NEW + STATUS_DELETED;
065:
066: public final String getStatusName() {
067: return STATUS_NAME[status];
068: }
069:
070: // Status of this object, one of the constants above.
071: protected byte status;
072:
073: // The object being tracked
074: protected Object proxy;
075:
076: protected ObjectState(Object proxy) {
077: this .proxy = proxy;
078: }
079:
080: // Immutables can be reused for default values
081: private static final Byte DEFAULT_BYTE = new Byte((byte) 0);
082: private static final Character DEFAULT_CHAR = new Character(
083: '\u0000');
084: private static final Double DEFAULT_DOUBLE = new Double(0.0D);
085: private static final Float DEFAULT_FLOAT = new Float(0.0F);
086: private static final Integer DEFAULT_INT = new Integer(0);
087: private static final Long DEFAULT_LONG = new Long(0L);
088: private static final Short DEFAULT_SHORT = new Short((short) 0);
089:
090: /** Returns a wrapped default value for primitive types. */
091: static Object defaultValue(Class type) {
092: if (type.equals(Integer.TYPE)) {
093: return DEFAULT_INT;
094: }
095: if (type.equals(Long.TYPE)) {
096: return DEFAULT_LONG;
097: }
098: if (type.equals(Short.TYPE)) {
099: return DEFAULT_SHORT;
100: }
101: if (type.equals(Float.TYPE)) {
102: return DEFAULT_FLOAT;
103: }
104: if (type.equals(Double.TYPE)) {
105: return DEFAULT_DOUBLE;
106: }
107: if (type.equals(Boolean.TYPE)) {
108: return Boolean.FALSE;
109: }
110: if (type.equals(Byte.TYPE)) {
111: return DEFAULT_BYTE;
112: }
113: if (type.equals(Character.TYPE)) {
114: return DEFAULT_CHAR;
115: }
116: return null;
117: }
118:
119: public boolean isDirty() {
120: return (status & STATUS_DIRTY) > 0;
121: }
122:
123: public void setDirty(boolean value) {
124: if (value) {
125: status |= STATUS_DIRTY;
126: } else {
127: status &= ~STATUS_DIRTY;
128: }
129: }
130:
131: public boolean isTransactional() {
132: return (status & STATUS_TRANSACTIONAL) > 0;
133: }
134:
135: public void setTransactional(boolean value) {
136: if (value) {
137: status |= STATUS_TRANSACTIONAL;
138: } else {
139: status &= ~STATUS_TRANSACTIONAL;
140: }
141: }
142:
143: public boolean isPersistent() {
144: return (status & STATUS_PERSISTENT) > 0;
145: }
146:
147: public void setPersistent(boolean value) {
148: if (value) {
149: status |= STATUS_PERSISTENT;
150: } else {
151: status &= ~STATUS_PERSISTENT;
152: }
153: }
154:
155: public boolean isNew() {
156: return (status & STATUS_NEW) > 0;
157: }
158:
159: public void setNew(boolean value) {
160: if (value) {
161: status |= STATUS_NEW;
162: } else {
163: status &= ~STATUS_NEW;
164: }
165: }
166:
167: public boolean isDeleted() {
168: return (status & STATUS_DELETED) > 0;
169: }
170:
171: public void setDeleted(boolean value) {
172: if (value) {
173: status |= STATUS_DELETED;
174: } else {
175: status &= ~STATUS_DELETED;
176: }
177: }
178:
179: public boolean isHollow() {
180: // Note that this one is different!
181: return (status == STATUS_HOLLOW);
182: }
183:
184: public void setHollow(boolean value) {
185: status = STATUS_HOLLOW;
186: }
187:
188: public void setStatus(byte status) {
189: this .status = status;
190: }
191:
192: public byte getStatus() {
193: return status;
194: }
195:
196: public void setProxy(Object proxy) {
197: this .proxy = proxy;
198: }
199:
200: public Object getProxy() {
201: return proxy;
202: }
203: }
|