samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java
changeset 111 3905a2e66b9b
child 112 64308321f7bd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionSynchronizedWithAssert.java	Sat Jun 14 09:54:36 2008 +0200
     1.3 @@ -0,0 +1,34 @@
     1.4 +package org.apidesign.reentrant;
     1.5 +
     1.6 +import java.util.Collection;
     1.7 +
     1.8 +public class CriticalSectionSynchronizedWithAssert<T extends Comparable<T>> implements CriticalSection<T> {
     1.9 +    private T pilot;
    1.10 +    private int cnt;
    1.11 +    private boolean working;
    1.12 +    
    1.13 +    public synchronized void assignPilot(T pilot) {
    1.14 +        assert !working : "Shall not be working yet in order to be consistent";
    1.15 +        this.pilot = pilot;
    1.16 +    }
    1.17 +
    1.18 +    public int sumBigger(Collection<T> args) {
    1.19 +        assert !working : "Shall not be working yet in order to be consistent";
    1.20 +        working = true;
    1.21 +        try {
    1.22 +            for (T cmp : args) {
    1.23 +                if (pilot.compareTo(cmp) < 0) {
    1.24 +                    cnt++;
    1.25 +                }
    1.26 +            }
    1.27 +            return cnt;
    1.28 +        } finally {
    1.29 +            working = false;
    1.30 +        }
    1.31 +    }
    1.32 +    
    1.33 +    public int getCount() {
    1.34 +        assert !working : "Shall not be working yet in order to be consistent";
    1.35 +        return cnt;
    1.36 +    }
    1.37 +}