001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.common.math;
023
024 import java.math.BigDecimal;
025 import java.util.Comparator;
026 import java.util.Random;
027
028 public class DoubleOperations implements MathOperations<Double>, Comparator<Double> {
029
030 public Class<Double> getOperandClass() {
031 return Double.class;
032 }
033
034 public Double add( Double value1, Double value2 ) {
035 if (value1 == null) return value2 != null ? value2 : createZeroValue();
036 if (value2 == null) return value1;
037 return (value1 + value2);
038 }
039
040 public Double subtract( Double value1, Double value2 ) {
041 if (value1 == null) return negate(value2);
042 if (value2 == null) return value1;
043 return (value1 - value2);
044 }
045
046 public Double multiply( Double value1, Double value2 ) {
047 if (value1 == null || value2 == null) return createZeroValue();
048 return (value1 * value2);
049 }
050
051 public double divide( Double value1, Double value2 ) {
052 if (value1 == null || value2 == null) throw new IllegalArgumentException();
053 return value1 / value2;
054 }
055
056 public Double negate( Double value ) {
057 if (value == null) return createZeroValue();
058 return (value * -1);
059 }
060
061 public Double increment( Double value ) {
062 if (value == null) return createZeroValue();
063 return (value + 1);
064 }
065
066 public Double maximum( Double value1, Double value2 ) {
067 if (value1 == null) return value2;
068 if (value2 == null) return value1;
069 return Math.max(value1, value2);
070 }
071
072 public Double minimum( Double value1, Double value2 ) {
073 if (value1 == null) return value2;
074 if (value2 == null) return value1;
075 return Math.min(value1, value2);
076 }
077
078 public int compare( Double value1, Double value2 ) {
079 if (value1 == null) return value2 != null ? -1 : 0;
080 if (value2 == null) return 1;
081 return value1.compareTo(value2);
082 }
083
084 public BigDecimal asBigDecimal( Double value ) {
085 return value != null ? new BigDecimal(value) : null;
086 }
087
088 public Double fromBigDecimal( BigDecimal value ) {
089 return value != null ? value.doubleValue() : null;
090 }
091
092 public Double createZeroValue() {
093 return 0.0d;
094 }
095
096 public Double create( int value ) {
097 return (double)value;
098 }
099
100 public Double create( long value ) {
101 return (double)value;
102 }
103
104 public Double create( double value ) {
105 return value;
106 }
107
108 public double sqrt( Double value ) {
109 return Math.sqrt(value);
110 }
111
112 public Comparator<Double> getComparator() {
113 return this;
114 }
115
116 public Double random( Double minimum, Double maximum, Random rng ) {
117 Double difference = subtract(maximum, minimum);
118 return minimum + difference.doubleValue() * rng.nextDouble();
119 }
120
121 public double doubleValue( Double value ) {
122 return value.doubleValue();
123 }
124
125 public float floatValue( Double value ) {
126 return value.floatValue();
127 }
128
129 public int intValue( Double value ) {
130 return value.intValue();
131 }
132
133 public long longValue( Double value ) {
134 return value.longValue();
135 }
136
137 public short shortValue( Double value ) {
138 return value.shortValue();
139 }
140
141 public int getExponentInScientificNotation( Double value ) {
142 double v = Math.abs(value);
143 int exp = 0;
144 if (v > 1.0d) {
145 while (v >= 10.0d) {
146 v /= 10.0d;
147 ++exp;
148 }
149 } else if (v == 0.0d) {
150 } else if (v < 1.0d) {
151 while (v < 1.0d) {
152 v *= 10.0d;
153 --exp;
154 }
155 }
156 return exp;
157 }
158
159 public Double roundUp( Double value, int decimalShift ) {
160 if (value == 0) return 0.0d;
161 double shiftedValue = (Math.abs(value) * Math.pow(10.0d, decimalShift) + 0.5d) * Math.signum(value);
162 double roundedValue = (int)shiftedValue;
163 return roundedValue * Math.pow(10.0d, -decimalShift);
164 }
165
166 public Double roundDown( Double value, int decimalShift ) {
167 if (value == 0) return 0.0d;
168 double shiftedValue = (Math.abs(value) * Math.pow(10.0d, decimalShift)) * Math.signum(value);
169 double roundedValue = (int)shiftedValue;
170 return roundedValue * Math.pow(10.0d, -decimalShift);
171 }
172
173 public Double keepSignificantFigures( Double value, int numSigFigs ) {
174 if (numSigFigs < 0) return value;
175 if (numSigFigs == 0) return 0.0d;
176 int currentExp = getExponentInScientificNotation(value);
177 int decimalShift = -currentExp + numSigFigs - 1;
178 return roundUp(value, decimalShift);
179 }
180 }