-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
c87d932
commit b5ca294
Showing
4 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package jchess.common.state; | ||
|
||
public interface IRevertibleState { | ||
void saveState(); | ||
|
||
void revertState(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package jchess.common.state; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class StateManager { | ||
private final List<IRevertibleState> stateList = new ArrayList<>(); | ||
|
||
public void registerState(IRevertibleState... states) { | ||
Collections.addAll(stateList, states); | ||
} | ||
|
||
public void saveState() { | ||
for (IRevertibleState state : stateList) { | ||
state.saveState(); | ||
} | ||
} | ||
|
||
public void revertState() { | ||
for (IRevertibleState state : stateList) { | ||
state.revertState(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jchess.common.state.impl; | ||
|
||
import jchess.common.state.IRevertibleState; | ||
|
||
import java.util.function.Function; | ||
|
||
public class ArrayState<T> implements IRevertibleState { | ||
private final T[] current; | ||
private final T[] saved; | ||
|
||
public ArrayState(int size, Function<Integer, T[]> arrayConstructor) { | ||
current = arrayConstructor.apply(size); | ||
saved = arrayConstructor.apply(size); | ||
} | ||
|
||
public T[] getCurrent() { | ||
return current; | ||
} | ||
|
||
@Override | ||
public void saveState() { | ||
System.arraycopy(current, 0, saved, 0, current.length); | ||
} | ||
|
||
@Override | ||
public void revertState() { | ||
System.arraycopy(saved, 0, current, 0, saved.length); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package jchess.common.state.impl; | ||
|
||
import jchess.common.state.IRevertibleState; | ||
|
||
public class BooleanState implements IRevertibleState { | ||
private boolean current; | ||
private boolean saved; | ||
|
||
public BooleanState(boolean initValue) { | ||
this.current = initValue; | ||
} | ||
|
||
public boolean getValue() { | ||
return current; | ||
} | ||
|
||
public void setValue(boolean current) { | ||
this.current = current; | ||
} | ||
|
||
@Override | ||
public void saveState() { | ||
saved = current; | ||
} | ||
|
||
@Override | ||
public void revertState() { | ||
current = saved; | ||
} | ||
} |