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