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 ShortOperations implements MathOperations<Short>, Comparator<Short> { 029 030 public Class<Short> getOperandClass() { 031 return Short.class; 032 } 033 034 public Short add( Short value1, Short value2 ) { 035 if (value1 == null) return value2 != null ? value2 : createZeroValue(); 036 if (value2 == null) return value1; 037 return (short)(value1 + value2); 038 } 039 040 public Short subtract( Short value1, Short value2 ) { 041 if (value1 == null) return negate(value2); 042 if (value2 == null) return value1; 043 return (short)(value1 - value2); 044 } 045 046 public Short multiply( Short value1, Short value2 ) { 047 if (value1 == null || value2 == null) return createZeroValue(); 048 return (short)(value1 * value2); 049 } 050 051 public double divide( Short value1, Short value2 ) { 052 if (value1 == null || value2 == null) throw new IllegalArgumentException(); 053 return value1 / value2; 054 } 055 056 public Short negate( Short value ) { 057 if (value == null) return createZeroValue(); 058 return (short)(value * -1); 059 } 060 061 public Short increment( Short value ) { 062 if (value == null) return createZeroValue(); 063 return (short)(value + 1); 064 } 065 066 public Short maximum( Short value1, Short value2 ) { 067 if (value1 == null) return value2; 068 if (value2 == null) return value1; 069 return (short)Math.max(value1, value2); 070 } 071 072 public Short minimum( Short value1, Short value2 ) { 073 if (value1 == null) return value2; 074 if (value2 == null) return value1; 075 return (short)Math.min(value1, value2); 076 } 077 078 public int compare( Short value1, Short 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( Short value ) { 085 return value != null ? new BigDecimal(value) : null; 086 } 087 088 public Short fromBigDecimal( BigDecimal value ) { 089 return value != null ? value.shortValue() : null; 090 } 091 092 public Short createZeroValue() { 093 return 0; 094 } 095 096 public Short create( int value ) { 097 return (short)value; 098 } 099 100 public Short create( long value ) { 101 return (short)value; 102 } 103 104 public Short create( double value ) { 105 return (short)value; 106 } 107 108 public double sqrt( Short value ) { 109 return Math.sqrt(value); 110 } 111 112 public Comparator<Short> getComparator() { 113 return this; 114 } 115 116 public Short random( Short minimum, Short maximum, Random rng ) { 117 Short difference = subtract(maximum, minimum); 118 int increment = rng.nextInt(difference.intValue()); 119 return new Integer(minimum + increment).shortValue(); 120 } 121 122 public double doubleValue( Short value ) { 123 return value.doubleValue(); 124 } 125 126 public float floatValue( Short value ) { 127 return value.floatValue(); 128 } 129 130 public int intValue( Short value ) { 131 return value.intValue(); 132 } 133 134 public long longValue( Short value ) { 135 return value.longValue(); 136 } 137 138 public short shortValue( Short value ) { 139 return value.shortValue(); 140 } 141 142 public int getExponentInScientificNotation( Short value ) { 143 int v = Math.abs(value); 144 int exp = 0; 145 if (v > 1) { 146 while (v >= 10) { 147 v /= 10; 148 ++exp; 149 } 150 } else if (v < 1) { 151 while (v < 1) { 152 v *= 10; 153 --exp; 154 } 155 } 156 return exp; 157 } 158 159 public Short roundUp( Short value, int decimalShift ) { 160 if (value == 0) return 0; 161 if (decimalShift >= 0) return value; 162 int shiftedValueP5 = Math.abs(value); 163 for (int i = 0; i != (-decimalShift - 1); ++i) 164 shiftedValueP5 /= 10; 165 shiftedValueP5 += 5l; 166 int shiftedValue = shiftedValueP5 / 10; 167 if (shiftedValue * 10l - shiftedValueP5 >= 5) ++shiftedValue; 168 shiftedValue *= Long.signum(value); 169 for (int i = 0; i != -decimalShift; ++i) 170 shiftedValue *= 10; 171 return (short)shiftedValue; 172 } 173 174 public Short roundDown( Short value, int decimalShift ) { 175 if (value == 0) return 0; 176 if (decimalShift >= 0) return value; 177 int shiftedValue = Math.abs(value); 178 for (int i = 0; i != -decimalShift; ++i) 179 shiftedValue /= 10; 180 shiftedValue *= Long.signum(value); 181 for (int i = 0; i != -decimalShift; ++i) 182 shiftedValue *= 10; 183 return (short)shiftedValue; 184 } 185 186 public Short keepSignificantFigures( Short value, int numSigFigs ) { 187 if (numSigFigs < 0) return value; 188 if (numSigFigs == 0) return 0; 189 int currentExp = getExponentInScientificNotation(value); 190 int decimalShift = -currentExp + numSigFigs - 1; 191 return roundUp(value, decimalShift); 192 } 193 }