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