All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.unkrig.commons.lang.protocol.RunnableUtil Maven / Gradle / Ivy

Go to download

A versatile Java(TM) library that implements many useful container and utility classes.

There is a newer version: 1.1.12
Show newest version

/*
 * de.unkrig.commons - A general-purpose Java class library
 *
 * Copyright (c) 2012, Arno Unkrig
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimer in the documentation and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package de.unkrig.commons.lang.protocol;

import java.util.Collection;

/**
 * Various {@link Runnable}-related utility methods.
 */
public final
class RunnableUtil {

    private
    RunnableUtil() {}

    /** A {@link Runnable} that does simply nothing. */
    public static final Runnable NOP = new Runnable() { @Override public void run() {} };

    /**
     * Runs {@code delegate1}, then {@code delegate2} in the current thread.
     *
     * @return A runnable that runs {@code delegate1} and then {@code delegate2}
     *
     * @see RunnableWhichThrows#run()
     */
    public static  RunnableWhichThrows
    runSequentially(final RunnableWhichThrows delegate1, final RunnableWhichThrows delegate2) {
        return new RunnableWhichThrows() {

            /**
             * @throws EX The exception thrown by {@code delegate1} or {@code delegate2}; if thrown by {@code
             *            delegate1}, then {@code delegate2} is not run
             */
            @Override public void
            run() throws EX {
                delegate1.run();
                delegate2.run();
            }
        };
    }

    /**
     * Runs the given {@code delegates} in the current thread, in the order of the collection's iterator.
     *
     * @return    A runnable that runs all the {@code delegates} in strict sequence
     *
     * @see RunnableWhichThrows#run()
     */
    public static  RunnableWhichThrows
    runSequentially(final Collection> delegates) {
        return new RunnableWhichThrows() {

            /**
             * @throws EX The exception thrown by one of the {@code delegates}; the following runnables are not run
             */
            @Override public void
            run() throws EX {
                for (RunnableWhichThrows delegate : delegates) {
                    delegate.run();
                }
            }
        };
    }

    /**
     * Converts a {@link RunnableWhichThrows} into a {@link Runnable}, which is possible iff the source runnable's
     * exception is a subclass of {@link RuntimeException}.
     *
     * @param  The source runnable's exception
     */
    public static  Runnable
    asRunnable(final RunnableWhichThrows delegate) {

        return new Runnable() { @Override public void run() { delegate.run(); } };
    }

    /**
     * Converts a {@link Runnable} into a {@link RunnableWhichThrows}.
     */
    public static  RunnableWhichThrows
    asRunnableWhichThrows(final Runnable delegate) {

        return new RunnableWhichThrows() { @Override public void run() { delegate.run(); } };
    }

    /**
     * The returned {@link Runnable} runs the delegate iff the {@code condition} evaluates to {@code true}.
     * 

* (The {@code condition} is evaluated with {@code null} as the {@code subject} argument.) * * @param subject The {@code subject} for the {@code condition} */ public static Runnable sparingRunnable(final Runnable delegate, final Predicate condition, final ST subject) { return new Runnable() { @Override public void run() { if (condition.evaluate(subject)) delegate.run(); } }; } /** * The returned {@link Runnable} runs the delegate iff the {@code condition} produces {@link Boolean#TRUE}. * * @see PredicateUtil#after(long) * @see ProducerUtil#every(long) */ public static Runnable sparingRunnable(final Runnable delegate, final Producer condition) { return new Runnable() { @Override public void run() { if (Boolean.TRUE.equals(condition.produce())) delegate.run(); } }; } /** * Runs runnable1 and then runnable2, unless swap is {@code true}, when the * running order is swapped (runnable2, then runnable1). */ public static void swapIf(boolean swap, RunnableWhichThrows runnable1, RunnableWhichThrows runnable2) throws EX { if (swap) { runnable2.run(); runnable1.run(); } else { runnable1.run(); runnable2.run(); } } /** * Wraps the delegate such that its declared exception is caught and ignored. */ public static Runnable ignoreExceptions(final Class exceptionClass, final RunnableWhichThrows delegate) { return new Runnable() { @Override public void run() { try { delegate.run(); } catch (RuntimeException re) { if (!exceptionClass.isAssignableFrom(re.getClass())) throw re; ; } catch (Error e) { // SUPPRESS CHECKSTYLE IllegalCatch if (!exceptionClass.isAssignableFrom(e.getClass())) throw e; ; } catch (Throwable t) { // SUPPRESS CHECKSTYLE IllegalCatch assert exceptionClass.isAssignableFrom(t.getClass()); ; } } }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy