Java Number Add add(T a, T b)

Here you can find the source of add(T a, T b)

Description

add

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T extends Number> T add(T a, T b) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T extends Number> T add(T a, T b) {
        if (a instanceof Integer) {
            return (T) new Integer((Integer) a + (Integer) b);
        }//from   w  w  w.  j  a  v a2  s  . c  o  m
        if (a instanceof Long) {
            return (T) new Long((Long) a + (Long) b);
        }
        if (a instanceof Double) {
            return (T) new Double((Double) a + (Double) b);
        }
        if (a instanceof BigDecimal) {
            return (T) ((BigDecimal) a).add((BigDecimal) b);
        }
        throw new IllegalArgumentException("Type not supported: " + a.getClass().getSimpleName());
    }
}

Related

  1. add(long val, long augend)
  2. add(Number a, Number b)
  3. add(Object addend, Object augend)
  4. add(Object num1, Object num2)
  5. add(String number1, String number2)
  6. add1(Double v1, Double v2)
  7. add4Money(Double value1, Double value2)
  8. addAmounts(final double num1, final double num2)
  9. addDoubles(Double value, Double addValue)