UndoHistory.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package holeg.ui.model;
  2. import java.util.Optional;
  3. public class UndoHistory {
  4. public static final int NumberOfSaves = 35;
  5. private static final LimitedSizeQueue<String> jsonSaves = new LimitedSizeQueue<>(NumberOfSaves);
  6. private static int actualIndex = -1;
  7. public static boolean canUndo(){
  8. return actualIndex > 0;
  9. }
  10. public static Optional<String> undo(){
  11. if(canUndo()){
  12. actualIndex -= 1;
  13. return Optional.of(jsonSaves.get(actualIndex));
  14. }
  15. return Optional.empty();
  16. }
  17. public static boolean canRedo(){
  18. return actualIndex < jsonSaves.size() -1;
  19. }
  20. public static Optional<String> redo(){
  21. if(canRedo()){
  22. actualIndex += 1;
  23. return Optional.of(jsonSaves.get(actualIndex));
  24. }
  25. return Optional.empty();
  26. }
  27. public static void addSave(String save){
  28. if(canRedo()){
  29. jsonSaves.removeRange(actualIndex + 1, jsonSaves.size());
  30. }
  31. if(actualIndex < NumberOfSaves){
  32. actualIndex += 1;
  33. }
  34. jsonSaves.add(save);
  35. }
  36. }