001: /*
002: * Copyright 2004 (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: BinaryWidget.java,v 1.1 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import java.util.Arrays;
014:
015: public class BinaryWidget extends Widget {
016: private byte[] fixedLengthBinary;
017: private byte[] normalBinary;
018: private byte[] hugeBinary;
019:
020: public BinaryWidget() {
021: super ();
022: }
023:
024: public byte[] getFixedLengthBinary() {
025: return fixedLengthBinary;
026: }
027:
028: public byte[] getNormalBinary() {
029: return normalBinary;
030: }
031:
032: public byte[] getHugeBinary() {
033: return hugeBinary;
034: }
035:
036: public void setNormalBinary(byte[] normalBinary) {
037: this .normalBinary = normalBinary;
038: }
039:
040: /**
041: * Fills all of the object's fields with random data values. Any non-
042: * primitive fields (with the exception of <code>id</code>) will also be
043: * assigned <code>null</code> on a random basis.
044: */
045:
046: public void fillRandom() {
047: super .fillRandom();
048:
049: fixedLengthBinary = nextNull() ? null : nextBinary(20);
050: normalBinary = nextNull() ? null : nextBinary(r.nextInt(21));
051: hugeBinary = nextNull() ? null : nextBinary(r.nextInt(101));
052: }
053:
054: /**
055: * Indicates whether some other object is "equal to" this one. By comparing
056: * against an original copy of the object, <code>compareTo()</code> can be
057: * used to verify that the object has been written to a database and read
058: * back correctly.
059: *
060: * @param obj the reference object with which to compare
061: *
062: * @return <code>true</code> if this object is equal to the obj argument;
063: * <code>false</code> otherwise.
064: */
065:
066: public boolean compareTo(Object obj) {
067: if (obj == this )
068: return true;
069:
070: if (!(obj instanceof BinaryWidget) || !super .compareTo(obj))
071: return false;
072:
073: BinaryWidget w = (BinaryWidget) obj;
074:
075: if (fixedLengthBinary == null) {
076: if (w.fixedLengthBinary != null)
077: return false;
078: } else if (!Arrays.equals(fixedLengthBinary,
079: w.fixedLengthBinary))
080: return false;
081:
082: if (normalBinary == null) {
083: if (w.normalBinary != null)
084: return false;
085: } else if (!Arrays.equals(normalBinary, w.normalBinary))
086: return false;
087:
088: if (hugeBinary == null) {
089: if (w.hugeBinary != null)
090: return false;
091: } else if (!Arrays.equals(hugeBinary, w.hugeBinary))
092: return false;
093:
094: return true;
095: }
096:
097: /**
098: * Returns a string representation for this object. All of the field
099: * values are included in the string for debugging purposes.
100: *
101: * @return a string representation for this object.
102: */
103:
104: public String toString() {
105: StringBuffer s = new StringBuffer(super .toString());
106:
107: s.append(" fixedLengthBinary = ").append(
108: toHexString(fixedLengthBinary));
109: s.append('\n');
110: s.append(" normalBinary = ").append(toHexString(normalBinary));
111: s.append('\n');
112: s.append(" hugeBinary = ").append(toHexString(hugeBinary));
113: s.append('\n');
114:
115: return s.toString();
116: }
117:
118: /**
119: * Returns a hex string representation of a given byte array.
120: *
121: * @param ba the byte array to convert to string.
122: *
123: * @return a hex string representation of the given byte array.
124: */
125:
126: private static String toHexString(byte[] ba) {
127: if (ba == null)
128: return "null";
129:
130: StringBuffer s = new StringBuffer();
131:
132: for (int i = 0; i < ba.length; ++i)
133: s.append(Integer.toHexString((int) ba[i] & 0xff));
134:
135: return s.toString();
136: }
137: }
|