Skip to content

Commit

Permalink
Replace Hold with Guard.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasMikula committed Apr 23, 2014
1 parent a1dfc55 commit cb7508f
Show file tree
Hide file tree
Showing 45 changed files with 196 additions and 163 deletions.
29 changes: 29 additions & 0 deletions reactfx/src/main/java/org/reactfx/Guard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.reactfx;

@FunctionalInterface
public interface Guard extends Hold {

static Guard EMPTY_GUARD = () -> {};

/**
* Releases this guard. Does not throw.
*/
@Override
void close();


/**
* Returns a guard that is a composition of multiple guards.
* Its {@code close()} method closes the guards in reverse order.
* @param guards guards that should be released (in reverse order)
* when the returned guards is released.
* @return
*/
static Guard multi(Guard... guards) {
return () -> {
for(int i = guards.length - 1; i >= 0; --i) {
guards[i].close();
}
};
}
}
4 changes: 4 additions & 0 deletions reactfx/src/main/java/org/reactfx/Hold.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.reactfx;

/**
* @deprecated Use {@link Guard} instead.
*/
@Deprecated
public interface Hold extends AutoCloseable {

static Hold EMPTY_HOLD = () -> {};
Expand Down
12 changes: 6 additions & 6 deletions reactfx/src/main/java/org/reactfx/Indicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class Indicator implements ObservableBooleanValue {

private boolean on = false;

public Hold on() {
public Guard on() {
if(on) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
set(true);
return this::release;
Expand All @@ -29,13 +29,13 @@ public Hold on() {
*
* <p>Equivalent to
* <pre>
* try(Hold h = on()) {
* try(Guard g = on()) {
* r.run();
* }
* </pre>
*/
public void onWhile(Runnable r) {
try(Hold h = on()) {
try(Guard g = on()) {
r.run();
}
}
Expand All @@ -52,13 +52,13 @@ public void onWhile(Runnable r) {
*
* <pre>
* T t;
* try(Hold h = on()) {
* try(Guard g = on()) {
* t = f.get();
* }
* </pre>
*/
public <T> T onWhile(Supplier<T> f) {
try(Hold h = on()) {
try(Guard g = on()) {
return f.get();
}
}
Expand Down
30 changes: 15 additions & 15 deletions reactfx/src/main/java/org/reactfx/InterceptableEventStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

public interface InterceptableEventStream<T> extends EventStream<T> {

Hold mute();
Hold pause();
Hold retainLatest();
Hold reduce(BinaryOperator<T> reduction);
Hold tryReduce(BiFunction<T, T, ReductionResult<T>> reduction);
Guard mute();
Guard pause();
Guard retainLatest();
Guard reduce(BinaryOperator<T> reduction);
Guard tryReduce(BiFunction<T, T, ReductionResult<T>> reduction);

/**
* @deprecated Use {@link #reduce(BinaryOperator)} instead.
Expand All @@ -29,43 +29,43 @@ default Hold fuse(BiFunction<T, T, ReductionResult<T>> fusor) {
}

default void muteWhile(Runnable r) {
try(Hold h = mute()) { r.run(); }
try(Guard g = mute()) { r.run(); }
};

default <U> U muteWhile(Supplier<U> f) {
try(Hold h = mute()) { return f.get(); }
try(Guard g = mute()) { return f.get(); }
}

default void pauseWhile(Runnable r) {
try(Hold h = pause()) { r.run(); }
try(Guard g = pause()) { r.run(); }
}

default <U> U pauseWhile(Supplier<U> f) {
try(Hold h = pause()) { return f.get(); }
try(Guard g = pause()) { return f.get(); }
}

default void retainLatestWhile(Runnable r) {
try(Hold h = retainLatest()) { r.run(); }
try(Guard g = retainLatest()) { r.run(); }
}

default <U> U retainLatestWhile(Supplier<U> f) {
try(Hold h = retainLatest()) { return f.get(); }
try(Guard g = retainLatest()) { return f.get(); }
}

default void reduceWhile(BinaryOperator<T> reduction, Runnable r) {
try(Hold h = reduce(reduction)) { r.run(); }
try(Guard g = reduce(reduction)) { r.run(); }
}

default <U> U reduceWhile(BinaryOperator<T> reduction, Supplier<U> f) {
try(Hold h = reduce(reduction)) { return f.get(); }
try(Guard g = reduce(reduction)) { return f.get(); }
}

default void tryReduceWhile(BiFunction<T, T, ReductionResult<T>> reduction, Runnable r) {
try(Hold h = tryReduce(reduction)) { r.run(); }
try(Guard g = tryReduce(reduction)) { r.run(); }
}

default <U> U tryReduceWhile(BiFunction<T, T, ReductionResult<T>> reduction, Supplier<U> f) {
try(Hold h = tryReduce(reduction)) { return f.get(); }
try(Guard g = tryReduce(reduction)) { return f.get(); }
}

/**
Expand Down
22 changes: 11 additions & 11 deletions reactfx/src/main/java/org/reactfx/InterceptableEventStreamImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,49 @@ protected Subscription subscribeToInputs() {
}

@Override
public Hold mute() {
public Guard mute() {
switch(consumer.getType()) {
case MUTE: return Hold.EMPTY_HOLD; // second mute would have no effect
case MUTE: return Guard.EMPTY_GUARD; // second mute would have no effect
default: return stack(new MutedConsumer<T>(consumer));
}
}

@Override
public Hold pause() {
public Guard pause() {
switch(consumer.getType()) {
case NORMAL: return stack(new PausedConsumer<T>(consumer));
default: return Hold.EMPTY_HOLD; // pausing has no effect if another interception is already in effect
default: return Guard.EMPTY_GUARD; // pausing has no effect if another interception is already in effect
}
}

@Override
public Hold retainLatest() {
public Guard retainLatest() {
switch(consumer.getType()) {
case MUTE: // retaining anything is pointless if it is going to be muted anyway
case RETAIN_LATEST: // second retainLatest would have no effect
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
default:
return stack(new RetainLatestConsumer<T>(consumer));
}
}

@Override
public Hold reduce(BinaryOperator<T> fusor) {
public Guard reduce(BinaryOperator<T> fusor) {
switch(consumer.getType()) {
case MUTE: return Hold.EMPTY_HOLD;
case MUTE: return Guard.EMPTY_GUARD;
default: return stack(new FusionConsumer<T>(consumer, fusor));
}
}

@Override
public Hold tryReduce(BiFunction<T, T, ReductionResult<T>> fusor) {
public Guard tryReduce(BiFunction<T, T, ReductionResult<T>> fusor) {
switch(consumer.getType()) {
case MUTE: return Hold.EMPTY_HOLD;
case MUTE: return Guard.EMPTY_GUARD;
default: return stack(new OptionalFusionConsumer<T>(consumer, fusor));
}
}

private Hold stack(StackedConsumer<T> newConsumer) {
private Guard stack(StackedConsumer<T> newConsumer) {
consumer = newConsumer;
return () -> unstack(newConsumer);
}
Expand Down
22 changes: 11 additions & 11 deletions reactfx/src/main/java/org/reactfx/inhibeans/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

import java.util.function.Supplier;

import org.reactfx.Hold;
import org.reactfx.Guard;

public interface Observable extends javafx.beans.Observable {

/**
* Prevents invalidation and change events from being emitted,
* until the returned block is released.
* until the returned guard is released.
*
* @return a {@code Block} instance that can be released to resume
* @return a {@code Guard} instance that can be released to resume
* the delivery of invalidation and change events. If this observable
* has been invalidated one or more times before the block is released,
* has been invalidated one or more times before the guard is released,
* a single notification is passed to invalidation and change listeners
* of this observable.
* The returned {@code Block} is {@code AutoCloseable}, which makes it
* convenient to use it in try-with-resources.
* The returned {@code Guard} is {@code AutoCloseable}, which makes it
* convenient to use in try-with-resources.
*/
Hold block();
Guard block();

/**
* Runs the given computation, making sure the invalidation and change
* events are blocked. When done, previous blocked state is restored.
*
* <p>Equivalent to
* <pre>
* try(Hold h = block()) {
* try(Guard g = block()) {
* r.run();
* }
* </pre>
*/
default void blockWhile(Runnable r) {
try(Hold b = block()) {
try(Guard g = block()) {
r.run();
}
}
Expand All @@ -49,13 +49,13 @@ default void blockWhile(Runnable r) {
*
* <pre>
* T t;
* try(Hold h = block()) {
* try(Guard g = block()) {
* t = f.get();
* }
* </pre>
*/
default <T> T blockWhile(Supplier<T> f) {
try(Hold b = block()) {
try(Guard g = block()) {
return f.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.reactfx.inhibeans.binding;

import org.reactfx.Hold;

import javafx.beans.InvalidationListener;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableBooleanValue;

import org.reactfx.Guard;

import com.sun.javafx.binding.ExpressionHelper;

/**
Expand All @@ -29,9 +29,9 @@ public static BooleanBinding wrap(ObservableBooleanValue source) {
private boolean fireOnRelease = false;

@Override
public Hold block() {
public Guard block() {
if(blocked) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
blocked = true;
return this::release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import org.reactfx.Hold;
import org.reactfx.Guard;

import com.sun.javafx.binding.ExpressionHelper;

Expand All @@ -29,9 +29,9 @@ public static DoubleBinding wrap(ObservableValue<? extends Number> source) {
private boolean fireOnRelease = false;

@Override
public Hold block() {
public Guard block() {
if(blocked) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
blocked = true;
return this::release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import org.reactfx.Hold;
import org.reactfx.Guard;

import com.sun.javafx.binding.ExpressionHelper;

Expand All @@ -29,9 +29,9 @@ public static FloatBinding wrap(ObservableValue<? extends Number> source) {
private boolean fireOnRelease = false;

@Override
public Hold block() {
public Guard block() {
if(blocked) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
blocked = true;
return this::release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import org.reactfx.Hold;
import org.reactfx.Guard;

import com.sun.javafx.binding.ExpressionHelper;

Expand All @@ -29,9 +29,9 @@ public static IntegerBinding wrap(ObservableValue<? extends Number> source) {
private boolean fireOnRelease = false;

@Override
public Hold block() {
public Guard block() {
if(blocked) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
blocked = true;
return this::release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import org.reactfx.Hold;
import org.reactfx.Guard;

import com.sun.javafx.binding.ExpressionHelper;

Expand All @@ -29,9 +29,9 @@ public static LongBinding wrap(ObservableValue<? extends Number> source) {
private boolean fireOnRelease = false;

@Override
public Hold block() {
public Guard block() {
if(blocked) {
return Hold.EMPTY_HOLD;
return Guard.EMPTY_GUARD;
} else {
blocked = true;
return this::release;
Expand Down
Loading

0 comments on commit cb7508f

Please sign in to comment.