001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: Widget.java,v 1.4 2003/03/17 07:02:53 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import javax.jdo.InstanceCallbacks;
014:
015: public class Widget extends TestObject implements InstanceCallbacks {
016: private boolean booleanField;
017: private Boolean booleanObjField;
018: private byte byteField;
019: private Byte byteObjField;
020: private char charField;
021: private Character charObjField;
022: private short shortField;
023: private Short shortObjField;
024: private int intField;
025: private Integer intObjField;
026: private long longField;
027: private Long longObjField;
028:
029: public Widget() {
030: }
031:
032: public boolean getBooleanField() {
033: return booleanField;
034: }
035:
036: public Boolean getBooleanObjField() {
037: return booleanObjField;
038: }
039:
040: public byte getByteField() {
041: return byteField;
042: }
043:
044: public Byte getByteObjField() {
045: return byteObjField;
046: }
047:
048: public char getCharField() {
049: return charField;
050: }
051:
052: public Character getCharObjField() {
053: return charObjField;
054: }
055:
056: public short getShortField() {
057: return shortField;
058: }
059:
060: public Short getShortObjField() {
061: return shortObjField;
062: }
063:
064: public int getIntField() {
065: return intField;
066: }
067:
068: public Integer getIntObjField() {
069: return intObjField;
070: }
071:
072: public long getLongField() {
073: return longField;
074: }
075:
076: public Long getLongObjField() {
077: return longObjField;
078: }
079:
080: public void setByteField(byte byteField) {
081: this .byteField = byteField;
082: }
083:
084: public void setShortField(short shortField) {
085: this .shortField = shortField;
086: }
087:
088: public void setIntField(int intField) {
089: this .intField = intField;
090: }
091:
092: public byte setByteFieldRandom() {
093: byte b = nextByte();
094:
095: this .byteField = b;
096:
097: return b;
098: }
099:
100: /**
101: * Fills all of the object's fields with random data values. Any non-
102: * primitive fields (with the exception of <code>id</code>) will also be
103: * assigned <code>null</code> on a random basis.
104: */
105:
106: public void fillRandom() {
107: booleanField = r.nextBoolean();
108: booleanObjField = nextNull() ? null : new Boolean(r
109: .nextBoolean());
110: byteField = nextByte();
111: byteObjField = nextNull() ? null : new Byte(nextByte());
112: charField = nextCharacter();
113: charObjField = nextNull() ? null : new Character(
114: nextCharacter());
115: shortField = (short) (r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE);
116: shortObjField = nextNull() ? null : new Short((short) (r
117: .nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE));
118: intField = r.nextInt();
119: intObjField = nextNull() ? null : new Integer(r.nextInt());
120: longField = r.nextLong();
121: longObjField = nextNull() ? null : new Long(r.nextLong());
122: }
123:
124: /**
125: * Indicates whether some other object is "equal to" this one. By comparing
126: * against an original copy of the object, <code>compareTo()</code> can be
127: * used to verify that the object has been written to a database and read
128: * back correctly.
129: *
130: * @param obj the reference object with which to compare
131: *
132: * @return <code>true</code> if this object is equal to the obj argument;
133: * <code>false</code> otherwise.
134: */
135:
136: public boolean compareTo(Object obj) {
137: if (obj == this )
138: return true;
139:
140: if (!(obj instanceof Widget))
141: return false;
142:
143: Widget w = (Widget) obj;
144:
145: if (booleanObjField == null) {
146: if (w.booleanObjField != null)
147: return false;
148: } else if (!booleanObjField.equals(w.booleanObjField))
149: return false;
150:
151: if (byteObjField == null) {
152: if (w.byteObjField != null)
153: return false;
154: } else if (!byteObjField.equals(w.byteObjField))
155: return false;
156:
157: if (charObjField == null) {
158: if (w.charObjField != null)
159: return false;
160: } else if (!charObjField.equals(w.charObjField))
161: return false;
162:
163: if (shortObjField == null) {
164: if (w.shortObjField != null)
165: return false;
166: } else if (!shortObjField.equals(w.shortObjField))
167: return false;
168:
169: if (intObjField == null) {
170: if (w.intObjField != null)
171: return false;
172: } else if (!intObjField.equals(w.intObjField))
173: return false;
174:
175: if (longObjField == null) {
176: if (w.longObjField != null)
177: return false;
178: } else if (!longObjField.equals(w.longObjField))
179: return false;
180:
181: return booleanField == w.booleanField
182: && byteField == w.byteField && charField == w.charField
183: && shortField == w.shortField && intField == w.intField
184: && longField == w.longField;
185: }
186:
187: /**
188: * Returns a string representation for this object. All of the field
189: * values are included in the string for debugging purposes.
190: *
191: * @return a string representation for this object.
192: */
193:
194: public String toString() {
195: StringBuffer s = new StringBuffer(super .toString());
196:
197: s.append(" booleanField = ").append(booleanField);
198: s.append('\n');
199: s.append(" booleanObjField = ").append(booleanObjField);
200: s.append('\n');
201: s.append(" byteField = ").append(byteField);
202: s.append('\n');
203: s.append(" byteObjField = ").append(byteObjField);
204: s.append('\n');
205: s.append(" charField = ").append(charField);
206: s.append('\n');
207: s.append(" charObjField = ").append(charObjField);
208: s.append('\n');
209: s.append(" shortField = ").append(shortField);
210: s.append('\n');
211: s.append(" shortObjField = ").append(shortObjField);
212: s.append('\n');
213: s.append(" intField = ").append(intField);
214: s.append('\n');
215: s.append(" intObjField = ").append(intObjField);
216: s.append('\n');
217: s.append(" longField = ").append(longField);
218: s.append('\n');
219: s.append(" longObjField = ").append(longObjField);
220: s.append('\n');
221:
222: return s.toString();
223: }
224:
225: public void jdoPostLoad() {
226: }
227:
228: public void jdoPreClear() {
229: }
230:
231: public void jdoPreDelete() {
232: }
233:
234: public void jdoPreStore() {
235: /*
236: * This is here for the benefit of one particular test in
237: * BasicStorageTest.updateObjects(), which tests the updating of
238: * a default-fetch-group field when the default fetch group has not
239: * been loaded. All it needs to do is touch a couple of DFG fields.
240: */
241: if (booleanField && intField < 0)
242: nextCharacter();
243: }
244: }
|