samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java
changeset 114 c479228609ea
child 204 bd5089d23771
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java	Sat Jun 14 09:54:39 2008 +0200
     1.3 @@ -0,0 +1,66 @@
     1.4 +package org.apidesign.reentrant;
     1.5 +
     1.6 +import java.util.Collection;
     1.7 +import java.util.concurrent.atomic.AtomicInteger;
     1.8 +
     1.9 +public class CriticalSectionReentrantImmutable<T extends Comparable<T>> implements CriticalSection<T> {
    1.10 +    private ImmutableData<T> data = new ImmutableData<T>();
    1.11 +    
    1.12 +    public void assignPilot(T pilot) {
    1.13 +        data = data.newPilot(pilot);
    1.14 +    }
    1.15 +
    1.16 +    // BEGIN: reentrant.merge.immutable
    1.17 +    public int sumBigger(Collection<T> args) {
    1.18 +        for (;;) {
    1.19 +            ImmutableData<T> previous = this.data;
    1.20 +            int[] ret = { 0 };
    1.21 +            ImmutableData<T> now = doCriticalSection(args, previous, ret);
    1.22 +            
    1.23 +            synchronized (this) {
    1.24 +                // if there was no parallel or reentrant change, 
    1.25 +                // apply and return. Otherwise try once more.
    1.26 +                if (previous == this.data) {
    1.27 +                    this.data = now;
    1.28 +                    return ret[0];
    1.29 +                }
    1.30 +            }
    1.31 +        }
    1.32 +    }
    1.33 +    // END: reentrant.merge.immutable
    1.34 +    
    1.35 +    private ImmutableData<T> doCriticalSection(Collection<T> args, ImmutableData<T> data, int[] result) {
    1.36 +        result[0] = 0;
    1.37 +        for (T cmp : args) {
    1.38 +            if (data.pilot.compareTo(cmp) < 0) {
    1.39 +                result[0]++;
    1.40 +            }
    1.41 +        }
    1.42 +        return data.newCnt(data.cnt + result[0]);
    1.43 +    }
    1.44 +
    1.45 +    public int getCount() {
    1.46 +        return data.cnt;
    1.47 +    }
    1.48 +    
    1.49 +    private static final class ImmutableData<T> {
    1.50 +        final T pilot;
    1.51 +        final int cnt;
    1.52 +        
    1.53 +        public ImmutableData() {
    1.54 +            pilot = null;
    1.55 +            cnt = 0;
    1.56 +        }
    1.57 +        private ImmutableData(T pilot, int cnt) {
    1.58 +            this.pilot = pilot;
    1.59 +            this.cnt = cnt;
    1.60 +        }
    1.61 +        
    1.62 +        public ImmutableData<T> newPilot(T pilot) {
    1.63 +            return new ImmutableData(pilot, this.cnt);
    1.64 +        }
    1.65 +        public ImmutableData<T> newCnt(int cnt) {
    1.66 +            return new ImmutableData(this.pilot, cnt);
    1.67 +        }
    1.68 +    }
    1.69 +}