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.loop;
025
026import java.util.Objects;
027import java.util.concurrent.ThreadFactory;
028
029import org.tools4j.nobark.run.ShutdownableThread;
030
031/**
032 * Provider for {@link Step} distinguishing between normal (main loop) steps and shutdown steps that are used during the
033 * termination phase of a {@link ShutdownableThread} as returned by
034 * {@link Loop#start(IdleStrategy, ExceptionHandler, ThreadFactory, StepProvider...) Loop#start(..)}.
035 */
036@FunctionalInterface
037public interface StepProvider {
038    /**
039     * Returns a step for normal or shutdown use.
040     *
041     * @param forShutdown true if the returned step will be used in the shutdown phase, and false otherwise
042     * @return the step, never null
043     */
044    Step provide(boolean forShutdown);
045
046    /**
047     * Returns a normal step using the given supplier.
048     *
049     * @param supplier the supplier that provides the step
050     * @return a step for use during the normal phase
051     */
052    static Step normalStep(final StepProvider supplier) {
053        return supplier.provide(false);
054    }
055
056    /**
057     * Returns a shutdown step using the given supplier.
058     *
059     * @param supplier the supplier that provides the step
060     * @return a step for use during the shutdown phase
061     */
062    static Step shutdownStep(final StepProvider supplier) {
063        return supplier.provide(true);
064    }
065
066
067    /**
068     * Returns a provider for the given step used for both normal and shutdown phase.
069     *
070     * @param step the step to be returned by the provider
071     * @return a provider that always returns the given step
072     */
073    static StepProvider alwaysProvide(final Step step) {
074        Objects.requireNonNull(step);
075        return forShutdown -> step;
076    }
077
078    /**
079     * Returns a provider for the given step used only for the normal phase;  a {@link Step#NO_OP no-OP} is returned for
080     * the shutdown phase.
081     *
082     * @param step the step to be returned by the provider for the normal phase
083     * @return a provider that returns the given step for normal phase and a no-OP for the shutdown phase
084     */
085    static StepProvider silenceDuringShutdown(final Step step) {
086        Objects.requireNonNull(step);
087        return forShutdown -> forShutdown ? Step.NO_OP : step;
088    }
089}