public class HistoricalDigestTree<T extends State> extends Object
complement(Digest)
). The digest of the current set of states is retrievable
using digest()
.
A common use case would be to maintain a Merkle-like tree (one level deep) which would allow users to determine the difference between a past state (identified using a digest) and the current state:
Digest oldDigest = instance.digest();
instance.add(state1, state2);
Set diff = instance.complement(oldDigest);
assertTrue(diff.contains(state1));
assertTrue(diff.contains(state2));
States should be comparable and matchable; a State
must implement the common Java Comparable
interface but also the State.matches(State)
, which allows this class to
find states that match but then compare them and replace older versions.Constructor and Description |
---|
HistoricalDigestTree(int maxHistorySize,
MessageDigest digestAlgorithm) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Collection<T> states)
Add some states to the current state set; this method will not change the current state set when a state is older
than the matching state currently held
|
boolean |
add(T... states)
Helper method for
add(java.util.Collection) |
Set<T> |
all() |
Set<T> |
complement(Digest digest)
Determine the set of states that are held currently but not held in the states denoted by the given digest; if
the digest is unrecognized, the entire current set of states is returned
|
Digest |
digest() |
protected int |
find(T state)
Uses
State.matches(State) to compare states for |
boolean |
isCurrent(Digest digest) |
boolean |
isEmpty(Digest digest) |
boolean |
isKnown(Digest digest) |
boolean |
isNewer(T a,
T b) |
public HistoricalDigestTree(int maxHistorySize, MessageDigest digestAlgorithm)
maxHistorySize
- the number of change sets to trackdigestAlgorithm
- the digest algorithm to use, e.g. MessageDigest.getInstance("SHA-256")public final Digest digest()
public boolean add(Collection<T> states)
states
- the state objects to add to the current setpublic boolean add(T... states)
add(java.util.Collection)
states
- the state objects to add to the current setprotected int find(T state)
State.matches(State)
to compare states forstate
- the state to match withpublic boolean isNewer(T a, T b)
a
- the first stateb
- the second statepublic boolean isEmpty(Digest digest)
digest
- the digest to testpublic boolean isCurrent(Digest digest)
digest
- the digest to testpublic boolean isKnown(Digest digest)
digest
- the digest to testpublic Set<T> complement(Digest digest)
digest
- the digest to compare againstCopyright © 2017. All rights reserved.