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