From 05ec409ce96da92d430c4a8e58b08d46f42d667a Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 5 Jun 2015 17:30:46 +1000 Subject: More work on the stateful checker; still not perfect, but it's getting better --- .../id/zancanaro/javacheck/state/CommandValue.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java (limited to 'src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java') diff --git a/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java b/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java new file mode 100644 index 0000000..8d3f272 --- /dev/null +++ b/src/main/java/au/id/zancanaro/javacheck/state/CommandValue.java @@ -0,0 +1,47 @@ +package au.id.zancanaro.javacheck.state; + +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.function.Supplier; + +public class CommandValue { + private static Map values = null; + + public static T withValues(Map newValues, Supplier action) { + Map oldValues = values; + try { + values = newValues; + return action.get(); + } 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 + "}"; + } +} -- cgit v1.2.3