diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-17 15:01:44 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-17 15:01:44 -0400 |
| commit | 77fcc58d1facffbc3af50be8c05985350e9f1355 (patch) | |
| tree | b7b81d24c107e644924dc526f8bb034efc62d2dc /BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java | |
| parent | a5850915df72f5968fd1b281eb9e455d50c580ee (diff) | |
Code maintenace and changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java new file mode 100644 index 0000000..77eb899 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -0,0 +1,101 @@ +package bjc.utils.data; + +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * A holder of a single value. + * + * @author ben + * + * @param <ContainedType> + * The type of value held + */ +public interface IHolder<ContainedType> { + /** + * Bind a function across the value in this container + * + * @param <BoundType> + * The type of value in this container + * @param binder + * The function to bind to the value + * @return A holder from binding the value + */ + public <BoundType> IHolder<BoundType> bind( + Function<ContainedType, IHolder<BoundType>> binder); + + /** + * Apply an action to the value + * + * @param action + * The action to apply to the value + */ + public default void doWith(Consumer<ContainedType> action) { + transform((value) -> { + action.accept(value); + + return value; + }); + } + + /** + * Get the value contained in this holder without changing it. + * + * @return The value held in this holder + */ + public default ContainedType getValue() { + return unwrap((value) -> value); + } + + /** + * Create a new holder with a mapped version of the value in this + * holder. + * + * Does not change the internal state of this holder + * + * @param <MappedType> + * The type of the mapped value + * @param mapper + * The function to do mapping with + * @return A holder with the mapped value + */ + public <MappedType> IHolder<MappedType> map( + Function<ContainedType, MappedType> mapper); + + /** + * Transform the value held in this holder + * + * @param transformer + * The function to transform the value with + * @return The holder itself, for easy chaining + */ + public IHolder<ContainedType> transform( + UnaryOperator<ContainedType> transformer); + + /** + * Unwrap the value contained in this holder so that it is no longer + * held + * + * @param <UnwrappedType> + * The type of the unwrapped value + * @param unwrapper + * The function to use to unwrap the value + * @return The unwrapped held value + */ + public <UnwrappedType> UnwrappedType unwrap( + Function<ContainedType, UnwrappedType> unwrapper); + + /** + * Replace the held value with a new one + * + * @param newValue + * The value to hold instead + * @return The holder itself + */ + public default IHolder<ContainedType> replace(ContainedType newValue) { + return transform((oldValue) -> { + return newValue; + }); + } +} |
