Package net.cscott.jutil
Interface Environment<K,V>
- All Superinterfaces:
Map<K,V>
- All Known Implementing Classes:
HashEnvironment,PersistentEnvironment
An
Environment is a Map with scoping:
you can save marks into the environment and undo all changes
since a mark.
Closing a mark undoes all changes since the mark, so a try-with-resources statement makes a scope:
try (Environment.Mark scope = env.getMark()) {
env.put("x", 1); // the binding of x until the end of the block
...
} // x has its old binding, or none, again
(javac -Xlint:try warns that scope is not used in
the block; Java 22 and later can name it _ instead.)- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA mark into an environment. -
Method Summary
Modifier and TypeMethodDescriptiongetMark()Get a mark that will allow you to restore the current state of this environment.voidUndo all changes since the supplied mark, restoring the map to its state at the time the mark was taken.Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Method Details
-
getMark
Environment.Mark getMark()Get a mark that will allow you to restore the current state of this environment. -
undoToMark
Undo all changes since the supplied mark, restoring the map to its state at the time the mark was taken. The undoToMark() operation must be repeatable.- Throws:
IllegalArgumentException- if the mark is not one of this environment's (inHashEnvironmentandPersistentEnvironment; since 2.0)
-