Package de.team33.patterns.lazy.narvi
Class LazyFeatures<H>
java.lang.Object
de.team33.patterns.lazy.narvi.LazyFeatures<H>
- Type Parameters:
H- The host type.
A tool for managing properties of a host instance, which typically result from other properties of the host
and are only actually determined when needed. Once determined, properties are retained until reset.
Example:
public class Sample {
// to manage some "lazy" features ...
// ----------------------------------
private final transient Features features = new Features();
// some "normal" properties (with corresponding getters and setters) ...
// ---------------------------------------------------------------------
private int intValue;
private String stringValue;
private Instant instantValue;
public final int getIntValue() {
return intValue;
}
public final Sample setIntValue(final int intValue) {
// when a "normal" property is modified some "lazy" features will expire ...
// -------------------------------------------------------------------------
features.reset();
this.intValue = intValue;
return this;
}
public final String getStringValue() {
return stringValue;
}
public final Sample setStringValue(final String stringValue) {
features.reset(); // s.a. - features will expire
this.stringValue = stringValue;
return this;
}
public final Instant getInstantValue() {
return instantValue;
}
public final Sample setInstantValue(final Instant instantValue) {
features.reset(); // s.a. - features will expire
this.instantValue = instantValue;
return this;
}
// A List representation of this instance - a "lazy" feature ...
// -------------------------------------------------------------
public final List<Object> toList() {
return features.get(Key.LIST);
}
@Override
public final boolean equals(final Object obj) {
return (this == obj) || ((obj instanceof final Sample other) && toList().equals(other.toList()));
}
// Also provided as "lazy" feature ...
// -----------------------------------
@Override
public final int hashCode() {
return features.get(Key.HASH);
}
// Also provided as "lazy" feature ...
// -----------------------------------
@Override
public final String toString() {
return features.get(Key.STRING);
}
// A local Features derivative (also works as host <H> here) ...
// -------------------------------------------------------------
private class Features extends LazyFeatures<Features> {
@Override
protected final Features host() {
return this;
}
// --------------------------------------------------------------
// ... and provides factory methods for the "lazy" features ...
// --------------------------------------------------------------
private List<Object> newList() {
return Arrays.asList(intValue, stringValue, instantValue);
}
private Integer newHash() {
return toList().hashCode();
}
private String newString() {
return Sample.class.getSimpleName() + toList().toString();
}
}
// A local Key derivative ...
// --------------------------
@FunctionalInterface
private interface Key<R> extends LazyFeatures.Key<Features, R> {
// ... to simplify the local Key definitions ...
// ---------------------------------------------
Key<List<Object>> LIST = Features::newList;
Key<Integer> HASH = Features::newHash;
Key<String> STRING = Features::newString;
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAbstracts the keys needed to access values managed by aLazyFeaturesinstance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <R> Rget(LazyFeatures.Key<? super H, ? extends R> key) On the first call after initialization or reset, executes the initialization code provided by the given key and stores the result.protected abstract Hhost()Returns the actual host<H>.final <R> Optional<R>peek(LazyFeatures.Key<? super H, ? extends R> key) Determines whether the initialization code provided by the given key has already been executed and its result stored.final voidreset()Resets (discards) all the stored results.final voidreset(LazyFeatures.Key<?, ?> key) Resets (discards) the stored result for the given key.
-
Constructor Details
-
LazyFeatures
public LazyFeatures()
-
-
Method Details
-
host
Returns the actual host<H>. This is the instance that finally provides the initialization code referred by the keys. -
peek
Determines whether the initialization code provided by the given key has already been executed and its result stored.Returns an
Optionalcontaining the stored result if any, otherwiseOptional.empty().CAUTION: returns
Optional.empty()even if a stored result exists but isnull!- Type Parameters:
R- The result type.
-
get
On the first call after initialization or reset, executes the initialization code provided by the given key and stores the result.Returns the stored result.
- Type Parameters:
R- The result type.- See Also:
-
reset
Resets (discards) the stored result for the given key. -
reset
public final void reset()Resets (discards) all the stored results.
-