aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/Ic2ExpReactorPlanner/BigintStorage.java
blob: d9d4d62c3c6155a29aa428d97ab847dc6f9a7ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Ic2ExpReactorPlanner;

import java.math.BigInteger;
import java.util.Base64;

/**
 * Stores numbers of varying size inside a BigInteger, expecting each to have a defined limit (which need not be an
 * exact power of 2). Numbers are to be extracted in reverse order they were stored, and special values can be used to
 * make certain values optional for inclusion (the calling class is responsible for handling this logic, though).
 * 
 * @author Brian McCloud
 */
public class BigintStorage {

    private BigInteger storedValue = BigInteger.ZERO;

    /**
     * Stores the specified value. Requires that 0 <= value <= max.
     * 
     * @param value the value to store.
     * @param max   the expected maximum for the value.
     */
    public void store(int value, int max) {
        if (value < 0 || value > max) {
            throw new IllegalArgumentException();
        }
        storedValue = storedValue.multiply(BigInteger.valueOf(max + 1)).add(BigInteger.valueOf(value));
    }

    /**
     * Extracts a value based on the specified maximum.
     * 
     * @param max the expected maximum for the value.
     * @return the extracted value.
     */
    public int extract(int max) {
        BigInteger[] values = storedValue.divideAndRemainder(BigInteger.valueOf(max + 1));
        storedValue = values[0];
        return values[1].intValue();
    }

    /**
     * Takes input of a Base64 string, and converts it to a BigintStorage.
     * 
     * @param code the Base64-encoded string (presumed to be from @outputBase64)
     * @return the converted storage object.
     */
    public static BigintStorage inputBase64(String code) {
        BigintStorage result = new BigintStorage();
        byte[] temp = Base64.getDecoder().decode(code);
        result.storedValue = new BigInteger(temp);
        return result;
    }

    /**
     * Outputs the current value of this BigintStorage as a Base64-encoded string.
     * 
     * @return the Base64-encoded string.
     */
    public String outputBase64() {
        byte[] temp = storedValue.toByteArray();
        return Base64.getEncoder().encodeToString(temp);
    }
}