1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/stateful/src/org/apidesign/stateful/api/ProgressStateful.java Sun May 02 14:35:48 2010 +0200
1.3 @@ -0,0 +1,53 @@
1.4 +package org.apidesign.stateful.api;
1.5 +
1.6 +/** API for notifying progress.
1.7 + *
1.8 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.9 + */
1.10 +// BEGIN: progress.api
1.11 +public abstract class ProgressStateful {
1.12 + public static ProgressStateful create(String name) {
1.13 + return createImpl(name);
1.14 + }
1.15 + public abstract void start(int totalAmount);
1.16 + public abstract void progress(int howMuch);
1.17 + public abstract void finish();
1.18 + // FINISH: progress.api
1.19 +
1.20 + ProgressStateful() {
1.21 + }
1.22 +
1.23 + private static ProgressStateful createImpl(String name) {
1.24 + return new Impl(name);
1.25 + }
1.26 +
1.27 + private static final class Impl extends ProgressStateful {
1.28 + private final String name;
1.29 + private int total = -1;
1.30 + private int current;
1.31 +
1.32 + public Impl(String name) {
1.33 + this.name = name;
1.34 + }
1.35 +
1.36 + @Override
1.37 + public void start(int totalAmount) {
1.38 + total = totalAmount;
1.39 + }
1.40 +
1.41 + @Override
1.42 + public void progress(int howMuch) {
1.43 + if (total == -1) {
1.44 + throw new IllegalStateException("Call start first!");
1.45 + }
1.46 + current = howMuch;
1.47 + }
1.48 +
1.49 + @Override
1.50 + public void finish() {
1.51 + total = -1;
1.52 + current = 0;
1.53 + }
1.54 +
1.55 + }
1.56 +}