samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java
changeset 409 40cabcdcd2be
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java	Thu Oct 30 21:30:10 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +package org.apidesign.stateful.api;
     1.5 +
     1.6 +/** API for notifying progress that cannot call
     1.7 + * {@link #start(int)} and {@link InProgress#progress(int)} in wrong
     1.8 + * order. In contract to {@link ProgressStateful} class which allows that.
     1.9 + *
    1.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.11 + */
    1.12 +public abstract class ProgressStateless {
    1.13 +// BEGIN: progress.phases
    1.14 +    public static ProgressStateless create(String name) {
    1.15 +        return createImpl(name);
    1.16 +    }
    1.17 +    public abstract InProgress start(int totalAmount);
    1.18 +
    1.19 +    public abstract class InProgress {
    1.20 +        public abstract void progress(int howMuch);
    1.21 +        public abstract void finish();
    1.22 +        // FINISH: progress.phases
    1.23 +
    1.24 +        InProgress() {
    1.25 +        }
    1.26 +    }
    1.27 +
    1.28 +
    1.29 +    ProgressStateless() {
    1.30 +    }
    1.31 +    
    1.32 +    private static ProgressStateless createImpl(String name) {
    1.33 +        return new Impl(name);
    1.34 +    }
    1.35 +
    1.36 +    private static final class Impl extends ProgressStateless {
    1.37 +        private final String name;
    1.38 +
    1.39 +        public Impl(String name) {
    1.40 +            this.name = name;
    1.41 +        }
    1.42 +
    1.43 +        @Override
    1.44 +        public InProgress start(int totalAmount) {
    1.45 +            return new InImpl(totalAmount);
    1.46 +        }
    1.47 +
    1.48 +        private class InImpl extends InProgress {
    1.49 +            private final int total;
    1.50 +            private int current;
    1.51 +
    1.52 +            public InImpl(int total) {
    1.53 +                this.total = total;
    1.54 +            }
    1.55 +
    1.56 +            @Override
    1.57 +            public void progress(int howMuch) {
    1.58 +                current = howMuch;
    1.59 +            }
    1.60 +
    1.61 +            @Override
    1.62 +            public void finish() {
    1.63 +                current = total;
    1.64 +            }
    1.65 +        }
    1.66 +    }
    1.67 +}