From e4d92d9097033cd32c45f6ea2c4fc60404d3321e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 28 Jan 2024 12:01:13 +0100 Subject: [PATCH] feat: allow accessing backing states --- .../java/org/incendo/state/EmptyStates.java | 6 ++++++ .../java/org/incendo/state/LazyStates.java | 6 ++++++ .../main/java/org/incendo/state/States.java | 8 ++++++++ .../java/org/incendo/state/StatesImpl.java | 18 ++++++++++++------ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/state-core/src/main/java/org/incendo/state/EmptyStates.java b/state-core/src/main/java/org/incendo/state/EmptyStates.java index c05d08a..eb92a30 100644 --- a/state-core/src/main/java/org/incendo/state/EmptyStates.java +++ b/state-core/src/main/java/org/incendo/state/EmptyStates.java @@ -23,6 +23,7 @@ // package org.incendo.state; +import java.util.stream.Stream; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @@ -50,4 +51,9 @@ public boolean empty() { public String toString() { return "()"; } + + @Override + public @NonNull Stream states() { + return Stream.empty(); + } } diff --git a/state-core/src/main/java/org/incendo/state/LazyStates.java b/state-core/src/main/java/org/incendo/state/LazyStates.java index ea4db0c..4322743 100644 --- a/state-core/src/main/java/org/incendo/state/LazyStates.java +++ b/state-core/src/main/java/org/incendo/state/LazyStates.java @@ -25,6 +25,7 @@ import java.util.Objects; import java.util.function.Supplier; +import java.util.stream.Stream; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @@ -59,4 +60,9 @@ public boolean contains(final @NonNull S state) { public boolean empty() { return this.backingStates().empty(); } + + @Override + public @NonNull Stream states() { + return this.backingStates().states(); + } } diff --git a/state-core/src/main/java/org/incendo/state/States.java b/state-core/src/main/java/org/incendo/state/States.java index 55d01c5..d371945 100644 --- a/state-core/src/main/java/org/incendo/state/States.java +++ b/state-core/src/main/java/org/incendo/state/States.java @@ -31,6 +31,7 @@ import java.util.Objects; import java.util.Set; import java.util.function.Supplier; +import java.util.stream.Stream; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @@ -127,4 +128,11 @@ public interface States> { * @return {@code true} if this instance contains no states, else {@code false} */ boolean empty(); + + /** + * Returns a stream of the stateCollection contained in this instance + * + * @return the stateCollection + */ + @NonNull Stream states(); } diff --git a/state-core/src/main/java/org/incendo/state/StatesImpl.java b/state-core/src/main/java/org/incendo/state/StatesImpl.java index 2997f9c..9dca3e8 100644 --- a/state-core/src/main/java/org/incendo/state/StatesImpl.java +++ b/state-core/src/main/java/org/incendo/state/StatesImpl.java @@ -30,15 +30,16 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; @API(status = API.Status.INTERNAL, since = "1.0.0") -record StatesImpl>(@NonNull Collection<@NonNull S> states) implements States { +record StatesImpl>(@NonNull Collection<@NonNull S> stateCollection) implements States { @Override public boolean contains(final @NonNull S state) { - return this.states.contains(state); + return this.stateCollection.contains(state); } @Override @@ -47,22 +48,27 @@ public boolean contains(final @NonNull S state) { Objects.requireNonNull(state, "state"); if (state instanceof Enum) { // Special case so that we preserve the EnumSet. - final EnumSet enumSet = EnumSet.copyOf((Collection) this.states); + final EnumSet enumSet = EnumSet.copyOf((Collection) this.stateCollection); enumSet.add(state); return new StatesImpl<>(Collections.unmodifiableCollection(enumSet)); } - final List states = new ArrayList<>(this.states); + final List states = new ArrayList<>(this.stateCollection); states.add(state); return new StatesImpl<>(Collections.unmodifiableCollection(states)); } @Override public String toString() { - return this.states.stream().map(S::toString).collect(Collectors.joining(", ", "(", ")")); + return this.stateCollection.stream().map(S::toString).collect(Collectors.joining(", ", "(", ")")); } @Override public boolean empty() { - return this.states.isEmpty(); + return this.stateCollection.isEmpty(); + } + + @Override + public @NonNull Stream states() { + return this.stateCollection.stream(); } }