1 10 11 package com.triactive.jdo.test; 12 13 14 public class FloatWidget extends Widget 15 { 16 22 public static final float FLOAT_EPSILON = 1e-5f; 23 24 30 public static final double DOUBLE_EPSILON = 1e-14d; 31 32 36 public static final float MIN_FLOAT_VALUE = 1e-22f; 37 38 42 public static final float MAX_FLOAT_VALUE = 1e17f; 43 44 48 public static final double MIN_DOUBLE_VALUE = 1e-62d; 49 50 54 public static final double MAX_DOUBLE_VALUE = 1e62d; 55 56 57 private float floatField; 58 private Float floatObjField; 59 private double doubleField; 60 private Double 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 getFloatObjField() 76 { 77 return floatObjField; 78 } 79 80 81 public double getDoubleField() 82 { 83 return doubleField; 84 } 85 86 87 public Double 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 124 125 public void fillRandom() 126 { 127 super.fillRandom(); 128 129 floatField = nextFloat(); 130 floatObjField = nextNull() ? null : new Float (nextFloat()); 131 doubleField = nextDouble(); 132 doubleObjField = nextNull() ? null : new Double (nextDouble()); 133 } 134 135 136 147 148 public boolean compareTo(Object 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 187 188 public String toString() 189 { 190 StringBuffer s = new StringBuffer (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 |