001/** 002 * The MIT License (MIT) 003 * 004 * Copyright (c) 2019 nobark (tools4j), Marco Terzer, Anton Anufriev 005 * 006 * Permission is hereby granted, free of charge, to any person obtaining a copy 007 * of this software and associated documentation files (the "Software"), to deal 008 * in the Software without restriction, including without limitation the rights 009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010 * copies of the Software, and to permit persons to whom the Software is 011 * furnished to do so, subject to the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be included in all 014 * copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022 * SOFTWARE. 023 */ 024package org.tools4j.nobark.run; 025 026/** 027 * A factory for a {@link Runnable} that can be stopped by complying with a 028 * {@link RunnableFactory.RunningCondition RunningCondition} that is passed to the factory method upon construction of 029 * the runnable. 030 */ 031@FunctionalInterface 032public interface RunnableFactory { 033 034 /** 035 * Creates a runnable that stops when the supplied {@code runningCondition} returns false. THe runnable is expected 036 * to perform some iterative function and it is obliged to check the running condition regularly and return from the 037 * {@link Runnable#run() run()} method when the running condition returns false. 038 * 039 * @param runningCondition the condition to be checked regularly by the returned runnable telling it either to 040 * continue or to abort 041 * @return a new runnable that complies with the supplied running condition 042 */ 043 Runnable create(RunningCondition runningCondition); 044 045 /** 046 * Condition for a runnable created by {@link RunnableFactory#create(RunningCondition)} telling the runnable when 047 * to stop. 048 */ 049 @FunctionalInterface 050 interface RunningCondition { 051 /** 052 * Returns true if the runnable should continue its work and false if it should abort and return from te 053 * {@link Runnable#run() run()} method. 054 * 055 * @return true to keep running and false to abort 056 */ 057 boolean keepRunning(); 058 } 059}