Correctly Deal with Identity Changes and Divergent History #5

Merged
kske merged 3 commits from b/divergent-history into develop 2021-12-24 22:16:46 +01:00
4 changed files with 63 additions and 6 deletions
Showing only changes of commit d484839c8b - Show all commits

View File

@ -27,8 +27,7 @@ public final class UnlimitedChangeManager<C extends Change> implements ChangeMan
change.apply(); change.apply();
// Remove divergent changes // Remove divergent changes
for (int i = index + 1; i < changes.size(); ++i) changes.subList(index + 1, changes.size()).clear();
changes.remove(i);
changes.add(change); changes.add(change);
++index; ++index;

View File

@ -14,11 +14,22 @@ class ChangeManagerTest {
IntWrapper wrapper; IntWrapper wrapper;
IntChange change; IntChange change;
/**
* Generates an int change with the given value.
*
* @param value the value of the change
* @return the created change
* @since 0.0.1
*/
IntChange change(int value) {
return new IntChange(wrapper, value);
}
@BeforeEach @BeforeEach
void prepareChangeManager() { void prepareChangeManager() {
manager = new UnlimitedChangeManager<>(); manager = new UnlimitedChangeManager<>();
wrapper = new IntWrapper(); wrapper = new IntWrapper();
change = new IntChange(wrapper, 1); change = change(1);
} }
/** /**
@ -43,7 +54,7 @@ class ChangeManagerTest {
@Order(15) @Order(15)
void testDiscardIdentity() { void testDiscardIdentity() {
manager.addChange(change); manager.addChange(change);
assertFalse(manager.addChange(new IntChange(wrapper, 0))); assertFalse(manager.addChange(change(0)));
assertSame(1, wrapper.value); assertSame(1, wrapper.value);
} }
@ -122,10 +133,13 @@ class ChangeManagerTest {
@Order(55) @Order(55)
void testDiscardDivergentHistory() { void testDiscardDivergentHistory() {
manager.addChange(change); manager.addChange(change);
manager.addChange(change(2));
manager.undo(); manager.undo();
assertTrue(manager.addChange(new IntChange(wrapper, 2))); manager.undo();
assertTrue(manager.addChange(change(3)));
assertFalse(manager.isRedoAvailable()); assertFalse(manager.isRedoAvailable());
assertSame(2, wrapper.value); assertSame(3, wrapper.value);
assertSame(1, manager.getChanges().size());
} }
/** /**

View File

@ -1,5 +1,7 @@
package dev.kske.undoredo.core; package dev.kske.undoredo.core;
import java.util.Objects;
/** /**
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 0.0.1 * @since 0.0.1
@ -28,4 +30,24 @@ class IntChange implements Change {
public boolean isIdentity() { public boolean isIdentity() {
return value == 0; return value == 0;
} }
@Override
public String toString() {
return String.valueOf(value);
}
@Override
public int hashCode() {
return Objects.hash(value, wrapper);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof IntChange))
return false;
IntChange other = (IntChange) obj;
return value == other.value && Objects.equals(wrapper, other.wrapper);
}
} }

View File

@ -1,5 +1,7 @@
package dev.kske.undoredo.core; package dev.kske.undoredo.core;
import java.util.Objects;
/** /**
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 0.0.1 * @since 0.0.1
@ -7,4 +9,24 @@ package dev.kske.undoredo.core;
class IntWrapper { class IntWrapper {
int value; int value;
@Override
public String toString() {
return String.valueOf(value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof IntWrapper))
return false;
IntWrapper other = (IntWrapper) obj;
return value == other.value;
}
} }