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