I have some code to figure out the correctness of manipulations on different data types (int, long, double) comparing to BigInteger. The manipulation is getting a factorial of a number until the result is the same as BigInteger has.
The question is how could i change my code, make more generic, compact and clean? How could i get the only one method, not 4 for different types? The logic in this methods is pretty the same as well as the flow.
The code (without comparing logic) is:
private static HashMap<BigInteger, BigInteger> bigIntegerFactorials = new HashMap<>();
private static BigInteger bigIntegerFactorial(BigInteger number) {
    if (number.equals(BigInteger.ONE)) {
        return BigInteger.ONE;
    }
    BigInteger result = bigIntegerFactorials.get(number);
    if (result == null) {
        result = number.multiply(bigIntegerFactorial(number.subtract(BigInteger.ONE)));
        bigIntegerFactorials.put(number, result);
    }
    return result;
}
private static HashMap<Integer, Integer> intFactorials = new HashMap<>();
private static int intFactorial(int number) {
    if (number == 1) {
        return 1;
    }
    Integer result = intFactorials.get(number);
    if (result == null) {
        result = number * intFactorial(number - 1);
        intFactorials.put(number, result);
    }
    return result;
}
private static HashMap<Long, Long> longFactorials = new HashMap<>();
private static long longFactorial(long number) {
    if (number == 1) {
        return 1L;
    }
    Long result = longFactorials.get(number);
    if (result == null) {
        result = number * longFactorial(number - 1);
        longFactorials.put(number, result);
    }
    return result;
}
private static HashMap<Double, Double> doubleFactorials = new HashMap<>();
private static double doubleFactorial(double number) {
    if (number == 1) {
        return 1.;
    }
    Double result = doubleFactorials.get(number);
    if (result == null) {
        result = number * doubleFactorial(number - 1);
        doubleFactorials.put(number, result);
    }
    return result;
}
Thanks a lot in advance.
                        
You could pass the multiply and decrement functions to a generic method:
Then you can change your primitive methods like so:
WARNING: this method seems to crash Netbeans but compiles fine with javac...