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

me.lucko.helper.utils.Delegates Maven / Gradle / Ivy

There is a newer version: 5.6.14
Show newest version
/*
 * This file is part of helper, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) 
 *  Copyright (c) contributors
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *  SOFTWARE.
 */

package me.lucko.helper.utils;

import com.google.common.base.Throwables;

import me.lucko.helper.interfaces.Delegate;
import me.lucko.helper.terminable.Terminable;

import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * A collection of utility methods for delegating Java 8 functions
 */
public final class Delegates {

    public static  Consumer runnableToConsumer(Runnable runnable) {
        return new RunnableToConsumer<>(runnable);
    }

    public static Supplier runnableToSupplier(Runnable runnable) {
        return new RunnableToSupplier<>(runnable);
    }

    public static  Supplier callableToSupplier(Callable callable) {
        return new CallableToSupplier<>(callable);
    }

    public static  BiConsumer consumerToBiConsumerFirst(Consumer consumer) {
        return new ConsumerToBiConsumerFirst<>(consumer);
    }

    public static  BiConsumer consumerToBiConsumerSecond(Consumer consumer) {
        return new ConsumerToBiConsumerSecond<>(consumer);
    }

    public static  BiPredicate predicateToBiPredicateFirst(Predicate predicate) {
        return new PredicateToBiPredicateFirst<>(predicate);
    }

    public static  BiPredicate predicateToBiPredicateSecond(Predicate predicate) {
        return new PredicateToBiPredicateSecond<>(predicate);
    }

    public static  Function consumerToFunction(Consumer consumer) {
        return new ConsumerToFunction<>(consumer);
    }

    public static  Function runnableToFunction(Runnable runnable) {
        return new RunnableToFunction<>(runnable);
    }

    public static Terminable runnableToTerminable(Runnable runnable) {
        return new RunnableToTerminable(runnable);
    }

    private static final class RunnableToConsumer implements Consumer, Delegate {
        private final Runnable delegate;
        private RunnableToConsumer(Runnable delegate) {
            this.delegate = delegate;
        }
        @Override public Runnable getDelegate() { return delegate; }

        @Override
        public void accept(T t) {
            delegate.run();
        }
    }

    private static final class CallableToSupplier implements Supplier, Delegate> {
        private final Callable delegate;
        private CallableToSupplier(Callable delegate) {
            this.delegate = delegate;
        }
        @Override public Callable getDelegate() { return delegate; }

        @Override
        public T get() {
            try {
                return delegate.call();
            } catch (Exception e) {
                // try to propagate the exception
                Throwables.throwIfUnchecked(e);
                throw new RuntimeException(e);
            }
        }
    }

    private static final class RunnableToSupplier implements Supplier, Delegate {
        private final Runnable delegate;
        private RunnableToSupplier(Runnable delegate) {
            this.delegate = delegate;
        }
        @Override public Runnable getDelegate() { return delegate; }

        @Override
        public T get() {
            delegate.run();
            return null;
        }
    }

    private static final class ConsumerToBiConsumerFirst implements BiConsumer, Delegate> {
        private final Consumer delegate;
        private ConsumerToBiConsumerFirst(Consumer delegate) {
            this.delegate = delegate;
        }
        @Override public Consumer getDelegate() { return delegate; }

        @Override
        public void accept(T t, U u) {
            delegate.accept(t);
        }
    }

    private static final class ConsumerToBiConsumerSecond implements BiConsumer, Delegate> {
        private final Consumer delegate;
        private ConsumerToBiConsumerSecond(Consumer delegate) {
            this.delegate = delegate;
        }
        @Override public Consumer getDelegate() { return delegate; }

        @Override
        public void accept(T t, U u) {
            delegate.accept(u);
        }
    }

    private static final class PredicateToBiPredicateFirst implements BiPredicate, Delegate> {
        private final Predicate delegate;
        private PredicateToBiPredicateFirst(Predicate delegate) {
            this.delegate = delegate;
        }
        @Override public Predicate getDelegate() { return delegate; }

        @Override
        public boolean test(T t, U u) {
            return delegate.test(t);
        }
    }

    private static final class PredicateToBiPredicateSecond implements BiPredicate, Delegate> {
        private final Predicate delegate;
        private PredicateToBiPredicateSecond(Predicate delegate) {
            this.delegate = delegate;
        }
        @Override public Predicate getDelegate() { return delegate; }

        @Override
        public boolean test(T t, U u) {
            return delegate.test(u);
        }
    }

    private static final class ConsumerToFunction implements Function, Delegate> {
        private final Consumer delegate;
        private ConsumerToFunction(Consumer delegate) {
            this.delegate = delegate;
        }
        @Override public Consumer getDelegate() { return delegate; }

        @Override
        public R apply(T t) {
            delegate.accept(t);
            return null;
        }
    }

    private static final class RunnableToFunction implements Function, Delegate {
        private final Runnable delegate;
        private RunnableToFunction(Runnable delegate) {
            this.delegate = delegate;
        }
        @Override public Runnable getDelegate() { return delegate; }

        @Override
        public R apply(T t) {
            delegate.run();
            return null;
        }
    }

    private static final class RunnableToTerminable implements Terminable, Delegate {
        private final Runnable delegate;
        private RunnableToTerminable(Runnable delegate) {
            this.delegate = delegate;
        }
        @Override public Runnable getDelegate() { return delegate; }

        @Override
        public boolean terminate() {
            delegate.run();
            return true;
        }

    }

    private Delegates() {
        throw new UnsupportedOperationException("This class cannot be instantiated");
    }
}