Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.devnatan.inventoryframework.component;

import me.devnatan.inventoryframework.state.StateValue;
import me.devnatan.inventoryframework.state.StateValueHost;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -16,7 +17,7 @@
* during rendering, this component is determined from the {@link #currentPageIndex() current page
* index}.
*/
public interface Pagination extends ComponentComposition {
public interface Pagination extends ComponentComposition, StateValue {

/**
* The current page number.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.devnatan.inventoryframework.state;

import org.jetbrains.annotations.ApiStatus;

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public abstract class AbstractStateValue implements StateValue {

private final State<?> state;

protected AbstractStateValue(State<?> state) {
this.state = state;
}

@Override
public final State<?> getState() {
return state;
}

@Override
public final long getId() {
return getState().internalId();
}

@Override
public abstract Object get();

@Override
public void set(Object value) {
throw new IllegalStateModificationException("Immutable");
}

@Override
public String toString() {
return "AbstractStateValue{" + "state=" + state + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,43 @@

/**
* Wrapper of the value of a {@link State} for a single {@link StateValueHost}.
* <p>
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
// TODO Must be a interface to allow spread across "component as state" (like Pagination)
public abstract class StateValue {

private final State<?> state;

public StateValue(State<?> state) {
this.state = state;
}
@ApiStatus.Internal
public interface StateValue {

/**
* The state who holds this value.
* The id of this state on its current host.
*
* @return The state who holds this value.
* @return The state id.
*/
public final State<?> getState() {
return state;
}
long getId();

/**
* The id of this state on its current host.
* The state who holds this value.
*
* @return The state id.
* @return The state who holds this value.
*/
public final long getId() {
return getState().internalId();
}
State<?> getState();

/**
* The current state value.
* <p>
* The value returned and consistency with values returned by the same earlier is unknown as
* this is implementation defined.
* <p>
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*
* @return The current state value.
*/
@ApiStatus.Internal
@UnknownNullability
protected abstract Object get();
Object get();

/**
* Sets the new state value.
* <p>
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*
* @param value The new value.
* @throws StateException If this value can't be set.
*/
@ApiStatus.Internal
protected void set(Object value) {
throw new IllegalStateModificationException("Immutable");
}
void set(Object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import me.devnatan.inventoryframework.context.IFSlotClickContext;
import me.devnatan.inventoryframework.context.IFSlotRenderContext;
import me.devnatan.inventoryframework.internal.LayoutSlot;
import me.devnatan.inventoryframework.state.AbstractStateValue;
import me.devnatan.inventoryframework.state.State;
import me.devnatan.inventoryframework.state.StateValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;
import org.jetbrains.annotations.VisibleForTesting;

// TODO add "key" to child pagination components and check if it needs to be updated based on it
@VisibleForTesting
public class PaginationImpl extends StateValue implements Pagination, InteractionHandler {
public class PaginationImpl extends AbstractStateValue implements Pagination, InteractionHandler {

private final List<Component> components = new LinkedList<>();
private final @NotNull IFContext host;
Expand Down Expand Up @@ -323,7 +323,9 @@ public Object get() {
}

@Override
protected void set(Object value) {}
public void set(Object value) {
// do nothing since Pagination is not immutable but unmodifiable directly
}

@Override
public @NotNull VirtualView getRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
/**
* Computed value whose value returned by the function to get the state value is always a new value
* created by {@link ComputedValue#factory}.
*
* <p>
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public final class ComputedValue extends StateValue {
public final class ComputedValue extends AbstractStateValue {

private final Supplier<?> factory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import java.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public final class ImmutableValue extends StateValue {
public final class ImmutableValue extends AbstractStateValue {

private final Object value;

public ImmutableValue(@NotNull State<?> state, Object value) {
public ImmutableValue(State<?> state, Object value) {
super(state);
this.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public final class InitialDataStateValue extends StateValue {
public final class InitialDataStateValue extends AbstractStateValue {

private final StateValue backingValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@
* <p>
* The initial state value is set by a {@link LazyValue#computation}, and this value remains the
* {@link LazyValue#currValue current value} throughout the lifecycle of that value.
*
* <p>
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public final class LazyValue extends StateValue {
public final class LazyValue extends AbstractStateValue {

private static final Object UNINITIALIZED = new Object();

private final Supplier<?> computation;
private Object currValue = UNINITIALIZED;

public LazyValue(@NotNull State<?> state, @NotNull Supplier<?> computation) {
public LazyValue(State<?> state, @NotNull Supplier<?> computation) {
super(state);
this.computation = computation;
}

@Override
public Object get() {
if (currValue.equals(UNINITIALIZED)) currValue = computation.get();

return currValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import java.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

/**
* <b><i> This is an internal inventory-framework API that should not be used from outside of
* this library. No compatibility guarantees are provided. </i></b>
*/
@ApiStatus.Internal
public class MutableValue extends StateValue {
public class MutableValue extends AbstractStateValue {

private Object currValue;

public MutableValue(@NotNull State<?> state, Object currValue) {
public MutableValue(State<?> state, Object currValue) {
super(state);
this.currValue = currValue;
}
Expand Down