KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003 (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.4 2003/02/25 06:55:16 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test;
12
13
14 public class FloatWidget extends Widget
15 {
16     /**
17      * The minimum accuracy required of the storage and retrieval of float
18      * values.
19      * 1e-6 indicates that values must retain at least six significant digits
20      * of accuracy.
21      */

22     public static final float FLOAT_EPSILON = 1e-5f;
23
24     /**
25      * The minimum accuracy required of the storage and retrieval of double
26      * values.
27      * 1e-14 indicates that values must retain at least 14 significant digits
28      * of accuracy.
29      */

30     public static final double DOUBLE_EPSILON = 1e-14d;
31
32     /**
33      * The minimum float value we will try to store in the DB.
34      * This is set to roughly half of the Java limit.
35      */

36     public static final float MIN_FLOAT_VALUE = 1e-22f;
37
38     /**
39      * The maximum float value we will try to store in the DB.
40      * This is set to roughly half of the Java limit.
41      */

42     public static final float MAX_FLOAT_VALUE = 1e17f;
43
44     /**
45      * The minimum double value we will try to store in the DB.
46      * This is set to roughly half of the Java limit.
47      */

48     public static final double MIN_DOUBLE_VALUE = 1e-62d;
49
50     /**
51      * The maximum double value we will try to store in the DB.
52      * This is set to roughly half of the Java limit.
53      */

54     public static final double MAX_DOUBLE_VALUE = 1e62d;
55
56
57     private float floatField;
58     private Float JavaDoc floatObjField;
59     private double doubleField;
60     private Double JavaDoc doubleObjField;
61
62
63     public FloatWidget()
64     {
65         super();
66     }
67
68
69     public float getFloatField()
70     {
71         return floatField;
72     }
73
74
75     public Float JavaDoc getFloatObjField()
76     {
77         return floatObjField;
78     }
79
80
81     public double getDoubleField()
82     {
83         return doubleField;
84     }
85
86
87     public Double JavaDoc getDoubleObjField()
88     {
89         return doubleObjField;
90     }
91
92
93     private float nextFloat()
94     {
95         float f;
96
97         do
98         {
99             f = Float.intBitsToFloat(r.nextInt());
100         } while (Float.isNaN(f) || Float.isInfinite(f) || f < MIN_FLOAT_VALUE || f > MAX_FLOAT_VALUE);
101
102         return f;
103     }
104
105
106     private double nextDouble()
107     {
108         double d;
109
110         do
111         {
112             d = Double.longBitsToDouble(r.nextLong());
113         } while (Double.isNaN(d) || Double.isInfinite(d) || d < MIN_DOUBLE_VALUE || d > MAX_DOUBLE_VALUE);
114
115         return d;
116     }
117
118
119     /**
120      * Fills all of the object's fields with random data values. Any non-
121      * primitive fields (with the exception of <code>id</code>) will also be
122      * assigned <code>null</code> on a random basis.
123      */

124
125     public void fillRandom()
126     {
127         super.fillRandom();
128
129         floatField = nextFloat();
130         floatObjField = nextNull() ? null : new Float JavaDoc(nextFloat());
131         doubleField = nextDouble();
132         doubleObjField = nextNull() ? null : new Double JavaDoc(nextDouble());
133     }
134
135
136     /**
137      * Indicates whether some other object is "equal to" this one. By comparing
138      * against an original copy of the object, <code>compareTo()</code> can be
139      * used to verify that the object has been written to a database and read
140      * back correctly.
141      *
142      * @param obj the reference object with which to compare
143      *
144      * @return <code>true</code> if this object is equal to the obj argument;
145      * <code>false</code> otherwise.
146      */

147
148     public boolean compareTo(Object JavaDoc obj)
149     {
150         if (obj == this)
151             return true;
152
153         if (!(obj instanceof FloatWidget) || !super.compareTo(obj))
154             return false;
155
156         FloatWidget w = (FloatWidget)obj;
157
158         if (floatObjField == null) { if (w.floatObjField != null) return false; }
159         else if (!approximates(floatObjField.floatValue(), w.floatObjField.floatValue())) return false;
160
161         if (doubleObjField == null) { if (w.doubleObjField != null) return false; }
162         else if (!approximates(doubleObjField.doubleValue(), w.doubleObjField.doubleValue())) return false;
163
164         return approximates(floatField, w.floatField)
165             && approximates(doubleField, w.doubleField);
166     }
167
168
169     public static boolean approximates(float x, float y)
170     {
171         return Math.abs(x - y) / Math.max(Math.max(Math.abs(x), Math.abs(y)), Float.MIN_VALUE) < FLOAT_EPSILON;
172     }
173
174
175     public static boolean approximates(double x, double y)
176     {
177         return Math.abs(x - y) / Math.max(Math.max(Math.abs(x), Math.abs(y)), Double.MIN_VALUE) < DOUBLE_EPSILON;
178     }
179
180
181     /**
182      * Returns a string representation for this object. All of the field
183      * values are included in the string for debugging purposes.
184      *
185      * @return a string representation for this object.
186      */

187
188     public String JavaDoc toString()
189     {
190         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
191
192         s.append(" floatField = ").append(floatField);
193         s.append('\n');
194         s.append(" floatObjField = ").append(floatObjField);
195         s.append('\n');
196         s.append(" doubleField = ").append(doubleField);
197         s.append('\n');
198         s.append(" doubleObjField = ").append(doubleObjField);
199         s.append('\n');
200
201         return s.toString();
202     }
203 }
204
Popular Tags