aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/Ic2ExpReactorPlanner/BigintStorage.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/Ic2ExpReactorPlanner/BigintStorage.java')
-rw-r--r--src/main/java/Ic2ExpReactorPlanner/BigintStorage.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/java/Ic2ExpReactorPlanner/BigintStorage.java b/src/main/java/Ic2ExpReactorPlanner/BigintStorage.java
new file mode 100644
index 0000000000..03960cd7b1
--- /dev/null
+++ b/src/main/java/Ic2ExpReactorPlanner/BigintStorage.java
@@ -0,0 +1,60 @@
+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);
+ }
+}