/* This class is thread safe */ public class Range { private int lower; // guarded by this private int upper; // guarded by this // INVARIANT: lower <= upper private boolean invariantOK(int l, int u) { return l <= u; } public Range(int lower, int upper) { if (!invariantOK(lower,upper)) throw new IllegalArgumentException(); this.lower = lower; this.upper = upper; } public synchronized boolean inRange(int v) { if (v >= lower && v <= upper) return true; return false; } public synchronized void incUpper() throws IllegalArgumentException { upper ++; } public synchronized void incLower() { if (!invariantOK(lower+1,upper)) throw new IllegalArgumentException(); lower ++; } public synchronized void decLower() { lower-- ; } public synchronized void decUpper() throws IllegalArgumentException { if (!invariantOK(lower,upper-1)) throw new IllegalArgumentException(); upper --; } }