Skip to content

Commit

Permalink
Merge pull request #28 from JordanMartinez/fixChange
Browse files Browse the repository at this point in the history
Apply undos and redos using the same Consumer
  • Loading branch information
JordanMartinez authored Jul 16, 2018
2 parents 8bced07 + bcd7f7b commit 2b5d6b1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 155 deletions.
131 changes: 48 additions & 83 deletions undofx/src/main/java/org/fxmisc/undo/UndoManagerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,15 @@ public static <C> UndoManager<C> zeroHistorySingleChangeUM(EventStream<C> change
* @param invert Inverts a change, so that applying the inverted change ({@code apply.accept(invert.apply(c))})
* has the effect of undoing the original change ({@code c}). Inverting a change twice should
* result in the original change ({@code invert.apply(invert.apply(c)).equals(c)}).
* @param applyUndo Used to apply a list of undoable changes. From the point of view of {@code applyUndo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param applyRedo Used to apply a list of redoable changes. From the point of view of {@code applyRedo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param apply Used to apply a list of changes. {@code list} describes a list of actions to be performed.
* Calling {@code apply.accept(list)} <em>must</em> cause {@code list} to be emitted from
* the {@code changeStream}.
*/
default <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo) {
return createMultiChangeUM(changeStream, invert, applyUndo, applyRedo, (c1, c2) -> Optional.empty());
Consumer<List<C>> apply) {
return createMultiChangeUM(changeStream, invert, apply, (c1, c2) -> Optional.empty());
}

/**
Expand All @@ -284,22 +280,18 @@ default <C> UndoManager<List<C>> createMultiChangeUM(
* @param invert Inverts a change, so that applying the inverted change ({@code apply.accept(invert.apply(c))})
* has the effect of undoing the original change ({@code c}). Inverting a change twice should
* result in the original change ({@code invert.apply(invert.apply(c)).equals(c)}).
* @param applyUndo Used to apply a list of undoable changes. From the point of view of {@code applyUndo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param applyRedo Used to apply a list of redoable changes. From the point of view of {@code applyRedo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param apply Used to apply a list of changes. {@code list} describes a list of actions to be performed.
* Calling {@code apply.accept(list)} <em>must</em> cause {@code list} to be emitted from
* the {@code changeStream}.
* @param merge Used to merge two subsequent changes into one.
* Returns an empty {@linkplain Optional} when the changes cannot or should not be merged.
*/
default <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge) {
return createMultiChangeUM(changeStream, invert, applyUndo, applyRedo, merge, c -> false);
return createMultiChangeUM(changeStream, invert, apply, merge, c -> false);
}

/**
Expand All @@ -310,12 +302,9 @@ default <C> UndoManager<List<C>> createMultiChangeUM(
* @param invert Inverts a change, so that applying the inverted change ({@code apply.accept(invert.apply(c))})
* has the effect of undoing the original change ({@code c}). Inverting a change twice should
* result in the original change ({@code invert.apply(invert.apply(c)).equals(c)}).
* @param applyUndo Used to apply a list of undoable changes. From the point of view of {@code applyUndo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param applyRedo Used to apply a list of redoable changes. From the point of view of {@code applyRedo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param apply Used to apply a list of changes. {@code list} describes a list of actions to be performed.
* Calling {@code apply.accept(list)} <em>must</em> cause {@code list} to be emitted from
* the {@code changeStream}.
* @param merge Used to merge two subsequent changes into one.
* Returns an empty {@linkplain Optional} when the changes cannot or should not be merged.
* If two changes "annihilate" (i.e. {@code merge.apply(c1, c2).isPresen()} and
Expand All @@ -327,11 +316,10 @@ default <C> UndoManager<List<C>> createMultiChangeUM(
default <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity) {
return createMultiChangeUM(changeStream, invert, applyUndo, applyRedo, merge, isIdentity, Duration.ZERO);
return createMultiChangeUM(changeStream, invert, apply, merge, isIdentity, Duration.ZERO);
}

/**
Expand All @@ -342,12 +330,9 @@ default <C> UndoManager<List<C>> createMultiChangeUM(
* @param invert Inverts a change, so that applying the inverted change ({@code apply.accept(invert.apply(c))})
* has the effect of undoing the original change ({@code c}). Inverting a change twice should
* result in the original change ({@code invert.apply(invert.apply(c)).equals(c)}).
* @param applyUndo Used to apply a list of undoable changes. From the point of view of {@code applyUndo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param applyRedo Used to apply a list of redoable changes. From the point of view of {@code applyRedo},
* {@code list} describes a list of actions to be performed. Calling {@code applyRedo.accept(list)}
* <em>must</em> cause {@code list} to be emitted from {@code changeStream}.
* @param apply Used to apply a list of changes. {@code list} describes a list of actions to be performed.
* Calling {@code apply.accept(list)} <em>must</em> cause {@code list} to be emitted from
* the {@code changeStream}.
* @param merge Used to merge two subsequent changes into one.
* Returns an empty {@linkplain Optional} when the changes cannot or should not be merged.
* If two changes "annihilate" (i.e. {@code merge.apply(c1, c2).isPresen()} and
Expand All @@ -363,8 +348,7 @@ default <C> UndoManager<List<C>> createMultiChangeUM(
<C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay);
Expand All @@ -377,129 +361,115 @@ <C> UndoManager<List<C>> createMultiChangeUM(
* Creates an {@link UndoManager} with unlimited history.
*
* For description of parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.
*/
public static <C> UndoManager<List<C>> unlimitedHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo) {
return unlimitedHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
(c1, c2) -> Optional.empty());
Consumer<List<C>> apply) {
return unlimitedHistoryMultiChangeUM(changeStream, invert, apply, (c1, c2) -> Optional.empty());
}

