samples/stateful/src/org/apidesign/stateful/api/ProgressStateful.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 02 May 2010 14:35:48 +0200
changeset 350 b8edf9de56c5
parent 348 samples/stateful/src/org/apidesign/stateful/api/Progress.java@26f9a0ec4315
permissions -rw-r--r--
Renaming Progress to ProgressStateful
jtulach@348
     1
package org.apidesign.stateful.api;
jtulach@348
     2
jtulach@348
     3
/** API for notifying progress.
jtulach@348
     4
 *
jtulach@348
     5
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@348
     6
 */
jtulach@348
     7
// BEGIN: progress.api
jtulach@350
     8
public abstract class ProgressStateful {
jtulach@350
     9
    public static ProgressStateful create(String name) {
jtulach@348
    10
        return createImpl(name);
jtulach@348
    11
    }
jtulach@348
    12
    public abstract void start(int totalAmount);
jtulach@348
    13
    public abstract void progress(int howMuch);
jtulach@348
    14
    public abstract void finish();
jtulach@348
    15
    // FINISH: progress.api
jtulach@348
    16
jtulach@350
    17
    ProgressStateful() {
jtulach@348
    18
    }
jtulach@348
    19
    
jtulach@350
    20
    private static ProgressStateful createImpl(String name) {
jtulach@348
    21
        return new Impl(name);
jtulach@348
    22
    }
jtulach@348
    23
jtulach@350
    24
    private static final class Impl extends ProgressStateful {
jtulach@348
    25
        private final String name;
jtulach@348
    26
        private int total = -1;
jtulach@348
    27
        private int current;
jtulach@348
    28
jtulach@348
    29
        public Impl(String name) {
jtulach@348
    30
            this.name = name;
jtulach@348
    31
        }
jtulach@348
    32
jtulach@348
    33
        @Override
jtulach@348
    34
        public void start(int totalAmount) {
jtulach@348
    35
            total = totalAmount;
jtulach@348
    36
        }
jtulach@348
    37
jtulach@348
    38
        @Override
jtulach@348
    39
        public void progress(int howMuch) {
jtulach@348
    40
            if (total == -1) {
jtulach@348
    41
                throw new IllegalStateException("Call start first!");
jtulach@348
    42
            }
jtulach@348
    43
            current = howMuch;
jtulach@348
    44
        }
jtulach@348
    45
jtulach@348
    46
        @Override
jtulach@348
    47
        public void finish() {
jtulach@348
    48
            total = -1;
jtulach@348
    49
            current = 0;
jtulach@348
    50
        }
jtulach@348
    51
jtulach@348
    52
    }
jtulach@348
    53
}