Created
October 1, 2015 19:54
-
-
Save vpiotr/8d39f3b4432ba530b3f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Author: Piotr Likus, SpaceInSoft, 2015 | |
* License: MIT | |
*/ | |
package com.spaceinsoft.utils; | |
import java.math.BigDecimal; | |
import java.math.BigInteger; | |
/** | |
* Implements arithmetic operations - same on all built-in numeric classes. | |
* Can be used in generic methods with Number-derived classes. | |
* | |
* Operations: | |
* add, subtract, multiply, divide, compareTo | |
* | |
* Supported types: | |
* BigDecimal, BigInteger, Number, Float, Double, Integer, Long, Short, Byte | |
* | |
* Example: | |
* see Accumulator.java | |
* | |
* @author Piotr Likus | |
*/ | |
public class NumberOperatorUtils { | |
public static Number add(Number a, Number b) { | |
if (a instanceof Integer) | |
return (Integer)a + (Integer)b; | |
else if (a instanceof Long) | |
return (Long)a + (Long)b; | |
else if (a instanceof Double) | |
return (Double)a + (Double)b; | |
else if (a instanceof Float) | |
return (Float)a + (Float)b; | |
else if (a instanceof BigDecimal) | |
return ((BigDecimal)a).add((BigDecimal)b); | |
else if (a instanceof BigInteger) | |
return ((BigInteger)a).add((BigInteger)b); | |
else if (a instanceof Short) | |
return (Short)a + (Short)b; | |
else if (a instanceof Byte) | |
return (Byte)a + (Byte)b; | |
else | |
throw new IllegalArgumentException("No addition implemented for: " + a.getClass()); | |
} | |
public static BigDecimal add(BigDecimal a, BigDecimal b) { | |
return a.add(b); | |
} | |
public static BigInteger add(BigInteger a, BigInteger b) { | |
return a.add(b); | |
} | |
public static Double add(Double a, Double b) { | |
return a + b; | |
} | |
public static Float add(Float a, Float b) { | |
return a + b; | |
} | |
public static Integer add(Integer a, Integer b) { | |
return a + b; | |
} | |
public static Long add(Long a, Long b) { | |
return a + b; | |
} | |
public static Short add(Short a, Short b) { | |
return (short)(a + b); | |
} | |
public static Byte add(Byte a, Byte b) { | |
return (byte)(a + b); | |
} | |
public static Number subtract(Number a, Number b) { | |
if (a instanceof Integer) | |
return (Integer)a - (Integer)b; | |
else if (a instanceof Long) | |
return (Long)a - (Long)b; | |
else if (a instanceof Double) | |
return (Double)a - (Double)b; | |
else if (a instanceof Float) | |
return (Float)a - (Float)b; | |
else if (a instanceof BigDecimal) | |
return ((BigDecimal)a).subtract((BigDecimal)b); | |
else if (a instanceof BigInteger) | |
return ((BigInteger)a).subtract((BigInteger)b); | |
else if (a instanceof Short) | |
return (Short)a - (Short)b; | |
else if (a instanceof Byte) | |
return (Byte)a - (Byte)b; | |
else | |
throw new IllegalArgumentException("No subtraction implemented for: " + a.getClass()); | |
} | |
public static BigDecimal subtract(BigDecimal a, BigDecimal b) { | |
return a.subtract(b); | |
} | |
public static BigInteger subtract(BigInteger a, BigInteger b) { | |
return a.subtract(b); | |
} | |
public static Double subtract(Double a, Double b) { | |
return a - b; | |
} | |
public static Float subtract(Float a, Float b) { | |
return a - b; | |
} | |
public static Integer subtract(Integer a, Integer b) { | |
return a - b; | |
} | |
public static Long subtract(Long a, Long b) { | |
return a - b; | |
} | |
public static Short subtract(Short a, Short b) { | |
return (short)(a - b); | |
} | |
public static Byte subtract(Byte a, Byte b) { | |
return (byte)(a - b); | |
} | |
public static Number multiply(Number a, Number b) { | |
if (a instanceof Integer) | |
return (Integer)a * (Integer)b; | |
else if (a instanceof Long) | |
return (Long)a * (Long)b; | |
else if (a instanceof Double) | |
return (Double)a * (Double)b; | |
else if (a instanceof Float) | |
return (Float)a * (Float)b; | |
else if (a instanceof BigDecimal) | |
return ((BigDecimal)a).multiply((BigDecimal)b); | |
else if (a instanceof BigInteger) | |
return ((BigInteger)a).multiply((BigInteger)b); | |
else if (a instanceof Short) | |
return (Short)a * (Short)b; | |
else if (a instanceof Byte) | |
return (Byte)a * (Byte)b; | |
else | |
throw new IllegalArgumentException("No multiplication implemented for: " + a.getClass()); | |
} | |
public static BigDecimal multiply(BigDecimal a, BigDecimal b) { | |
return a.multiply(b); | |
} | |
public static BigInteger multiply(BigInteger a, BigInteger b) { | |
return a.multiply(b); | |
} | |
public static Double multiply(Double a, Double b) { | |
return a * b; | |
} | |
public static Float multiply(Float a, Float b) { | |
return a * b; | |
} | |
public static Integer multiply(Integer a, Integer b) { | |
return a * b; | |
} | |
public static Long multiply(Long a, Long b) { | |
return a * b; | |
} | |
public static Short multiply(Short a, Short b) { | |
return (short)(a * b); | |
} | |
public static Byte multiply(Byte a, Byte b) { | |
return (byte)(a * b); | |
} | |
public static Number divide(Number a, Number b) { | |
if (a instanceof Integer) | |
return (Integer)a / (Integer)b; | |
else if (a instanceof Long) | |
return (Long)a / (Long)b; | |
else if (a instanceof Double) | |
return (Double)a / (Double)b; | |
else if (a instanceof Float) | |
return (Float)a / (Float)b; | |
else if (a instanceof BigDecimal) | |
return ((BigDecimal)a).divide((BigDecimal)b); | |
else if (a instanceof BigInteger) | |
return ((BigInteger)a).divide((BigInteger)b); | |
else if (a instanceof Short) | |
return (Short)a / (Short)b; | |
else if (a instanceof Byte) | |
return (Byte)a / (Byte)b; | |
else | |
throw new IllegalArgumentException("No division implemented for: " + a.getClass()); | |
} | |
public static BigDecimal divide(BigDecimal a, BigDecimal b) { | |
return a.divide(b); | |
} | |
public static BigInteger divide(BigInteger a, BigInteger b) { | |
return a.divide(b); | |
} | |
public static Double divide(Double a, Double b) { | |
return a / b; | |
} | |
public static Float divide(Float a, Float b) { | |
return a / b; | |
} | |
public static Integer divide(Integer a, Integer b) { | |
return a / b; | |
} | |
public static Long divide(Long a, Long b) { | |
return a / b; | |
} | |
public static Short divide(Short a, Short b) { | |
return (short)(a / b); | |
} | |
public static Byte divide(Byte a, Byte b) { | |
return (byte)(a / b); | |
} | |
public static int compareTo(Number a, Number b) { | |
if (a instanceof Integer) | |
return ((Integer)a).compareTo((Integer)b); | |
else if (a instanceof Long) | |
return ((Long)a).compareTo((Long)b); | |
else if (a instanceof Double) | |
return ((Double)a).compareTo((Double)b); | |
else if (a instanceof Float) | |
return ((Float)a).compareTo((Float)b); | |
else if (a instanceof BigDecimal) | |
return ((BigDecimal)a).compareTo((BigDecimal)b); | |
else if (a instanceof BigInteger) | |
return ((BigInteger)a).compareTo((BigInteger)b); | |
else if (a instanceof Short) | |
return ((Short)a).compareTo((Short)b); | |
else if (a instanceof Byte) | |
return ((Byte)a).compareTo((Byte)b); | |
else | |
throw new IllegalArgumentException("No comparison implemented for: " + a.getClass()); | |
} | |
public static int compareTo(BigDecimal a, BigDecimal b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(BigInteger a, BigInteger b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Double a, Double b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Float a, Float b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Integer a, Integer b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Long a, Long b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Short a, Short b) { | |
return a.compareTo(b); | |
} | |
public static int compareTo(Byte a, Byte b) { | |
return a.compareTo(b); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment