package au.id.zancanaro.javacheck.state; import java.util.Map; import java.util.NoSuchElementException; public class CommandValue { public static interface Action { T doAction() throws Throwable; } public static interface VoidAction { void doAction() throws Throwable; } private static Map values = null; public static T withValues(Map newValues, Action action) throws Throwable { Map oldValues = values; try { values = newValues; return action.doAction(); } finally { values = oldValues; } } public static void withValues(Map newValues, VoidAction action) throws Throwable { Map oldValues = values; try { values = newValues; action.doAction(); } finally { values = oldValues; } } private final int id; public CommandValue(int id) { this.id = id; } public boolean isAbstract() { return values == null; } @SuppressWarnings("unchecked") public T get() { if (values.containsKey(getId())) { return (T) values.get(getId()); } else { throw new NoSuchElementException("Concrete values cannot be supplied prior to being calculated"); } } public int getId() { return id; } @Override public String toString() { return "#{" + id + "}"; } }