/**
* Creates an {@link UndoManager} with unlimited history.
*
* For description of parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.
*/
public static <C> UndoManager<List<C>> unlimitedHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge) {
return unlimitedHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
merge, c -> false, Duration.ZERO);
return unlimitedHistoryMultiChangeUM(changeStream, invert, apply, merge, c -> false, Duration.ZERO);
}

/**
* Creates an {@link UndoManager} with unlimited history.
*
* For description of parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.
*/
public static <C> UndoManager<List<C>> unlimitedHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity) {
return unlimitedHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
merge, isIdentity, Duration.ZERO);
return unlimitedHistoryMultiChangeUM(changeStream, invert, apply, merge, isIdentity, Duration.ZERO);
}

/**
* Creates an {@link UndoManager} with unlimited history.
*
* For description of parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.
*/
public static <C> UndoManager<List<C>> unlimitedHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay) {
ChangeQueue<List<C>> queue = new UnlimitedChangeQueue<>();
return new MultiChangeUndoManagerImpl<>(queue, invert, applyUndo, applyRedo,
merge, isIdentity, changeStream, preventMergeDelay);
return new MultiChangeUndoManagerImpl<>(queue, invert, apply, merge, isIdentity, changeStream, preventMergeDelay);
}

/**
* Creates an {@link UndoManager} with bounded history.
* When at full capacity, a new change will cause the oldest change to be forgotten.
*
* <p>For description of the remaining parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.</p>
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.</p>
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager<List<C>> fixedSizeHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
int capacity) {
return fixedSizeHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
(c1, c2) -> Optional.empty(), capacity);
return fixedSizeHistoryMultiChangeUM(changeStream, invert, apply, (c1, c2) -> Optional.empty(), capacity);
}

/**
* Creates an {@link UndoManager} with bounded history.
* When at full capacity, a new change will cause the oldest change to be forgotten.
*
* <p>For description of the remaining parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.</p>
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.</p>
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager<List<C>> fixedSizeHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
int capacity) {
return fixedSizeHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
merge, c -> false, Duration.ZERO, capacity);
return fixedSizeHistoryMultiChangeUM(changeStream, invert, apply, merge, c -> false, Duration.ZERO, capacity);
}

/**
* Creates an {@link UndoManager} with bounded history.
* When at full capacity, a new change will cause the oldest change to be forgotten.
*
* <p>For description of the remaining parameters, see
* {@link #createMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)}.</p>
* {@link #createMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)}.</p>
*
* @param capacity maximum number of changes the returned UndoManager can store
*/
public static <C> UndoManager<List<C>> fixedSizeHistoryMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay,
int capacity) {
ChangeQueue<List<C>> queue = new FixedSizeChangeQueue<>(capacity);
return new MultiChangeUndoManagerImpl<>(queue, invert, applyUndo, applyRedo,
merge, isIdentity, changeStream, preventMergeDelay);
return new MultiChangeUndoManagerImpl<>(queue, invert, apply, merge, isIdentity, changeStream, preventMergeDelay);
}

