Package io.crums.model
Class HashUtc
java.lang.Object
io.crums.model.HashUtc
public class HashUtc
extends java.lang.Object
A 32 byte hash, plus an 8 byte long representing a witness time.
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.ByteBuffer
data
The positional state is never modified, not even momentarily.static int
DATA_SIZE
Data size is Constants.HASH_WIDTH + Long.BYTES.static java.util.Comparator<HashUtc>
HASH_COMPARATOR
Compares 2 instances solely by hash.static long
INCEPTION_UTC
Version 1 concept date.static long
RUN_MAX_UTC
As a sanity check on data, the maximum UTC is bounded by 1 year from class-load time. -
Constructor Summary
-
Method Summary
Modifier and Type Method Description boolean
equals(java.lang.Object o)
boolean
equalsHashUtc(HashUtc o)
Typed equals(Object).java.nio.ByteBuffer
hash()
Returns a read-only buffer whose remaining bytes are the hash.int
hashCode()
Consistent with equals.boolean
hashEquals(HashUtc o)
Tests if this instance's hash is the same as the other.java.lang.String
hashHex()
Returns the hash in its hexadecimal representation.protected void
sanityCheckUtc(long utc)
Bounds checks the given utc as one that could possibly be issued by the service.java.nio.ByteBuffer
serialForm()
Returns the instance's serialized representation as a read-only buffer.java.lang.String
toString()
For debug purposes.long
utc()
Time in UTC milliseconds.static java.nio.ByteBuffer
writeToBuffer(byte[] hash, long utc, java.nio.ByteBuffer out)
Serializes the given hash/utc tuple to the given out buffer.static java.nio.ByteBuffer
writeToBuffer(java.nio.ByteBuffer hash, long utc, java.nio.ByteBuffer out)
Serializes the given hash/utc tuple to the given out buffer.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Field Details
-
HASH_COMPARATOR
Compares 2 instances solely by hash. -
INCEPTION_UTC
public static final long INCEPTION_UTCVersion 1 concept date. We're ditching our existing data, so this'll be okay. A claim to a hash witnessed in the form of a crum before this date (July 1, 2020 12:00:00 PM GMT) is nonsense. The constructor disallows it.- See Also:
RUN_MAX_UTC
, Constant Field Values
-
RUN_MAX_UTC
public static final long RUN_MAX_UTCAs a sanity check on data, the maximum UTC is bounded by 1 year from class-load time. So this means you can't leave a server running forever. Not unrealistic.- See Also:
INCEPTION_UTC
-
DATA_SIZE
public static final int DATA_SIZEData size is Constants.HASH_WIDTH + Long.BYTES.- See Also:
- Constant Field Values
-
data
protected final java.nio.ByteBuffer dataThe positional state is never modified, not even momentarily. (If a subclass does, then it'll likely have undefined concurrent behavior.)
-
-
Constructor Details
-
HashUtc
public HashUtc(java.nio.ByteBuffer data)Creates a new instance with the given data buffer.- Parameters:
data
- the caller agrees not to modify the given buffer in any way; otherwise the immutability the immutability guarantee is for naught.- See Also:
serialForm()
-
HashUtc
public HashUtc(byte[] hash, long utc)Creates a new instance with the given fields.- See Also:
INCEPTION_UTC
,RUN_MAX_UTC
-
HashUtc
public HashUtc(java.nio.ByteBuffer hash, long utc)Creates a new instance with the given fields.- See Also:
INCEPTION_UTC
,RUN_MAX_UTC
-
-
Method Details
-
writeToBuffer
public static java.nio.ByteBuffer writeToBuffer(byte[] hash, long utc, java.nio.ByteBuffer out)Serializes the given hash/utc tuple to the given out buffer. The point of this method is to centralize serialization/deserializaton format (since it's changed).- Parameters:
hash
- Constants.HASH_WIDTH bytes longutc
- (not bounds checked)out
- assumed to have remaining bytes. On return, assuming good arguments, the position is advanced DATA_SIZE bytes.- Returns:
- the out argument (unflipped)
- See Also:
constructor for deserialization.
,writeToBuffer(ByteBuffer, long, ByteBuffer)
-
writeToBuffer
public static java.nio.ByteBuffer writeToBuffer(java.nio.ByteBuffer hash, long utc, java.nio.ByteBuffer out)Serializes the given hash/utc tuple to the given out buffer. The point of this method is to centralize serialization/deserializaton format (since it's changed).- Parameters:
hash
- Constants.HASH_WIDTH remaning bytesutc
- (not bounds checked)out
- assumed to have remaining bytes. On return, assuming good arguments, the position is advanced DATA_SIZE bytes.- Returns:
- the out argument (unflipped)
- See Also:
constructor for deserialization.
,writeToBuffer(byte[], long, ByteBuffer)
-
sanityCheckUtc
protected final void sanityCheckUtc(long utc)Bounds checks the given utc as one that could possibly be issued by the service. -
hash
public java.nio.ByteBuffer hash()Returns a read-only buffer whose remaining bytes are the hash. (It's capacity is in fact greater.) -
hashHex
public java.lang.String hashHex()Returns the hash in its hexadecimal representation. -
utc
public long utc()Time in UTC milliseconds. -
serialForm
public java.nio.ByteBuffer serialForm()Returns the instance's serialized representation as a read-only buffer.- Returns:
- a buffer with DATA_SIZE remaining bytes.
- See Also:
#Crum(ByteBuffer)
-
equals
public final boolean equals(java.lang.Object o)- Overrides:
equals
in classjava.lang.Object
- See Also:
equalsHashUtc(HashUtc)
-
equalsHashUtc
Typed equals(Object). -
hashEquals
Tests if this instance's hash is the same as the other. -
hashCode
public final int hashCode()Consistent with equals. And efficient. (We lean on the randomness of cryptographic hashes).- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()For debug purposes.- Overrides:
toString
in classjava.lang.Object
-