Package uk.megaslice.delta
Class Delta<T,K>
- java.lang.Object
-
- uk.megaslice.delta.Delta<T,K>
-
- Type Parameters:
T- the type of dataset itemsK- the type of the items' natural keys
public final class Delta<T,K> extends Object
Represents changes to a dataset in terms of insert, update and delete operations. Deltas can be created using thediffmethod.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Collection<T>apply(Iterable<T> items, NaturalKey<T,K> naturalKey)Applies this delta's inserts, updates and deletes to another dataset.Map<K,T>apply(Map<K,T> items)Applies this delta's inserts, updates and deletes to another dataset.Delta<T,K>combine(Delta<T,K> other)Combines this delta with another delta, using a default item equivalence function.Delta<T,K>combine(Delta<T,K> other, Equivalence<T> equivalence)Combines this delta with another delta.<U> Delta<T,K>combine(Delta<T,K> other, Essence<T,U> essence)Combines this delta with another delta, using anEssencefunction to distill items for comparison.Collection<T>deletedItems()Returns the deleted items of this delta.Collection<Operation<T>>deletes()Returns the delete operations of this delta.static <T,K>
Delta<T,K>diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey)Creates a delta from two datasets, using a default item equivalence function.static <T,K>
Delta<T,K>diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey, Equivalence<T> equivalence)Creates a delta from two datasets.static <T,U,K>
Delta<T,K>diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey, Essence<T,U> essence)Creates a delta from two datasets, using anEssencefunction to distill items for comparison.static <T,K>
Delta<T,K>diff(Map<K,T> before, Map<K,T> after)Creates a delta from two datasets, using a default item equivalence function.static <T,K>
Delta<T,K>diff(Map<K,T> before, Map<K,T> after, Equivalence<T> equivalence)Creates a delta from two datasets.static <T,U,K>
Delta<T,K>diff(Map<K,T> before, Map<K,T> after, Essence<T,U> essence)Creates a delta from two datasets, using anEssencefunction to distill items for comparison.static <T,K>
Delta<T,K>empty()Returns an empty delta.Optional<Operation<T>>get(K key)Retrieves an operation by the natural key of an item in the dataset.Collection<T>insertedItems()Returns the inserted items of this delta.Collection<Operation<T>>inserts()Returns the insert operations of this delta.booleanisEmpty()Indicates whether this delta is empty.Map<K,Operation<T>>operations()Returns the insert, update and delete operations of this delta.Collection<T>updatedItems()Returns the updated items of this delta.Collection<Operation<T>>updates()Returns the update operations of this delta.
-
-
-
Method Detail
-
operations
public Map<K,Operation<T>> operations()
Returns the insert, update and delete operations of this delta.- Returns:
- the operations of this delta
-
inserts
public Collection<Operation<T>> inserts()
Returns the insert operations of this delta.- Returns:
- the insert operations of this delta
-
insertedItems
public Collection<T> insertedItems()
Returns the inserted items of this delta.- Returns:
- the inserted items of this delta
-
updates
public Collection<Operation<T>> updates()
Returns the update operations of this delta.- Returns:
- the update operations of this delta
-
updatedItems
public Collection<T> updatedItems()
Returns the updated items of this delta.- Returns:
- the updated items of this delta
-
deletes
public Collection<Operation<T>> deletes()
Returns the delete operations of this delta.- Returns:
- the delete operations of this delta
-
deletedItems
public Collection<T> deletedItems()
Returns the deleted items of this delta.- Returns:
- the deleted items of this delta
-
get
public Optional<Operation<T>> get(K key)
Retrieves an operation by the natural key of an item in the dataset.- Parameters:
key- the natural key of an item affected by this delta- Returns:
- a
Optionalcontaining the operation for the given key, or an emptyOptionalif no operation exists for the key - Throws:
NullPointerException- ifkeyis null
-
isEmpty
public boolean isEmpty()
Indicates whether this delta is empty.- Returns:
- true if this delta has no operations, false otherwise
-
apply
public Collection<T> apply(Iterable<T> items, NaturalKey<T,K> naturalKey)
Applies this delta's inserts, updates and deletes to another dataset.- Parameters:
items- the dataset to which this delta will be appliednaturalKey- a function to derive natural keys for dataset items- Returns:
- a new collection containing items with this delta applied
- Throws:
DuplicateKeyException- ifitemscontains any items with the same natural key, or if this delta contains an insert with the same key as an item in the datasetNullPointerException- ifitemsis null, any element initemsis null,naturalKeyis null or ifnaturalKeyproduces a null for any item
-
apply
public Map<K,T> apply(Map<K,T> items)
Applies this delta's inserts, updates and deletes to another dataset.- Parameters:
items- the dataset to which this delta will be applied- Returns:
- a new collection containing items with this delta applied
- Throws:
DuplicateKeyException- if this delta contains an insert with the same key as an item in the datasetNullPointerException- ifitemsis null, or if any key or value initemsis null
-
combine
public Delta<T,K> combine(Delta<T,K> other)
Combines this delta with another delta, using a default item equivalence function. Applying a combined delta of A and B is equivalent to applying A, then B.- Parameters:
other- the other delta with which to combine- Returns:
- a new combined delta
- Throws:
InvalidCombination- if operations for the same natural key in the deltas cannot be combinedNullPointerException- ifotheris null- See Also:
Operation.combine(Operation)
-
combine
public <U> Delta<T,K> combine(Delta<T,K> other, Essence<T,U> essence)
Combines this delta with another delta, using anEssencefunction to distill items for comparison. Applying a combined delta of A and B is equivalent to applying A, then B.- Type Parameters:
U- the distilled type for equivalence comparisons- Parameters:
other- the other delta with which to combineessence- a function to distill items down to their essential features for equivalence comparisons- Returns:
- a new combined delta
- Throws:
InvalidCombination- if operations for the same natural key in the deltas cannot be combinedNullPointerException- ifotheris null- See Also:
Operation.combine(Operation)
-
combine
public Delta<T,K> combine(Delta<T,K> other, Equivalence<T> equivalence)
Combines this delta with another delta. Applying a combined delta of A and B is equivalent to applying A, then B.- Parameters:
other- the other delta with which to combineequivalence- a function to determine whether two dataset items are equivalent- Returns:
- a new combined delta
- Throws:
InvalidCombination- if operations for the same natural key in the deltas cannot be combinedNullPointerException- ifotheris null orequivalenceis null- See Also:
Operation.combine(Operation, Equivalence)
-
empty
public static <T,K> Delta<T,K> empty()
Returns an empty delta.- Type Parameters:
T- the type of items in the deltaK- the type of the items' natural keys- Returns:
- an empty delta
-
diff
public static <T,K> Delta<T,K> diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey)
Creates a delta from two datasets, using a default item equivalence function.- Type Parameters:
T- the type of dataset itemsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were madenaturalKey- a function to derive natural keys for dataset items in the datasets- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
DuplicateKeyException- if any items inbeforehave the same natural key, or if any items inafterhave the same natural keyNullPointerException- ifbeforeis null,afteris null, any element inbeforeorafteris null,naturalKeyis null or ifnaturalKeyproduces a null for any item
-
diff
public static <T,U,K> Delta<T,K> diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey, Essence<T,U> essence)
Creates a delta from two datasets, using anEssencefunction to distill items for comparison.- Type Parameters:
T- the type of dataset itemsU- the distilled type for equivalence comparisonsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were madenaturalKey- a function to derive natural keys for dataset items in the datasetsessence- a function to distill items down to their essential features for equivalence comparisons- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
DuplicateKeyException- if any items inbeforehave the same natural key, or if any items inafterhave the same natural keyNullPointerException- ifbeforeis null,afteris null, any element inbeforeorafteris null,naturalKeyis null or ifnaturalKeyproduces a null for any item
-
diff
public static <T,K> Delta<T,K> diff(Iterable<T> before, Iterable<T> after, NaturalKey<T,K> naturalKey, Equivalence<T> equivalence)
Creates a delta from two datasets.- Type Parameters:
T- the type of dataset itemsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were madenaturalKey- a function to derive natural keys for dataset items in the datasetsequivalence- a function to determine whether two dataset items are equivalent- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
DuplicateKeyException- if any items inbeforehave the same natural key, or if any items inafterhave the same natural keyNullPointerException- ifbeforeis null,afteris null, any element inbeforeorafteris null,naturalKeyis null,naturalKeyproduces a null for any item or ifequivalenceis null
-
diff
public static <T,K> Delta<T,K> diff(Map<K,T> before, Map<K,T> after)
Creates a delta from two datasets, using a default item equivalence function.- Type Parameters:
T- the type of dataset itemsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were made- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
NullPointerException- ifbeforeis null,afteris null, or if any key or value inbeforeorafteris null
-
diff
public static <T,U,K> Delta<T,K> diff(Map<K,T> before, Map<K,T> after, Essence<T,U> essence)
Creates a delta from two datasets, using anEssencefunction to distill items for comparison.- Type Parameters:
T- the type of dataset itemsU- the distilled type for equivalence comparisonsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were madeessence- a function to distill items down to their essential features for equivalence comparisons- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
NullPointerException- ifbeforeis null,afteris null, or if any key or value inbeforeorafteris null
-
diff
public static <T,K> Delta<T,K> diff(Map<K,T> before, Map<K,T> after, Equivalence<T> equivalence)
Creates a delta from two datasets.- Type Parameters:
T- the type of dataset itemsK- the type of the items' natural keys- Parameters:
before- the dataset before changes were madeafter- the dataset after changes were madeequivalence- a function to determine whether two dataset items are equivalent- Returns:
- a delta representing the difference between the two datasets in terms of inserts, updates and deletes
- Throws:
NullPointerException- ifbeforeis null,afteris null, any key or value inbeforeorafteris null, or ifequivalenceis null
-
-