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