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

org.burningwave.function.Handler Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
/*
 * This file is part of Burningwave Reflection.
 *
 * Author: Roberto Gentili
 *
 * Hosted at: https://github.com/burningwave/reflection
 *
 * --
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2022 Roberto Gentili
 *
 * 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 org.burningwave.function;

import org.burningwave.Throwables;

@SuppressWarnings("unchecked")
public abstract class Handler {

	public static  T get(final ThrowingSupplier supplier) {
		try {
			return supplier.get();
		} catch (final Throwable exc) {
			return Throwables.INSTANCE.throwException(exc);
		}
	}

	public static  T getFirst(final ThrowingSupplier... suppliers) {
		Throwable exception = null;
		for (final ThrowingSupplier supplier : suppliers) {
			try {
				return supplier.get();
			} catch (final Throwable exc) {
				exception = exc;
			}
		}
		return Throwables.INSTANCE.throwException(exception);
	}

    public static  ThrowingTriPredicate and(
		final ThrowingTriPredicate left, final ThrowingTriPredicate right
	) {
        return new ThrowingTriPredicate() {
			@Override
			public boolean test(final P0 p0, final P1 p1, final P2 p2) throws E {
				return left.test(p0, p1, p2) && right.test(p0, p1, p2);
			}
		};
    }

    public static  ThrowingTriPredicate negate(final ThrowingTriPredicate predicate) {
        return new ThrowingTriPredicate() {
			@Override
			public boolean test(final P0 p0, final P1 p1, final P2 p2) throws E {
				return !predicate.test(p0, p1, p2);
			}
		};
    }

    public static  ThrowingTriPredicate or(
		final ThrowingTriPredicate left, final ThrowingTriPredicate right
	) {
        return new ThrowingTriPredicate() {
			@Override
			public boolean test(final P0 p0, final P1 p1, final P2 p2) throws E {
				return left.test(p0, p1, p2) || right.test(p0, p1, p2);
			}
		};
    }

    public static  ThrowingPredicate and(
    	final ThrowingPredicate left,
		final ThrowingPredicate right
	) {
        return new ThrowingPredicate() {
			@Override
			public boolean test(final T t) throws E {
				return left.test(t) && right.test(t);
			}
		};
    }

    public static  ThrowingPredicate negate(final ThrowingPredicate predicate) {
        return new ThrowingPredicate() {
			@Override
			public boolean test(final T t) throws E {
				return !predicate.test(t);
			}
		};
    }

    public static  ThrowingPredicate or(
    	final ThrowingPredicate left,
		final ThrowingPredicate right
	) {
        return new ThrowingPredicate() {
			@Override
			public boolean test(final T t) throws E {
				return left.test(t) || right.test(t);
			}
		};
    }

    public static  ThrowingBiPredicate and(
		final ThrowingBiPredicate left,
		final ThrowingBiPredicate right
	) {
        return new ThrowingBiPredicate() {
			@Override
			public boolean test(final T t, final U u) throws E {
				return left.test(t, u) && right.test(t, u);
			}
		};
    }

    public static   ThrowingBiPredicate negate(
		final ThrowingBiPredicate predicate
	) {
        return new ThrowingBiPredicate() {
			@Override
			public boolean test(final T t, final U u) throws E {
				return !predicate.test(t, u);
			}
		};
    }

    public static  ThrowingBiPredicate or(
		final ThrowingBiPredicate left,
		final ThrowingBiPredicate right
	) {
        return new ThrowingBiPredicate() {
			@Override
			public boolean test(final T t, final U u) throws E {
				return left.test(t, u) || right.test(t, u);
			}
		};
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy