# HG changeset patch # User Jaroslav Tulach # Date 1272804993 -7200 # Node ID efecc4bc0321067315812911208db5e95208f22a # Parent b8edf9de56c5c10186577e769f1d168c04860fcd Showing stateless "phased" API for Progress diff -r b8edf9de56c5 -r efecc4bc0321 samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java Sun May 02 14:56:33 2010 +0200 @@ -0,0 +1,64 @@ +package org.apidesign.stateful.api; + +/** API for notifying progress that cannot call + * {@link #start(int)} and {@link InProgress#progress(int)} in wrong + * order. In contract to {@link ProgressStateful} class which allows that. + * + * @author Jaroslav Tulach + */ +public abstract class ProgressStateless { +// BEGIN: progress.phases + public static ProgressStateless create(String name) { + return createImpl(name); + } + public abstract InProgress start(int totalAmount); + + public abstract class InProgress { + public abstract void progress(int howMuch); + public abstract void finish(); + // FINISH: progress.phases + + InProgress() { + } + } + + + ProgressStateless() { + } + + private static ProgressStateless createImpl(String name) { + return new Impl(name); + } + + private static final class Impl extends ProgressStateless { + private final String name; + + public Impl(String name) { + this.name = name; + } + + @Override + public InProgress start(int totalAmount) { + return new InImpl(totalAmount); + } + + private class InImpl extends InProgress { + private final int total; + private int current; + + public InImpl(int total) { + this.total = total; + } + + @Override + public void progress(int howMuch) { + current = howMuch; + } + + @Override + public void finish() { + current = total; + } + } + } +} diff -r b8edf9de56c5 -r efecc4bc0321 samples/stateful/test/org/apidesign/stateful/api/ProgressStatefulTest.java --- a/samples/stateful/test/org/apidesign/stateful/api/ProgressStatefulTest.java Sun May 02 14:35:48 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package org.apidesign.stateful.api; - -import junit.framework.TestCase; - -public class ProgressStatefulTest extends TestCase { - - public ProgressStatefulTest(String testName) { - super(testName); - } - - public void testProgressWithoutStart() { - try { - // BEGIN: progress.wrong.order - ProgressStateful p = ProgressStateful.create("WrongOrder"); - p.progress(10); - p.finish(); - // END: progress.wrong.order - - fail("Calling progress without start yields an exception!?"); - } catch (IllegalStateException ex) { - // OK - } - } - -} diff -r b8edf9de56c5 -r efecc4bc0321 samples/stateful/test/org/apidesign/stateful/api/ProgressTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/stateful/test/org/apidesign/stateful/api/ProgressTest.java Sun May 02 14:56:33 2010 +0200 @@ -0,0 +1,34 @@ +package org.apidesign.stateful.api; + +import junit.framework.TestCase; +import org.apidesign.stateful.api.ProgressStateless.InProgress; + +public class ProgressTest extends TestCase { + + public ProgressTest(String testName) { + super(testName); + } + + public void testProgressStatefulWithoutStart() { + try { + // BEGIN: progress.wrong.order + ProgressStateful p = ProgressStateful.create("WrongOrder"); + p.progress(10); + p.finish(); + // END: progress.wrong.order + + fail("Calling progress without start yields an exception!?"); + } catch (IllegalStateException ex) { + // OK + } + } + + public void testProgressStatelessNeedsStart() { + ProgressStateless p = ProgressStateless.create("GoodOrder"); + InProgress progress = p.start(10); + // without calling start(), there is no way to call progress() method + progress.progress(10); + progress.finish(); + } + +}