Skip to content

Commit

Permalink
feat: allow accessing backing states
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 28, 2024
1 parent 5ec4f47 commit e4d92d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
6 changes: 6 additions & 0 deletions state-core/src/main/java/org/incendo/state/EmptyStates.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -50,4 +51,9 @@ public boolean empty() {
public String toString() {
return "()";
}

@Override
public @NonNull Stream<S> states() {
return Stream.empty();
}
}
6 changes: 6 additions & 0 deletions state-core/src/main/java/org/incendo/state/LazyStates.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,4 +60,9 @@ public boolean contains(final @NonNull S state) {
public boolean empty() {
return this.backingStates().empty();
}

@Override
public @NonNull Stream<S> states() {
return this.backingStates().states();
}
}
8 changes: 8 additions & 0 deletions state-core/src/main/java/org/incendo/state/States.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -127,4 +128,11 @@ public interface States<S extends State<S>> {
* @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<S> states();
}
18 changes: 12 additions & 6 deletions state-core/src/main/java/org/incendo/state/StatesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<S extends State<S>>(@NonNull Collection<@NonNull S> states) implements States<S> {
record StatesImpl<S extends State<S>>(@NonNull Collection<@NonNull S> stateCollection) implements States<S> {

@Override
public boolean contains(final @NonNull S state) {
return this.states.contains(state);
return this.stateCollection.contains(state);
}

@Override
Expand All @@ -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<S> states = new ArrayList<>(this.states);
final List<S> 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<S> states() {
return this.stateCollection.stream();
}
}

0 comments on commit e4d92d9

Please sign in to comment.