Package alexiil.mc.lib.attributes.misc
Interface Reference<T>
- All Known Subinterfaces:
StackReference
- All Known Implementing Classes:
CallableRef,DestroyableRef,Ref,SimulatableRef,SingleCopyingItemSlot,SingleFluidTank,SingleItemSlot,UnmodifiableRef
public interface Reference<T>
A reference to some object. The object can either be obtained from (
get()), or changed with
(set(Object)). Note that changing the object isn't always permitted, and so it may return false if no change
happened.
Most users will only need to construct Ref instances to create a Reference, which is a simple
reference that stores the object in a public modifiable field. However there are also 4 other basic implementations:
UnmodifiableRefis a Reference which always returns the same object fromget(), and returns false fromset(Object)andisValid(Object). You can either construct instances directly or via the factory methodunmodifiable(Object).CallableRefis a Reference which delegates to 3 objects: aSupplierforget(), aConsumerforset(Object), and aPredicateforisValid(Object). You can either construct instances directly, or use the factory methodcallable(Supplier, Consumer, Predicate).SimulatableRefis similar toCallableRef, but uses a singleLimitedConsumerfor bothset(Object)andisValid(Object).DestroyableRefis a bit different from the others, as it wraps anotherReferenceinternally, and delegates all calls to it. However you can callDestroyableRef.destroy()to disallow any further modifications to the backing reference.
-
Method Summary
Modifier and TypeMethodDescriptiondefault DestroyableRef<T>static <T> CallableRef<T>static <T> DestroyableRef<T>destroying(Reference<T> ref) get()booleanbooleandefault booleanset(T value, Simulation simulation) Delegates toset(Object)if the simulation isSimulation.ACTION, otherwise it delegates toisValid(Object).static <T> SimulatableRef<T>simulating(Supplier<T> getter, LimitedConsumer<T> setter) static <T> UnmodifiableRef<T>unmodifiable(T obj)
-
Method Details
-
get
T get()- Returns:
- The object referenced. Note that you should generally not modify the returned value directly - instead
copy it before passing it to
set(Object)orisValid(Object)to see if your modifications are permitted.
-
set
- Returns:
- True if the new value was accepted, false otherwise.
-
isValid
- Returns:
- True if
set(Object)was called with the same value.
-
set
Delegates toset(Object)if the simulation isSimulation.ACTION, otherwise it delegates toisValid(Object). -
asDestroyable
- Returns:
- A
DestroyableRefthat can be modified untilDestroyableRef.destroy()is called, after which all calls toset(Object)andisValid(Object)will return false.
-
unmodifiable
- Returns:
- A new
Referencethat doesn't permit modifications to the given object.
-
callable
- Parameters:
getter- TheSupplierwhich is used forget().setter- TheConsumerwhich is used forset(Object)(after the filter).filter- ThePredicatewhich is used to test inputs for bothset(Object)andisValid(Object).- Returns:
- A new
CallableRefthat delegates to the getter, setter, and filter for all of the functions.
-
simulating
- Parameters:
getter- TheSupplierwhich is used forget()setter- TheLimitedConsumerwhich is used forset(Object)andisValid(Object).- Returns:
- A new
SimulatableRefthat delegates to the getter and setter for all of it's functions.
-
destroying
- Returns:
- A new
DestroyableRefthat delegates to the given reference for getting, although setting only succeeds untilDestroyableRef.destroy()is called.
-