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: DecimalWidget.java,v 1.4 2003/02/23 08:39:12 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import com.triactive.jdo.DatabaseProperties;
014: import java.math.BigDecimal;
015: import java.math.BigInteger;
016:
017: public class DecimalWidget extends Widget {
018: private BigInteger bigIntegerField;
019: private BigDecimal bigDecimalField;
020:
021: public DecimalWidget() {
022: super ();
023: }
024:
025: public BigInteger getBigIntegerField() {
026: return bigIntegerField;
027: }
028:
029: public BigDecimal getBigDecimalField() {
030: return bigDecimalField;
031: }
032:
033: /**
034: * Fills all of the object's fields with random data values. Any non-
035: * primitive fields (with the exception of <code>id</code>) will also be
036: * assigned <code>null</code> on a random basis.
037: */
038:
039: public void fillRandom() {
040: super .fillRandom();
041:
042: /*
043: * The number of bits specified here must transform to a max number of
044: * decimal digits that will accomodate the most limited known DBMS,
045: * which at present is Firebird (18 digits). 2^59 is the largest power
046: * of two that fits in 18 decimal digits.
047: */
048: int numRandBits = 59;
049:
050: /*
051: * Go easy on the toy database. As of 3.23.55 MySQL was known to fail
052: * (return values that are close but not equal) with values >= 2^53.
053: */
054: if (DatabaseProperties.dbURL.startsWith("jdbc:mysql"))
055: numRandBits = 52;
056:
057: bigIntegerField = nextNull() ? null : new BigInteger(
058: numRandBits, r);
059: bigDecimalField = nextNull() ? null : new BigDecimal(
060: new BigInteger(numRandBits, r), 2);
061: }
062:
063: /**
064: * Indicates whether some other object is "equal to" this one. By comparing
065: * against an original copy of the object, <code>compareTo()</code> can be
066: * used to verify that the object has been written to a database and read
067: * back correctly.
068: *
069: * @param obj the reference object with which to compare
070: *
071: * @return <code>true</code> if this object is equal to the obj argument;
072: * <code>false</code> otherwise.
073: */
074:
075: public boolean compareTo(Object obj) {
076: if (obj == this )
077: return true;
078:
079: if (!(obj instanceof DecimalWidget) || !super .compareTo(obj))
080: return false;
081:
082: DecimalWidget w = (DecimalWidget) obj;
083:
084: if (bigIntegerField == null) {
085: if (w.bigIntegerField != null)
086: return false;
087: } else if (!bigIntegerField.equals(w.bigIntegerField))
088: return false;
089:
090: if (bigDecimalField == null) {
091: if (w.bigDecimalField != null)
092: return false;
093: } else if (bigDecimalField.compareTo(w.bigDecimalField) != 0)
094: return false;
095:
096: return true;
097: }
098:
099: /**
100: * Returns a string representation for this object. All of the field
101: * values are included in the string for debugging purposes.
102: *
103: * @return a string representation for this object.
104: */
105:
106: public String toString() {
107: StringBuffer s = new StringBuffer(super .toString());
108:
109: s.append(" bigIntegerField = ").append(bigIntegerField);
110: s.append('\n');
111: s.append(" bigDecimalField = ").append(bigDecimalField);
112: s.append('\n');
113:
114: return s.toString();
115: }
116: }
|