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:
UnmodifiableRef
is 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)
.CallableRef
is a Reference which delegates to 3 objects: aSupplier
forget()
, aConsumer
forset(Object)
, and aPredicate
forisValid(Object)
. You can either construct instances directly, or use the factory methodcallable(Supplier, Consumer, Predicate)
.SimulatableRef
is similar toCallableRef
, but uses a singleLimitedConsumer
for bothset(Object)
andisValid(Object)
.DestroyableRef
is a bit different from the others, as it wraps anotherReference
internally, 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()
boolean
boolean
default boolean
set
(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
DestroyableRef
that can be modified untilDestroyableRef.destroy()
is called, after which all calls toset(Object)
andisValid(Object)
will return false.
-
unmodifiable
- Returns:
- A new
Reference
that doesn't permit modifications to the given object.
-
callable
- Parameters:
getter
- TheSupplier
which is used forget()
.setter
- TheConsumer
which is used forset(Object)
(after the filter).filter
- ThePredicate
which is used to test inputs for bothset(Object)
andisValid(Object)
.- Returns:
- A new
CallableRef
that delegates to the getter, setter, and filter for all of the functions.
-
simulating
- Parameters:
getter
- TheSupplier
which is used forget()
setter
- TheLimitedConsumer
which is used forset(Object)
andisValid(Object)
.- Returns:
- A new
SimulatableRef
that delegates to the getter and setter for all of it's functions.
-
destroying
- Returns:
- A new
DestroyableRef
that delegates to the given reference for getting, although setting only succeeds untilDestroyableRef.destroy()
is called.
-