KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > model > test > widgets > FloatWidget


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: FloatWidget.java,v 1.5 2004/03/22 04:58:12 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.model.test.widgets;
12
13 import com.triactive.jdo.model.*;
14 import java.math.BigDecimal JavaDoc;
15 import java.math.BigInteger JavaDoc;
16 import java.util.Random JavaDoc;
17 import junit.framework.Assert;
18
19
20 /**
21  * An object used to test persistence of floating-point and decimal fields,
22  * in addition to those in {@link BinaryWidget}.
23  *
24  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
25  * @version $Revision: 1.5 $
26  */

27
28 public class FloatWidget extends BinaryWidget
29 {
30     private float floatField;
31     private double doubleField;
32     private Float JavaDoc floatObjField;
33     private Double JavaDoc doubleObjField;
34     private BigDecimal JavaDoc bigDecimalField;
35     private BigInteger JavaDoc bigIntegerField;
36
37
38     /**
39      * Constructs an empty test object.
40      */

41
42     public FloatWidget()
43     {
44         super();
45     }
46
47     private static Random JavaDoc r = new Random JavaDoc(0);
48
49
50     /**
51      * Fills all of the object's fields with random data values. Any non-
52      * primitive fields will also be assigned <code>null</code> on a random
53      * basis.
54      */

55
56     public void fillRandom()
57     {
58         super.fillRandom();
59
60         floatField = r.nextFloat();
61         doubleField = r.nextDouble();
62         floatObjField = r.nextBoolean() ? null : new Float JavaDoc(r.nextFloat());
63         doubleObjField = r.nextBoolean() ? null : new Double JavaDoc(r.nextDouble());
64         bigDecimalField = r.nextBoolean() ? null : BigDecimal.valueOf((long)r.nextInt(), 2);
65         bigIntegerField = r.nextBoolean() ? null : BigInteger.valueOf((long)r.nextInt());
66     }
67
68
69     /**
70      * Indicates whether some other object is "equal to" this one. By comparing
71      * against an original copy of the object, <code>equals()</code> can be
72      * used to verify that the object has been written to a database and read
73      * back correctly.
74      *
75      * @param obj the reference object with which to compare
76      *
77      * @return <code>true</code> if this object is equal to the obj argument;
78      * <code>false</code> otherwise.
79      */

80
81     public boolean equals(Object JavaDoc obj)
82     {
83         if (this == obj)
84             return true;
85
86         if (!(obj instanceof FloatWidget) || !super.equals(obj))
87             return false;
88
89         FloatWidget w = (FloatWidget)obj;
90
91         /*
92          * Expecting that we may lose some precision (or experience rounding)
93          * as a floating value is transferred to the database and back, we
94          * define equality as being at least 6 decimal digits of accuracy for
95          * float types and 15 decimal digits for double types.
96          *
97          * Note that the comparisons used below only work if the values in
98          * question are in the range 0.0 <= x < 1.0.
99          */

100
101         if (Math.abs(floatField - w.floatField) >= 1e-6) return false;
102         if (Math.abs(doubleField - w.doubleField) >= 1e-15) return false;
103
104         if (floatObjField == null) { if (w.floatObjField != null) return false; }
105         else if (Math.abs(floatObjField.floatValue() - w.floatObjField.floatValue()) >= 1e-6) return false;
106
107         if (doubleObjField == null) { if (w.doubleObjField != null) return false; }
108         else if (Math.abs(doubleObjField.doubleValue() - w.doubleObjField.doubleValue()) >= 1e-15) return false;
109
110         /*
111          * BigDecimals and BigIntegers can be compared normally -- they're
112          * required to be exact at all times.
113          */

114
115         if (bigDecimalField == null) { if (w.bigDecimalField != null) return false; }
116         else if (!bigDecimalField.equals(w.bigDecimalField)) return false;
117
118         if (bigIntegerField == null) { if (w.bigIntegerField != null) return false; }
119         else if (!bigIntegerField.equals(w.bigIntegerField)) return false;
120
121         return true;
122     }
123
124
125     /**
126      * Returns a hash code value for this object.
127      *
128      * @return a hash code value for this object.
129      */

130
131     public int hashCode()
132     {
133         /*
134          * Note that, like equals() above, the technique used below for
135          * computing a hash code on the floating fields only works if the
136          * values in question are in the range 0.0 <= x < 1.0.
137          */

138
139         return (int)(floatField * 1e6)
140              ^ (int)(doubleField * 1e9)
141              ^ (floatObjField == null ? 0 : (int)(floatObjField.floatValue() * 1e6))
142              ^ (doubleObjField == null ? 0 : (int)(doubleObjField.doubleValue() * 1e9))
143              ^ (bigDecimalField == null ? 0 : bigDecimalField.hashCode())
144              ^ (bigIntegerField == null ? 0 : bigIntegerField.hashCode());
145     }
146
147
148     /**
149      * Returns a string representation for this object. All of the field
150      * values are included in the string for debugging purposes.
151      *
152      * @return a string representation for this object.
153      */

154
155     public String JavaDoc toString()
156     {
157         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
158
159         s.append(" floatField = ").append(floatField);
160         s.append("\n doubleField = ").append(doubleField);
161         s.append("\n bigDecimalField = ").append(bigDecimalField);
162         s.append("\n bigIntegerField = ").append(bigIntegerField);
163         s.append('\n');
164
165         return s.toString();
166     }
167
168
169     /**
170      * Asserts that the given metadata is correct for an object of this class.
171      *
172      * @param cmd the class metadata to be tested.
173      * @param test the test to be used to call assert methods.
174      */

175
176     public static void assertValidMetaData(ClassMetaData cmd, Assert test)
177     {
178         test.assertNotNull("Metadata", cmd);
179         test.assertEquals(FloatWidget.class, cmd.getPCClass());
180         test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName());
181         test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("package.jdo") >= 0);
182         test.assertEquals("Superclass", BinaryWidget.class, cmd.getPCSuperclass());
183         test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType());
184         test.assertNull("Identity class", cmd.getIdentityClass());
185
186         String JavaDoc[] sortedFieldNames = new String JavaDoc[]
187             {
188                 "bigDecimalField",
189                 "bigIntegerField",
190                 "doubleField",
191                 "doubleObjField",
192                 "floatField",
193                 "floatObjField"
194             };
195
196         int[] nullValueHandlings = new int[]
197             {
198                 FieldMetaData.NULL_VALUE_NONE,
199                 FieldMetaData.NULL_VALUE_NONE,
200                 FieldMetaData.NULL_VALUE_EXCEPTION,
201                 FieldMetaData.NULL_VALUE_NONE,
202                 FieldMetaData.NULL_VALUE_EXCEPTION,
203                 FieldMetaData.NULL_VALUE_NONE
204             };
205
206         test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount());
207
208         for (int i = 0; i < sortedFieldNames.length; ++i)
209         {
210             FieldMetaData fmd = cmd.getFieldRelative(i);
211             String JavaDoc s = sortedFieldNames[i];
212
213             test.assertEquals(s, fmd.getName());
214             test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier());
215             test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart());
216             test.assertEquals(s + " null value handling", nullValueHandlings[i], fmd.getNullValueHandling());
217             test.assertEquals(s + " default fetch group", true, fmd.isInDefaultFetchGroup());
218             test.assertNull(s + " array metadata", fmd.getArrayMetaData());
219             test.assertNull(s + " collection metadata", fmd.getCollectionMetaData());
220             test.assertNull(s + " map metadata", fmd.getMapMetaData());
221         }
222     }
223 }
224
Popular Tags