/*
* Copyright 2002 (C) TJDO.
* All rights reserved.
*
* This software is distributed under the terms of the TJDO License version 1.0.
* See the terms of the TJDO License in the documentation provided with this software.
*
* $Id: DecimalWidget.java,v 1.4 2003/02/23 08:39:12 jackknifebarber Exp $
*/
package com.triactive.jdo.test;
import com.triactive.jdo.DatabaseProperties;
import java.math.BigDecimal;
import java.math.BigInteger;
public class DecimalWidget extends Widget
{
private BigInteger bigIntegerField;
private BigDecimal bigDecimalField;
public DecimalWidget()
{
super();
}
public BigInteger getBigIntegerField()
{
return bigIntegerField;
}
public BigDecimal getBigDecimalField()
{
return bigDecimalField;
}
/**
* Fills all of the object's fields with random data values. Any non-
* primitive fields (with the exception of <code>id</code>) will also be
* assigned <code>null</code> on a random basis.
*/
public void fillRandom()
{
super.fillRandom();
/*
* The number of bits specified here must transform to a max number of
* decimal digits that will accomodate the most limited known DBMS,
* which at present is Firebird (18 digits). 2^59 is the largest power
* of two that fits in 18 decimal digits.
*/
int numRandBits = 59;
/*
* Go easy on the toy database. As of 3.23.55 MySQL was known to fail
* (return values that are close but not equal) with values >= 2^53.
*/
if (DatabaseProperties.dbURL.startsWith("jdbc:mysql"))
numRandBits = 52;
bigIntegerField = nextNull() ? null : new BigInteger(numRandBits, r);
bigDecimalField = nextNull() ? null : new BigDecimal(new BigInteger(numRandBits, r), 2);
}
/**
* Indicates whether some other object is "equal to" this one. By comparing
* against an original copy of the object, <code>compareTo()</code> can be
* used to verify that the object has been written to a database and read
* back correctly.
*
* @param obj the reference object with which to compare
*
* @return <code>true</code> if this object is equal to the obj argument;
* <code>false</code> otherwise.
*/
public boolean compareTo(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof DecimalWidget) || !super.compareTo(obj))
return false;
DecimalWidget w = (DecimalWidget)obj;
if (bigIntegerField == null) { if (w.bigIntegerField != null) return false; }
else if (!bigIntegerField.equals(w.bigIntegerField)) return false;
if (bigDecimalField == null) { if (w.bigDecimalField != null) return false; }
else if (bigDecimalField.compareTo(w.bigDecimalField) != 0) return false;
return true;
}
/**
* Returns a string representation for this object. All of the field
* values are included in the string for debugging purposes.
*
* @return a string representation for this object.
*/
public String toString()
{
StringBuffer s = new StringBuffer(super.toString());
s.append(" bigIntegerField = ").append(bigIntegerField);
s.append('\n');
s.append(" bigDecimalField = ").append(bigDecimalField);
s.append('\n');
return s.toString();
}
}
|