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 integer numbers.
33   */
34  @Immutable
35  public class IntegerOperations implements MathOperations<Integer>, Comparator<Integer> {
36  
37      public Class<Integer> getOperandClass() {
38          return Integer.class;
39      }
40  
41      public Integer add( Integer value1,
42                          Integer value2 ) {
43          if (value1 == null) return value2 != null ? value2 : createZeroValue();
44          if (value2 == null) return value1;
45          return value1 + value2;
46      }
47  
48      public Integer subtract( Integer value1,
49                               Integer value2 ) {
50          if (value1 == null) return negate(value2);
51          if (value2 == null) return value1;
52          return value1 - value2;
53      }
54  
55      public Integer multiply( Integer value1,
56                               Integer value2 ) {
57          if (value1 == null || value2 == null) return createZeroValue();
58          return value1 * value2;
59      }
60  
61      public double divide( Integer value1,
62                            Integer value2 ) {
63          if (value1 == null || value2 == null) throw new IllegalArgumentException();
64          return value1 / value2;
65      }
66  
67      public Integer negate( Integer value ) {
68          if (value == null) return createZeroValue();
69          return value * -1;
70      }
71  
72      public Integer increment( Integer value ) {
73          if (value == null) return createZeroValue();
74          return value + 1;
75      }
76  
77      public Integer maximum( Integer value1,
78                              Integer value2 ) {
79          if (value1 == null) return value2;
80          if (value2 == null) return value1;
81          return Math.max(value1, value2);
82      }
83  
84      public Integer minimum( Integer value1,
85                              Integer 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( Integer value1,
92                          Integer 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( Integer value ) {
99          return value != null ? new BigDecimal(value) : null;
100     }
101 
102     public Integer fromBigDecimal( BigDecimal value ) {
103         return value != null ? value.intValue() : null;
104     }
105 
106     public Integer createZeroValue() {
107         return 0;
108     }
109 
110     public Integer create( int value ) {
111         return value;
112     }
113 
114     public Integer create( long value ) {
115         return (int)value;
116     }
117 
118     public Integer create( double value ) {
119         return (int)value;
120     }
121 
122     public double sqrt( Integer value ) {
123         return Math.sqrt(value);
124     }
125 
126     public Comparator<Integer> getComparator() {
127         return this;
128     }
129 
130     public Integer random( Integer minimum,
131                            Integer maximum,
132                            Random rng ) {
133         Integer difference = subtract(maximum, minimum);
134         return minimum + rng.nextInt(difference);
135     }
136 
137     public double doubleValue( Integer value ) {
138         return value.doubleValue();
139     }
140 
141     public float floatValue( Integer value ) {
142         return value.floatValue();
143     }
144 
145     public int intValue( Integer value ) {
146         return value.intValue();
147     }
148 
149     public long longValue( Integer value ) {
150         return value.longValue();
151     }
152 
153     public short shortValue( Integer value ) {
154         return value.shortValue();
155     }
156 
157     public int getExponentInScientificNotation( Integer value ) {
158         int v = Math.abs(value);
159         int exp = 0;
160         if (v > 1) {
161             while (v >= 10) {
162                 v /= 10;
163                 ++exp;
164             }
165         }
166         return exp;
167     }
168 
169     public Integer roundUp( Integer value,
170                             int decimalShift ) {
171         if (value == 0) return 0;
172         if (decimalShift >= 0) return value;
173         int shiftedValueP5 = Math.abs(value);
174         for (int i = 0; i != (-decimalShift - 1); ++i)
175             shiftedValueP5 /= 10;
176         shiftedValueP5 += 5l;
177         int shiftedValue = shiftedValueP5 / 10;
178         if (shiftedValue * 10l - shiftedValueP5 >= 5) ++shiftedValue;
179         shiftedValue *= Long.signum(value);
180         for (int i = 0; i != -decimalShift; ++i)
181             shiftedValue *= 10;
182         return shiftedValue;
183     }
184 
185     public Integer roundDown( Integer value,
186                               int decimalShift ) {
187         if (value == 0) return 0;
188         if (decimalShift >= 0) return value;
189         int shiftedValue = Math.abs(value);
190         for (int i = 0; i != -decimalShift; ++i)
191             shiftedValue /= 10;
192         shiftedValue *= Long.signum(value);
193         for (int i = 0; i != -decimalShift; ++i)
194             shiftedValue *= 10;
195         return shiftedValue;
196     }
197 
198     public Integer keepSignificantFigures( Integer value,
199                                            int numSigFigs ) {
200         if (numSigFigs < 0) return value;
201         if (numSigFigs == 0) return 0;
202         int currentExp = getExponentInScientificNotation(value);
203         int decimalShift = -currentExp + numSigFigs - 1;
204         return roundUp(value, decimalShift);
205     }
206 }