/**
Expand All @@ -513,7 +483,7 @@ public static <C> UndoManager<List<C>> fixedSizeHistoryMultiChangeUM(
public static <C> UndoManager<List<C>> zeroHistoryMultiChangeUM(EventStream<List<C>> changeStream) {
ChangeQueue<List<C>> queue = new ZeroSizeChangeQueue<>();
Consumer<List<C>> doNothingForUndoAndRedo = c -> {};
return new MultiChangeUndoManagerImpl<>(queue, c -> c, doNothingForUndoAndRedo, doNothingForUndoAndRedo,
return new MultiChangeUndoManagerImpl<>(queue, c -> c, doNothingForUndoAndRedo,
(c1, c2) -> Optional.empty(), c -> false, changeStream);
}

Expand All @@ -525,7 +495,7 @@ public static <C> UndoManager<List<C>> zeroHistoryMultiChangeUM(EventStream<List
* Creates a factory for {@link UndoManager}s with unlimited history.
*
* @see #unlimitedHistorySingleChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)
* @see #unlimitedHistoryMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration)
* @see #unlimitedHistoryMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration)
*/
public static UndoManagerFactory unlimitedHistoryFactory() {
return new UndoManagerFactory() {
Expand All @@ -544,13 +514,11 @@ public <C> UndoManager<C> createSingleChangeUM(
public <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay) {
return unlimitedHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
merge, isIdentity, preventMergeDelay);
return unlimitedHistoryMultiChangeUM(changeStream, invert, apply, merge, isIdentity, preventMergeDelay);
}
};
}
Expand All @@ -560,7 +528,7 @@ public <C> UndoManager<List<C>> createMultiChangeUM(
* When at full capacity, a new change will cause the oldest change to be forgotten.
*
* @see #fixedSizeHistorySingleChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration, int)
* @see #fixedSizeHistoryMultiChangeUM(EventStream, Function, Consumer, Consumer, BiFunction, Predicate, Duration, int)
* @see #fixedSizeHistoryMultiChangeUM(EventStream, Function, Consumer, BiFunction, Predicate, Duration, int)
*/
public static UndoManagerFactory fixedSizeHistoryFactory(int capacity) {
return new UndoManagerFactory() {
Expand All @@ -579,13 +547,11 @@ public <C> UndoManager<C> createSingleChangeUM(
public <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay) {
return fixedSizeHistoryMultiChangeUM(changeStream, invert, applyUndo, applyRedo,
merge, isIdentity, preventMergeDelay, capacity);
return fixedSizeHistoryMultiChangeUM(changeStream, invert, apply, merge, isIdentity, preventMergeDelay, capacity);
}
};
}
Expand Down Expand Up @@ -613,8 +579,7 @@ public <C> UndoManager<C> createSingleChangeUM(
public <C> UndoManager<List<C>> createMultiChangeUM(
EventStream<List<C>> changeStream,
Function<? super C, ? extends C> invert,
Consumer<List<C>> applyUndo,
Consumer<List<C>> applyRedo,
Consumer<List<C>> apply,
BiFunction<C, C, Optional<C>> merge,
Predicate<C> isIdentity,
Duration preventMergeDelay) {
Expand Down
Loading

0 comments on commit 2b5d6b1

Please sign in to comment.