Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Add hasYieldedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
rcahoon committed Apr 13, 2024
1 parent 8a08dd2 commit c63f676
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/team766/framework/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import java.lang.StackWalker.StackFrame;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;

class ContextImpl<T> implements Runnable, ContextWithValue<T>, LaunchedContextWithValue<T> {
private T m_lastYieldedValue;
private Optional<T> m_lastYieldedValue = Optional.empty();

/**
* Represents the baton-passing state (see class comments). Instead of
Expand Down Expand Up @@ -374,20 +375,25 @@ public void yield() {

@Override
public void yield(final T valueToYield) {
m_lastYieldedValue = valueToYield;
m_lastYieldedValue = Optional.of(valueToYield);
this.yield();
}

@Override
public T lastYieldedValue() {
return m_lastYieldedValue;
return m_lastYieldedValue.orElse(null);
}

@Override
public boolean hasYieldedValue() {
return m_lastYieldedValue.isPresent();
}

@Override
public T getAndClearLastYieldedValue() {
final var result = m_lastYieldedValue;
m_lastYieldedValue = null;
return result;
m_lastYieldedValue = Optional.empty();
return result.orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ public interface LaunchedContextWithValue<T> extends LaunchedContext {
*/
T lastYieldedValue();

/**
* Return true if a has been yielded by the Procedure since since the last call to
* getAndClearLastYieldedValue. Return false otherwise.
*/
boolean hasYieldedValue();

/**
* Return the most recent value that the Procedure passed to Context.yield(T), and clear the
* recorded last yielded value such that subsequent calls to lastYieldedValue() will return
* null.
* recorded last yielded value such that subsequent calls to hasYieldedValue() will return
* false.
*/
T getAndClearLastYieldedValue();
}

0 comments on commit c63f676

Please sign in to comment.