001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009package imagingbook.common.util.progress;
010
011/**
012 * A simple example for how to use {@link ProgressReporter} and {@link ProgressMonitor}.
013 *
014 * @see ProgressReporter
015 * @see ProgressMonitor
016 */
017public abstract class ProgressMonitorExample {
018        
019        /**
020         * The task to be monitored (some slow process).
021         */
022        public static class SlowProcess implements ProgressReporter {           
023                int iter = 0;
024                int iterMax = 100;
025                
026                @Override
027                public double getProgress() {
028                        return (double) iter / iterMax;
029                }
030                
031                public void run() {
032                        for (iter = 0; iter < iterMax; iter++) {
033                                try {
034                                        Thread.sleep(100);
035                                } catch (InterruptedException e) {}
036                        }
037                }
038        }
039
040        // --------------------------------------------------------------
041        
042        /**
043         * Main method for demonstration.
044         * @param args ignored
045         */
046        public static void main(String[] args) {
047                SlowProcess process = new SlowProcess();
048                try (ProgressMonitor monitor = new ConsoleProgressMonitor(process)) {   // uses autoStart
049                        monitor.setWaitTime(200);       // check progress every 200 ms
050                        process.run();                          // the task to be monitored
051                }
052
053        }
054        
055}