Showing stateless "phased" API for Progress
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 02 May 2010 14:56:33 +0200
changeset 351efecc4bc0321
parent 350 b8edf9de56c5
child 352 6c968e7b5a3f
Showing stateless "phased" API for Progress
samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java
samples/stateful/test/org/apidesign/stateful/api/ProgressStatefulTest.java
samples/stateful/test/org/apidesign/stateful/api/ProgressTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java	Sun May 02 14:56:33 2010 +0200
     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 +}
     2.1 --- a/samples/stateful/test/org/apidesign/stateful/api/ProgressStatefulTest.java	Sun May 02 14:35:48 2010 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,25 +0,0 @@
     2.4 -package org.apidesign.stateful.api;
     2.5 -
     2.6 -import junit.framework.TestCase;
     2.7 -
     2.8 -public class ProgressStatefulTest extends TestCase {
     2.9 -    
    2.10 -    public ProgressStatefulTest(String testName) {
    2.11 -        super(testName);
    2.12 -    }
    2.13 -
    2.14 -    public void testProgressWithoutStart() {
    2.15 -        try {
    2.16 -            // BEGIN: progress.wrong.order
    2.17 -            ProgressStateful p = ProgressStateful.create("WrongOrder");
    2.18 -            p.progress(10);
    2.19 -            p.finish();
    2.20 -            // END: progress.wrong.order
    2.21 -            
    2.22 -            fail("Calling progress without start yields an exception!?");
    2.23 -        } catch (IllegalStateException ex) {
    2.24 -            // OK
    2.25 -        }
    2.26 -    }
    2.27 -
    2.28 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/stateful/test/org/apidesign/stateful/api/ProgressTest.java	Sun May 02 14:56:33 2010 +0200
     3.3 @@ -0,0 +1,34 @@
     3.4 +package org.apidesign.stateful.api;
     3.5 +
     3.6 +import junit.framework.TestCase;
     3.7 +import org.apidesign.stateful.api.ProgressStateless.InProgress;
     3.8 +
     3.9 +public class ProgressTest extends TestCase {
    3.10 +    
    3.11 +    public ProgressTest(String testName) {
    3.12 +        super(testName);
    3.13 +    }
    3.14 +
    3.15 +    public void testProgressStatefulWithoutStart() {
    3.16 +        try {
    3.17 +            // BEGIN: progress.wrong.order
    3.18 +            ProgressStateful p = ProgressStateful.create("WrongOrder");
    3.19 +            p.progress(10);
    3.20 +            p.finish();
    3.21 +            // END: progress.wrong.order
    3.22 +            
    3.23 +            fail("Calling progress without start yields an exception!?");
    3.24 +        } catch (IllegalStateException ex) {
    3.25 +            // OK
    3.26 +        }
    3.27 +    }
    3.28 +
    3.29 +    public void testProgressStatelessNeedsStart() {
    3.30 +        ProgressStateless p = ProgressStateless.create("GoodOrder");
    3.31 +        InProgress progress = p.start(10);
    3.32 +        // without calling start(), there is no way to call progress() method
    3.33 +        progress.progress(10);
    3.34 +        progress.finish();
    3.35 +    }
    3.36 +
    3.37 +}