KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > DecimalWidget


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DecimalWidget.java,v 1.4 2003/02/23 08:39:12 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13 import com.triactive.jdo.DatabaseProperties;
14 import java.math.BigDecimal JavaDoc;
15 import java.math.BigInteger JavaDoc;
16
17
18 public class DecimalWidget extends Widget
19 {
20     private BigInteger JavaDoc bigIntegerField;
21     private BigDecimal JavaDoc bigDecimalField;
22
23
24     public DecimalWidget()
25     {
26         super();
27     }
28
29
30     public BigInteger JavaDoc getBigIntegerField()
31     {
32         return bigIntegerField;
33     }
34
35
36     public BigDecimal JavaDoc getBigDecimalField()
37     {
38         return bigDecimalField;
39     }
40
41
42     /**
43      * Fills all of the object's fields with random data values. Any non-
44      * primitive fields (with the exception of <code>id</code>) will also be
45      * assigned <code>null</code> on a random basis.
46      */

47
48     public void fillRandom()
49     {
50         super.fillRandom();
51
52         /*
53          * The number of bits specified here must transform to a max number of
54          * decimal digits that will accomodate the most limited known DBMS,
55          * which at present is Firebird (18 digits). 2^59 is the largest power
56          * of two that fits in 18 decimal digits.
57          */

58         int numRandBits = 59;
59
60         /*
61          * Go easy on the toy database. As of 3.23.55 MySQL was known to fail
62          * (return values that are close but not equal) with values >= 2^53.
63          */

64         if (DatabaseProperties.dbURL.startsWith("jdbc:mysql"))
65             numRandBits = 52;
66
67         bigIntegerField = nextNull() ? null : new BigInteger JavaDoc(numRandBits, r);
68         bigDecimalField = nextNull() ? null : new BigDecimal JavaDoc(new BigInteger JavaDoc(numRandBits, r), 2);
69     }
70
71
72     /**
73      * Indicates whether some other object is "equal to" this one. By comparing
74      * against an original copy of the object, <code>compareTo()</code> can be
75      * used to verify that the object has been written to a database and read
76      * back correctly.
77      *
78      * @param obj the reference object with which to compare
79      *
80      * @return <code>true</code> if this object is equal to the obj argument;
81      * <code>false</code> otherwise.
82      */

83
84     public boolean compareTo(Object JavaDoc obj)
85     {
86         if (obj == this)
87             return true;
88
89         if (!(obj instanceof DecimalWidget) || !super.compareTo(obj))
90             return false;
91
92         DecimalWidget w = (DecimalWidget)obj;
93
94         if (bigIntegerField == null) { if (w.bigIntegerField != null) return false; }
95         else if (!bigIntegerField.equals(w.bigIntegerField)) return false;
96
97         if (bigDecimalField == null) { if (w.bigDecimalField != null) return false; }
98         else if (bigDecimalField.compareTo(w.bigDecimalField) != 0) return false;
99
100         return true;
101     }
102
103
104     /**
105      * Returns a string representation for this object. All of the field
106      * values are included in the string for debugging purposes.
107      *
108      * @return a string representation for this object.
109      */

110
111     public String JavaDoc toString()
112     {
113         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
114
115         s.append(" bigIntegerField = ").append(bigIntegerField);
116         s.append('\n');
117         s.append(" bigDecimalField = ").append(bigDecimalField);
118         s.append('\n');
119
120         return s.toString();
121     }
122 }
123
Popular Tags