Add support for U128, passed as [u8; 16] but with human wrappers
[ldk-java] / src / main / java / org / ldk / util / UInt128.java
1 package org.ldk.util;
2
3 import java.util.Arrays;
4
5 /**
6  * A 5-bit unsigned integer
7  */
8 public class UInt128 {
9     private byte[] le_bytes;
10
11     /**
12      * Constructs a 128-bit integer from its little-endian representation
13      */
14     public UInt128(byte[] le_bytes) {
15         if (le_bytes.length != 16) {
16             throw new IllegalArgumentException();
17         }
18         this.le_bytes = le_bytes;
19     }
20
21     /**
22      * Constructs a 128-bit integer from a long, ignoring the sign bit
23      */
24     public UInt128(long val) {
25         byte[] le_bytes = new byte[16];
26         for (int i = 0; i < 8; i++)
27             le_bytes[i] = (byte) ((val >> i*8) & 0xff);
28         this.le_bytes = le_bytes;
29     }
30
31     /**
32      * @return The value as 16 little endian bytes
33      */
34     public byte[] getLEBytes() { return le_bytes; }
35
36     @Override public boolean equals(Object o) {
37         if (o == null || !(o instanceof UInt128)) return false;
38         return Arrays.equals(le_bytes, ((UInt128) o).le_bytes);
39     }
40
41     @Override public int hashCode() { return Arrays.hashCode(le_bytes); }
42 }