samples/stateful/src/org/apidesign/stateful/api/ProgressStateless.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
jtulach@351
     1
package org.apidesign.stateful.api;
jtulach@351
     2
jtulach@351
     3
/** API for notifying progress that cannot call
jtulach@351
     4
 * {@link #start(int)} and {@link InProgress#progress(int)} in wrong
jtulach@351
     5
 * order. In contract to {@link ProgressStateful} class which allows that.
jtulach@351
     6
 *
jtulach@351
     7
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@351
     8
 */
jtulach@351
     9
public abstract class ProgressStateless {
jtulach@351
    10
// BEGIN: progress.phases
jtulach@351
    11
    public static ProgressStateless create(String name) {
jtulach@351
    12
        return createImpl(name);
jtulach@351
    13
    }
jtulach@351
    14
    public abstract InProgress start(int totalAmount);
jtulach@351
    15
jtulach@351
    16
    public abstract class InProgress {
jtulach@351
    17
        public abstract void progress(int howMuch);
jtulach@351
    18
        public abstract void finish();
jtulach@351
    19
        // FINISH: progress.phases
jtulach@351
    20
jtulach@351
    21
        InProgress() {
jtulach@351
    22
        }
jtulach@351
    23
    }
jtulach@351
    24
jtulach@351
    25
jtulach@351
    26
    ProgressStateless() {
jtulach@351
    27
    }
jtulach@351
    28
    
jtulach@351
    29
    private static ProgressStateless createImpl(String name) {
jtulach@351
    30
        return new Impl(name);
jtulach@351
    31
    }
jtulach@351
    32
jtulach@351
    33
    private static final class Impl extends ProgressStateless {
jtulach@351
    34
        private final String name;
jtulach@351
    35
jtulach@351
    36
        public Impl(String name) {
jtulach@351
    37
            this.name = name;
jtulach@351
    38
        }
jtulach@351
    39
jtulach@351
    40
        @Override
jtulach@351
    41
        public InProgress start(int totalAmount) {
jtulach@351
    42
            return new InImpl(totalAmount);
jtulach@351
    43
        }
jtulach@351
    44
jtulach@351
    45
        private class InImpl extends InProgress {
jtulach@351
    46
            private final int total;
jtulach@351
    47
            private int current;
jtulach@351
    48
jtulach@351
    49
            public InImpl(int total) {
jtulach@351
    50
                this.total = total;
jtulach@351
    51
            }
jtulach@351
    52
jtulach@351
    53
            @Override
jtulach@351
    54
            public void progress(int howMuch) {
jtulach@351
    55
                current = howMuch;
jtulach@351
    56
            }
jtulach@351
    57
jtulach@351
    58
            @Override
jtulach@351
    59
            public void finish() {
jtulach@351
    60
                current = total;
jtulach@351
    61
            }
jtulach@351
    62
        }
jtulach@351
    63
    }
jtulach@351
    64
}