-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
283 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 49 additions & 13 deletions
62
...le-utils/common-utils/src/main/java/org/daisy/pipeline/braille/common/WithSideEffect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,73 @@ | ||
package org.daisy.pipeline.braille.common; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public abstract class WithSideEffect<T,W> implements Function<W,T> { | ||
public abstract class WithSideEffect<T,W> implements com.google.common.base.Function<W,T> { | ||
|
||
private T value = null; | ||
private boolean computed = false; | ||
|
||
public final T apply(W world) { | ||
public final T apply(W world) throws Exception { | ||
if (!computed) { | ||
sideEffectsBuilder = new ImmutableList.Builder<Function<? super W,?>>(); | ||
sideEffectsBuilder = new ImmutableList.Builder<com.google.common.base.Function<? super W,?>>(); | ||
firstWorld = world; | ||
value = _apply(); | ||
sideEffects = sideEffectsBuilder.build(); | ||
sideEffectsBuilder = null; | ||
computed = true; } | ||
try { | ||
value = _apply(); | ||
sideEffects = sideEffectsBuilder.build(); | ||
computed = true; } | ||
catch (Throwable t) { | ||
throw new Exception(t); } | ||
finally { | ||
sideEffectsBuilder = null; | ||
firstWorld = null; }} | ||
else | ||
for (Function<? super W,?> sideEffect : sideEffects) | ||
for (com.google.common.base.Function<? super W,?> sideEffect : sideEffects) | ||
try { sideEffect.apply(world); } catch(Throwable t) {} | ||
return value; | ||
} | ||
|
||
protected abstract T _apply(); | ||
protected abstract T _apply() throws Throwable; | ||
|
||
private List<Function<? super W,?>> sideEffects; | ||
private ImmutableList.Builder<Function<? super W,?>> sideEffectsBuilder; | ||
private List<com.google.common.base.Function<? super W,?>> sideEffects; | ||
private ImmutableList.Builder<com.google.common.base.Function<? super W,?>> sideEffectsBuilder; | ||
private W firstWorld; | ||
|
||
protected final <V> V applyWithSideEffect(final Function<? super W,? extends V> withSideEffect) { | ||
protected final <V> V applyWithSideEffect(final com.google.common.base.Function<? super W,? extends V> withSideEffect) { | ||
sideEffectsBuilder.add(withSideEffect); | ||
return withSideEffect.apply(firstWorld); | ||
} | ||
|
||
@SuppressWarnings("serial") | ||
public static class Exception extends NoSuchElementException { | ||
private final Throwable cause; | ||
private Exception(Throwable cause) { | ||
this.cause = cause; | ||
} | ||
@Override | ||
public Throwable getCause() { | ||
return cause; | ||
} | ||
} | ||
|
||
public static abstract class Function<F,T,W> implements com.google.common.base.Function<WithSideEffect<F,W>,WithSideEffect<T,W>> { | ||
public abstract T _apply(F input); | ||
public final WithSideEffect<T,W> apply(final WithSideEffect<F,W> input) { | ||
return new WithSideEffect<T,W>() { | ||
public T _apply() { | ||
current = this; | ||
try { | ||
return Function.this._apply(applyWithSideEffect(input)); } | ||
finally { | ||
current = null; } | ||
} | ||
}; | ||
} | ||
private WithSideEffect<T,W> current; | ||
protected final <V> V applyWithSideEffect(final com.google.common.base.Function<? super W,? extends V> withSideEffect) { | ||
return current.applyWithSideEffect(withSideEffect); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.