View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.common.math;
25  
26  import java.math.BigDecimal;
27  import java.util.Comparator;
28  import java.util.Random;
29  import net.jcip.annotations.Immutable;
30  
31  /**
32   * The {@link MathOperations math operations} for float numbers.
33   */
34  @Immutable
35  public class FloatOperations implements MathOperations<Float>, Comparator<Float> {
36  
37      public Class<Float> getOperandClass() {
38          return Float.class;
39      }
40  
41      public Float add( Float value1,
42                        Float value2 ) {
43          if (value1 == null) return value2 != null ? value2 : createZeroValue();
44          if (value2 == null) return value1;
45          return (value1 + value2);
46      }
47  
48      public Float subtract( Float value1,
49                             Float value2 ) {
50          if (value1 == null) return negate(value2);
51          if (value2 == null) return value1;
52          return (value1 - value2);
53      }
54  
55      public Float multiply( Float value1,
56                             Float value2 ) {
57          if (value1 == null || value2 == null) return createZeroValue();
58          return (value1 * value2);
59      }
60  
61      public double divide( Float value1,
62                            Float value2 ) {
63          if (value1 == null || value2 == null) throw new IllegalArgumentException();
64          return value1 / value2;
65      }
66  
67      public Float negate( Float value ) {
68          if (value == null) return createZeroValue();
69          return (value * -1);
70      }
71  
72      public Float increment( Float value ) {
73          if (value == null) return createZeroValue();
74          return (value + 1);
75      }
76  
77      public Float maximum( Float value1,
78                            Float value2 ) {
79          if (value1 == null) return value2;
80          if (value2 == null) return value1;
81          return Math.max(value1, value2);
82      }
83  
84      public Float minimum( Float value1,
85                            Float value2 ) {
86          if (value1 == null) return value2;
87          if (value2 == null) return value1;
88          return Math.min(value1, value2);
89      }
90  
91      public int compare( Float value1,
92                          Float value2 ) {
93          if (value1 == null) return value2 != null ? -1 : 0;
94          if (value2 == null) return 1;
95          return value1.compareTo(value2);
96      }
97  
98      public BigDecimal asBigDecimal( Float value ) {
99          return value != null ? new BigDecimal(value) : null;
100     }
101 
102     public Float fromBigDecimal( BigDecimal value ) {
103         return value != null ? value.floatValue() : null;
104     }
105 
106     public Float createZeroValue() {
107         return 0.0f;
108     }
109 
110     public Float create( int value ) {
111         return (float)value;
112     }
113 
114     public Float create( long value ) {
115         return (float)value;
116     }
117 
118     public Float create( double value ) {
119         return (float)value;
120     }
121 
122     public double sqrt( Float value ) {
123         return Math.sqrt(value);
124     }
125 
126     public Comparator<Float> getComparator() {
127         return this;
128     }
129 
130     public Float random( Float minimum,
131                          Float maximum,
132                          Random rng ) {
133         Float difference = subtract(maximum, minimum);
134         return minimum + difference.floatValue() * rng.nextFloat();
135     }
136 
137     public double doubleValue( Float value ) {
138         return value.doubleValue();
139     }
140 
141     public float floatValue( Float value ) {
142         return value.floatValue();
143     }
144 
145     public int intValue( Float value ) {
146         return value.intValue();
147     }
148 
149     public long longValue( Float value ) {
150         return value.longValue();
151     }
152 
153     public short shortValue( Float value ) {
154         return value.shortValue();
155     }
156 
157     public int getExponentInScientificNotation( Float value ) {
158         double v = Math.abs(value);
159         int exp = 0;
160         if (v > 1.0d) {
161             while (v >= 10.0d) {
162                 v /= 10.0d;
163                 ++exp;
164             }
165         } else if (v == 0.0d) {
166         } else if (v < 1.0d) {
167             while (v < 1.0d) {
168                 v *= 10.0d;
169                 --exp;
170             }
171         }
172         return exp;
173     }
174 
175     public Float roundUp( Float value,
176                           int decimalShift ) {
177         if (value == 0) return 0.0f;
178         double shiftedValue = (Math.abs(value.doubleValue()) * Math.pow(10.0d, decimalShift) + 0.5d) * Math.signum(value);
179         double roundedValue = (long)shiftedValue;
180         return (float)(roundedValue * Math.pow(10.0d, -decimalShift));
181     }
182 
183     public Float roundDown( Float value,
184                             int decimalShift ) {
185         if (value == 0) return 0.0f;
186         if (decimalShift > 0) return value;
187         double shiftedValue = (Math.abs(value.doubleValue()) * Math.pow(10.0d, decimalShift)) * Math.signum(value);
188         double roundedValue = (long)shiftedValue;
189         return (float)(roundedValue * Math.pow(10.0d, -decimalShift));
190     }
191 
192     public Float keepSignificantFigures( Float value,
193                                          int numSigFigs ) {
194         int currentExp = getExponentInScientificNotation(value);
195         int decimalShift = (int)Math.signum(currentExp) * (Math.abs(currentExp) + numSigFigs - 1);
196         return roundUp(value, decimalShift);
197     }
198 }