Class Builder
java.lang.Object
io.crums.util.mrkl.Builder
- Direct Known Subclasses:
FixedLeafBuilder
Collects items (byte arrays) and builds a Merkle tree. If all the items (the leaves of the tree)
added are fixed-width (and the tree's data fits under 1GB memory), then the instance
builds a FixedLeafTree; otherwise, it builds a FreeLeafTree instance.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanBreadth-first view of the nodes' data.protected final MessageDigestprotected final ObjectSynchronization lock. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCopy constructor.Creates a new copy-on-write (copy-on-add) instance with a dedicatedMessageDigestusing the given hashing algorithm.Creates a new instance with a dedicatedMessageDigestusing the given hashing algorithm. -
Method Summary
Modifier and TypeMethodDescriptionintadd(byte[] item) Adds the specified item as the next leaf node are returns its leaf index.intadd(byte[] item, int off, int len) Adds the specified item as the next leaf node are returns its leaf index.build()Builds and returns the tree.voidclear()Clears the state of the instance, as if new.protected voidfinal intcount()Returns the number of items added.final Stringbyte[]hash(byte[] data) Convenience method to hash using this instance's digest algo.final intbyte[]item(int index) Returns (a copy of) of the item at the givenindex.intReturns the leaf width if fixed; -1 if variable; -2 if not set.protected final List<byte[]> level(int index) protected final intlevelSize(int level) protected List<byte[]> newByteArrayList(int level) protected Tree
-
Field Details
-
lock
Synchronization lock. -
data
-
digest
-
copyOnWrite
protected final boolean copyOnWrite
-
-
Constructor Details
-
Builder
Creates a new copy-on-write (copy-on-add) instance with a dedicatedMessageDigestusing the given hashing algorithm.- Parameters:
algo- the digest algorithm (e.g. MD5, SHA-1, SHA-256)- Throws:
IllegalArgumentException- in lieu of checkedNoSuchAlgorithmException
-
Builder
Creates a new instance with a dedicatedMessageDigestusing the given hashing algorithm.- Parameters:
algo- the digest algorithm (e.g. MD5, SHA-1, SHA-256)copyOnWrite- iftrue, then every add(byte[]) is argument is copied (the argument's value is considered volatile). When you know you won't be modifying the input arguments set this tofalse- Throws:
IllegalArgumentException- in lieu of checkedNoSuchAlgorithmException
-
Builder
Copy constructor. The new instance shares the same fields as this instance.- Parameters:
copy-
-
-
Method Details
-
add
public int add(byte[] item) Adds the specified item as the next leaf node are returns its leaf index.- Parameters:
item- the item's data (copied)- Returns:
- the item's leaf node index in the to-be built tree
- See Also:
-
hash
public byte[] hash(byte[] data) Convenience method to hash using this instance's digest algo. Does not affect the state of the builder.- Parameters:
data- to be hashed- Returns:
- a new array containing the hash
-
hashWidth
public final int hashWidth() -
add
Adds the specified item as the next leaf node are returns its leaf index.- Parameters:
item- the item's data (copied)off- starting offset intoitemlen- the number of bytes followingoff- Returns:
- the item's leaf node index in the to-be built tree
- Throws:
IndexOutOfBoundsException
-
build
-
completeTree
protected void completeTree() -
packageTree
-
leafWidth
public int leafWidth()Returns the leaf width if fixed; -1 if variable; -2 if not set.- Returns:
- ≥ -2
-
clear
public void clear()Clears the state of the instance, as if new. -
count
public final int count()Returns the number of items added. -
item
Returns (a copy of) of the item at the givenindex.- Parameters:
index- < count()- Throws:
IndexOutOfBoundsException
-
getHashAlgo
-
levelSize
protected final int levelSize(int level) -
newByteArrayList
-
level
-