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

com.landawn.abacus.util.Fn Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

The newest version!
/*
 * Copyright (c) 2017, Haiyang Li.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.landawn.abacus.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractQueue;
import java.util.AbstractSet;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.annotation.Stateful;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableArray;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableObjArray;
import com.landawn.abacus.util.Tuple.Tuple1;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.Tuple.Tuple4;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalBoolean;
import com.landawn.abacus.util.u.OptionalByte;
import com.landawn.abacus.util.u.OptionalChar;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalFloat;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.u.OptionalShort;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.ByteBiFunction;
import com.landawn.abacus.util.function.ByteBiPredicate;
import com.landawn.abacus.util.function.ByteBinaryOperator;
import com.landawn.abacus.util.function.ByteConsumer;
import com.landawn.abacus.util.function.ByteFunction;
import com.landawn.abacus.util.function.BytePredicate;
import com.landawn.abacus.util.function.Callable;
import com.landawn.abacus.util.function.CharBiFunction;
import com.landawn.abacus.util.function.CharBiPredicate;
import com.landawn.abacus.util.function.CharBinaryOperator;
import com.landawn.abacus.util.function.CharConsumer;
import com.landawn.abacus.util.function.CharFunction;
import com.landawn.abacus.util.function.CharPredicate;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.DoubleBiFunction;
import com.landawn.abacus.util.function.DoubleBiPredicate;
import com.landawn.abacus.util.function.DoubleBinaryOperator;
import com.landawn.abacus.util.function.DoubleConsumer;
import com.landawn.abacus.util.function.DoubleFunction;
import com.landawn.abacus.util.function.DoublePredicate;
import com.landawn.abacus.util.function.FloatBiFunction;
import com.landawn.abacus.util.function.FloatBiPredicate;
import com.landawn.abacus.util.function.FloatBinaryOperator;
import com.landawn.abacus.util.function.FloatConsumer;
import com.landawn.abacus.util.function.FloatFunction;
import com.landawn.abacus.util.function.FloatPredicate;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntBiFunction;
import com.landawn.abacus.util.function.IntBiObjConsumer;
import com.landawn.abacus.util.function.IntBiObjFunction;
import com.landawn.abacus.util.function.IntBiObjPredicate;
import com.landawn.abacus.util.function.IntBiPredicate;
import com.landawn.abacus.util.function.IntBinaryOperator;
import com.landawn.abacus.util.function.IntConsumer;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.IntObjConsumer;
import com.landawn.abacus.util.function.IntObjFunction;
import com.landawn.abacus.util.function.IntObjPredicate;
import com.landawn.abacus.util.function.IntPredicate;
import com.landawn.abacus.util.function.LongBiFunction;
import com.landawn.abacus.util.function.LongBiPredicate;
import com.landawn.abacus.util.function.LongBinaryOperator;
import com.landawn.abacus.util.function.LongConsumer;
import com.landawn.abacus.util.function.LongFunction;
import com.landawn.abacus.util.function.LongPredicate;
import com.landawn.abacus.util.function.LongSupplier;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.QuadFunction;
import com.landawn.abacus.util.function.Runnable;
import com.landawn.abacus.util.function.ShortBiFunction;
import com.landawn.abacus.util.function.ShortBiPredicate;
import com.landawn.abacus.util.function.ShortBinaryOperator;
import com.landawn.abacus.util.function.ShortConsumer;
import com.landawn.abacus.util.function.ShortFunction;
import com.landawn.abacus.util.function.ShortPredicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToBooleanFunction;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
import com.landawn.abacus.util.function.TriConsumer;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
import com.landawn.abacus.util.function.UnaryOperator;

/**
 * Factory utility class for functional interfaces.
 *
 * 
* Note: Usually you shouldn't cache or reuse any Function/Predicate/Consumer/... created by calling the methods in this class. * These methods should be called every time. *
* *
 * 
 *
 * {@code Map map = N.asMap("a", 1, "b", 2, "c", 3);}
 * // Instead of
 * {@code Stream.of(map).filter(e -> e.getKey().equals("a") || e.getKey().equals("b")).toMap(e -> e.getKey(), e -> e.getValue());}
 * // Using Fn
 * {@code Stream.of(map).filter(Fn.testByKey(k -> k.equals("a") || k.equals("b"))).collect(Collectors.toMap());}
 *
 * 
 * 
* * * * */ @SuppressWarnings({ "java:S6539", "java:S1192", "java:S1221", "java:S1452", "java:S2445" }) public final class Fn { private static final Object NONE = ClassUtil.createNullMask(); private static final Timer timer = new Timer(); // @SuppressWarnings("rawtypes") // public static final IntFunction> FACTORY_OF_MAP = (IntFunction) Factory.MAP_FACTORY; // // @SuppressWarnings("rawtypes") // public static final IntFunction> FACTORY_OF_LINKED_HASH_MAP = (IntFunction) Factory.LINKED_HASH_MAP_FACTORY; // // @SuppressWarnings("rawtypes") // public static final Supplier> SUPPLIER_OF_MAP = (Supplier) Suppliers.MAP; // // @SuppressWarnings("rawtypes") // public static final Supplier> SUPPLIER_OF_LINKED_HASH_MAP = (Supplier) Suppliers.LINKED_HASH_MAP; private static final Runnable EMPTY_ACTION = () -> { }; @SuppressWarnings("rawtypes") private static final Consumer DO_NOTHING = value -> { // do nothing. }; private static final Consumer CLOSE = IOUtil::close; private static final Consumer CLOSE_QUIETLY = IOUtil::closeQuietly; @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_EQUAL = (key, value) -> N.println(Strings.concat(N.toString(key), "=", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_HYPHEN = (key, value) -> N.println(Strings.concat(N.toString(key), "-", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_UNDERSCORE = (key, value) -> N.println(Strings.concat(N.toString(key), "_", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COLON = (key, value) -> N.println(Strings.concat(N.toString(key), ":", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COLON_SPACE = (key, value) -> N.println(Strings.concat(N.toString(key), ": ", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COMMA = (key, value) -> N.println(Strings.concat(N.toString(key), ",", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COMMA_SPACE = (key, value) -> N.println(Strings.concat(N.toString(key), ", ", N.toString(value))); @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_EMPTY = (key, value) -> N.println(Strings.concat(N.toString(key), N.toString(value))); @SuppressWarnings("rawtypes") private static final Consumer PRINTLN = N::println; @SuppressWarnings("rawtypes") private static final Function TO_STRING = N::toString; private static final UnaryOperator TO_CAMEL_CASE = Strings::toCamelCase; private static final UnaryOperator TO_LOWER_CASE = Strings::toLowerCase; private static final UnaryOperator TO_LOWER_CASE_WITH_UNDERSCORE = Strings::toLowerCaseWithUnderscore; private static final UnaryOperator TO_UPPER_CASE = Strings::toUpperCase; private static final UnaryOperator TO_UPPER_CASE_WITH_UNDERSCORE = Strings::toUpperCaseWithUnderscore; private static final Function TO_RUNTIME_EXCEPTION = e -> ExceptionUtil.toRuntimeException(e, true); @SuppressWarnings("rawtypes") private static final BiFunction COMPARE = N::compare; @SuppressWarnings("rawtypes") private static final Function IDENTITY = t -> t; private static final UnaryOperator TRIM = Strings::trim; private static final UnaryOperator TRIM_TO_EMPTY = Strings::trimToEmpty; private static final UnaryOperator TRIM_TO_NULL = Strings::trimToNull; private static final UnaryOperator STRIP = Strings::strip; private static final UnaryOperator STRIP_TO_EMPTY = Strings::stripToEmpty; private static final UnaryOperator STRIP_TO_NULL = Strings::stripToNull; private static final UnaryOperator NULL_TO_EMPTY = t -> t == null ? Strings.EMPTY_STRING : t; @SuppressWarnings("rawtypes") private static final UnaryOperator NULL_TO_EMPTY_LIST = t -> t == null ? N.emptyList() : t; @SuppressWarnings("rawtypes") private static final UnaryOperator NULL_TO_EMPTY_SET = t -> t == null ? N.emptySet() : t; @SuppressWarnings("rawtypes") private static final UnaryOperator NULL_TO_EMPTY_MAP = t -> t == null ? N.emptyMap() : t; private static final Function LENGTH = t -> t == null ? 0 : t.length(); private static final Function LEN = t -> t == null ? 0 : t.length; @SuppressWarnings("rawtypes") private static final Function SIZE = t -> t == null ? 0 : t.size(); @SuppressWarnings("rawtypes") private static final Function SIZE_MAP = t -> t == null ? 0 : t.size(); private static final Function, Object> KEY = Entry::getKey; private static final Function, Object> VALUE = Entry::getValue; private static final Function, Object> LEFT = Pair::getLeft; private static final Function, Object> RIGHT = Pair::getRight; private static final Function, Map.Entry> INVERSE = t -> new ImmutableEntry<>(t.getValue(), t.getKey()); private static final BiFunction> ENTRY = ImmutableEntry::new; private static final BiFunction> PAIR = Pair::of; private static final TriFunction> TRIPLE = Triple::of; private static final Function> TUPLE_1 = Tuple::of; private static final BiFunction> TUPLE_2 = Tuple::of; private static final TriFunction> TUPLE_3 = Tuple::of; private static final QuadFunction> TUPLE_4 = Tuple::of; @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_TRUE = value -> true; @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_FALSE = value -> false; @SuppressWarnings("rawtypes") private static final Predicate IS_NULL = Objects::isNull; private static final Predicate IS_EMPTY = Strings::isEmpty; private static final Predicate IS_BLANK = Strings::isBlank; @SuppressWarnings("rawtypes") private static final Predicate NOT_NULL = Objects::nonNull; private static final Predicate IS_NOT_EMPTY = Strings::isNotEmpty; private static final Predicate IS_NOT_BLANK = Strings::isNotBlank; private static final Predicate IS_FILE = file -> file != null && file.isFile(); private static final Predicate IS_DIRECTORY = file -> file != null && file.isDirectory(); public static final ToBooleanFunction GET_AS_BOOLEAN = OptionalBoolean::get; public static final ToCharFunction GET_AS_CHAR = OptionalChar::get; public static final ToByteFunction GET_AS_BYTE = OptionalByte::get; public static final ToShortFunction GET_AS_SHORT = OptionalShort::get; public static final ToIntFunction GET_AS_INT = OptionalInt::get; public static final ToLongFunction GET_AS_LONG = OptionalLong::get; public static final ToFloatFunction GET_AS_FLOAT = OptionalFloat::get; public static final ToDoubleFunction GET_AS_DOUBLE = OptionalDouble::get; public static final ToIntFunction GET_AS_INT_JDK = java.util.OptionalInt::getAsInt; public static final ToLongFunction GET_AS_LONG_JDK = java.util.OptionalLong::getAsLong; public static final ToDoubleFunction GET_AS_DOUBLE_JDK = java.util.OptionalDouble::getAsDouble; public static final Predicate IS_PRESENT_BOOLEAN = OptionalBoolean::isPresent; public static final Predicate IS_PRESENT_CHAR = OptionalChar::isPresent; public static final Predicate IS_PRESENT_BYTE = OptionalByte::isPresent; public static final Predicate IS_PRESENT_SHORT = OptionalShort::isPresent; public static final Predicate IS_PRESENT_INT = OptionalInt::isPresent; public static final Predicate IS_PRESENT_LONG = OptionalLong::isPresent; public static final Predicate IS_PRESENT_FLOAT = OptionalFloat::isPresent; public static final Predicate IS_PRESENT_DOUBLE = OptionalDouble::isPresent; public static final Predicate IS_PRESENT_INT_JDK = java.util.OptionalInt::isPresent; public static final Predicate IS_PRESENT_LONG_JDK = java.util.OptionalLong::isPresent; public static final Predicate IS_PRESENT_DOUBLE_JDK = java.util.OptionalDouble::isPresent; Fn() { // for extension. } // /** // * // * @param // * @param supplier // * @return // */ // public static T get(final java.util.function.Supplier supplier) { // return supplier.get(); // } /** * Returns a {@code Supplier} which returns a single instance created by calling the specified {@code supplier.get()}. * * @param * @param supplier * @return */ public static Supplier memoize(final java.util.function.Supplier supplier) { return LazyInitializer.of(supplier); } // Copied from Google Guava under Apache License v2. /** * Copied from Google Guava under Apache License v2. *
*
* * Returns a supplier that caches the instance supplied by the delegate and removes the cached * value after the specified time has passed. Subsequent calls to {@code get()} return the cached * value if the expiration time has not passed. After the expiration time, a new value is * retrieved, cached, and returned. See: memoization * *

The returned supplier is thread-safe. The supplier's serialized form does not contain the * cached value, which will be recalculated when {@code get()} is called on the reserialized * instance. The actual memoization does not happen when the underlying delegate throws an * exception. * *

When the underlying delegate throws an exception then this memorizing supplier will keep * delegating calls until it returns valid data. * * @param * @param supplier * @param duration the length of time after a value is created that it should stop being returned * by subsequent {@code get()} calls * @param unit the unit that {@code duration} is expressed in * @return * @throws IllegalArgumentException if {@code duration} is not positive */ public static Supplier memoizeWithExpiration(final java.util.function.Supplier supplier, final long duration, final TimeUnit unit) throws IllegalArgumentException { N.checkArgNotNull(supplier, cs.Supplier); N.checkArgument(duration > 0, "duration (%s %s) must be > 0", duration, unit); return new Supplier<>() { private final java.util.function.Supplier delegate = supplier; private final long durationNanos = unit.toNanos(duration); private volatile T value; // The special value 0 means "not yet initialized". private volatile long expirationNanos = 0; @Override public T get() { // Another variant of Double-Checked Locking. // // We use two volatile reads. We could reduce this to one by // putting our fields into a holder class, but (at least on x86) // the extra memory consumption and indirection are more // expensive than the extra volatile reads. long nanos = expirationNanos; final long now = System.nanoTime(); if (nanos == 0 || now - nanos >= 0) { synchronized (this) { if (nanos == expirationNanos) { // recheck for lost race final T t = delegate.get(); value = t; nanos = now + durationNanos; // In the very unlikely event that nanos is 0, set it to 1; // no one will notice 1 ns of tardiness. expirationNanos = (nanos == 0) ? 1 : nanos; return t; } } } // This is safe because we checked `expirationNanos`. return value; } }; } // /** // * // * @param // * @param // * @param func // * @return // */ // @Beta // @SequentialOnly // @Stateful // public static IntFunction memoize(final IntFunction func) { // return new IntFunction() { // private volatile R resultForNull = (R) NONE; // // @Override // public R apply(int t) { // R result = resultForNull; // // if (result == NONE) { // synchronized (this) { // if (resultForNull == NONE) { // resultForNull = func.apply(t); // } // // result = resultForNull; // } // } // // return result; // } // }; // } /** * * @param * @param * @param func * @return */ public static Function memoize(final java.util.function.Function func) { return new Function<>() { private final R none = (R) NONE; private final Map resultMap = new ConcurrentHashMap<>(); private volatile R resultForNull = none; //NOSONAR @SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE") @Override public R apply(final T t) { R result = null; if (t == null) { result = resultForNull; if (result == none) { synchronized (this) { if (resultForNull == none) { resultForNull = func.apply(null); } result = resultForNull; } } } else { result = resultMap.get(t); if (result == null) { result = func.apply(t); resultMap.put(t, result == null ? none : result); } } return result == none ? null : result; } }; } // /** // * Only for temporary use in sequential stream/single thread, not for parallel stream/multiple threads. // * The returned Collection will clean up before it's returned every time when {@code get} is called. // * Don't save the returned Collection object or use it to save objects. // * // * @param // * @param // * @param supplier // * @return // * @see {@code Stream.split/sliding}; // * @deprecated // */ // @Deprecated // @Beta // @SequentialOnly // @Stateful // public static > Supplier reuse(final java.util.function.Supplier supplier) { // return new Supplier<>() { // private C c; // // @Override // public C get() { // if (c == null) { // c = supplier.get(); // } else if (c.size() > 0) { // c.clear(); // } // // return c; // } // }; // } // // /** // * Only for temporary use in sequential stream/single thread, not for parallel stream/multiple threads. // * The returned Collection will clean up before it's returned every time when {@code get} is called. // * Don't save the returned Collection object or use it to save objects. // * // * @param // * @param // * @param supplier // * @return // * @see {@code Stream.split/sliding}; // * @deprecated // */ // @Deprecated // @Beta // @SequentialOnly // @Stateful // public static > IntFunction reuse(final java.util.function.IntFunction supplier) { // return new IntFunction<>() { // private C c; // // @Override // public C apply(int size) { // if (c == null) { // c = supplier.apply(size); // } else if (c.size() > 0) { // c.clear(); // } // // return c; // } // }; // } /** * * @param closeable * @return */ public static Runnable close(final AutoCloseable closeable) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.close(closeable); } }; } /** * * @param a * @return */ @SafeVarargs public static Runnable closeAll(final AutoCloseable... a) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAll(a); } }; } /** * * @param c * @return */ public static Runnable closeAll(final Collection c) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAll(c); } }; } /** * * @param closeable * @return */ public static Runnable closeQuietly(final AutoCloseable closeable) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeQuietly(closeable); } }; } /** * Close all quietly. * * @param a * @return */ @SafeVarargs public static Runnable closeAllQuietly(final AutoCloseable... a) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAllQuietly(a); } }; } /** * Close all quietly. * * @param c * @return */ public static Runnable closeAllQuietly(final Collection c) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAllQuietly(c); } }; } public static Runnable emptyAction() { return EMPTY_ACTION; } /** * * @param service * @return */ public static Runnable shutDown(final ExecutorService service) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; service.shutdown(); } }; } /** * * @param service * @param terminationTimeout * @param timeUnit * @return */ public static Runnable shutDown(final ExecutorService service, final long terminationTimeout, final TimeUnit timeUnit) { return new Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; try { service.shutdown(); //noinspection ResultOfMethodCallIgnored service.awaitTermination(terminationTimeout, timeUnit); } catch (final InterruptedException e) { // ignore. } } }; } /** * * @param * @return */ public static Consumer doNothing() { return DO_NOTHING; } /** * * @param * @param errorMessage * @return */ public static Consumer throwRuntimeException(final String errorMessage) { return t -> { throw new RuntimeException(errorMessage); }; } /** * * @param * @param exceptionSupplier * @return */ public static Consumer throwException(final java.util.function.Supplier exceptionSupplier) { return t -> { throw exceptionSupplier.get(); }; } public static Function toRuntimeException() { return TO_RUNTIME_EXCEPTION; } /** * * @param * @return */ public static Consumer close() { return (Consumer) CLOSE; } /** * * @param * @return */ public static Consumer closeQuietly() { return (Consumer) CLOSE_QUIETLY; } /** * * @param * @param millis * @return */ public static Consumer sleep(final long millis) { return t -> N.sleep(millis); } /** * * @param * @param millis * @return */ public static Consumer sleepUninterruptibly(final long millis) { return t -> N.sleepUninterruptibly(millis); } /** * Returns a stateful {@code Consumer}. Don't save or cache for reuse * * @param * @param permitsPerSecond * @return * @see RateLimiter#acquire() * @see RateLimiter#create(double) */ @Stateful public static Consumer rateLimiter(final double permitsPerSecond) { return rateLimiter(RateLimiter.create(permitsPerSecond)); } /** * Returns a stateful {@code Consumer}. Don't save or cache for reuse * * @param * @param rateLimiter * @return * @see RateLimiter#acquire() */ @Stateful public static Consumer rateLimiter(final RateLimiter rateLimiter) { return t -> rateLimiter.acquire(); } /** * * @param * @return */ public static Consumer println() { return PRINTLN; } /** * * @param * @param * @param separator * @return * @throws IllegalArgumentException */ public static BiConsumer println(final String separator) throws IllegalArgumentException { N.checkArgNotNull(separator); switch (separator) { // NOSONAR case "=": return PRINTLN_EQUAL; case ":": return PRINTLN_COLON; case ": ": return PRINTLN_COLON_SPACE; case "-": return PRINTLN_HYPHEN; case "_": return PRINTLN_UNDERSCORE; case ",": return PRINTLN_COMMA; case ", ": return PRINTLN_COMMA_SPACE; case "": return PRINTLN_EMPTY; default: return (t, u) -> N.println(t + separator + u); } } /** * * @param * @return */ public static Function toStr() { return TO_STRING; } /** * To camel case. * * @return */ public static UnaryOperator toCamelCase() { return TO_CAMEL_CASE; } /** * To lower case. * * @return */ public static UnaryOperator toLowerCase() { return TO_LOWER_CASE; } /** * To lower case with underscore. * * @return */ public static UnaryOperator toLowerCaseWithUnderscore() { return TO_LOWER_CASE_WITH_UNDERSCORE; } /** * To upper case. * * @return */ public static UnaryOperator toUpperCase() { return TO_UPPER_CASE; } /** * To upper case with underscore. * * @return */ public static UnaryOperator toUpperCaseWithUnderscore() { return TO_UPPER_CASE_WITH_UNDERSCORE; } @SuppressWarnings("rawtypes") private static final Function TO_JSON = N::toJson; /** * * @param * @return */ public static Function toJson() { return TO_JSON; } @SuppressWarnings("rawtypes") private static final Function TO_XML = N::toXml; /** * * @param * @return */ public static Function toXml() { return TO_XML; } /** * * @param * @return */ public static Function identity() { return IDENTITY; } /** * * @param the key type * @param * @param keyExtractor * @return * @throws IllegalArgumentException */ public static Function> keyed(final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); return t -> Keyed.of(keyExtractor.apply(t), t); } private static final Function, Object> VAL = Keyed::val; private static final Function, Object>, Object> KK_VAL = t -> t.getKey().val(); /** * * @param the key type * @param * @return */ @SuppressWarnings("rawtypes") public static Function, T> val() { return (Function) VAL; } /** * * @param * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Function, V>, T> kkv() { return (Function) KK_VAL; } private static final Function> WRAP = Wrapper::of; /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function> wrap() { return (Function) WRAP; } /** * * @param * @param hashFunction * @param equalsFunction * @return * @throws IllegalArgumentException */ public static Function> wrap(final java.util.function.ToIntFunction hashFunction, final java.util.function.BiPredicate equalsFunction) throws IllegalArgumentException { N.checkArgNotNull(hashFunction); N.checkArgNotNull(equalsFunction); return t -> Wrapper.of(t, hashFunction, equalsFunction); } private static final Function, Object> UNWRAP = Wrapper::value; /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, T> unwrap() { return (Function) UNWRAP; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Function, K> key() { return (Function) KEY; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Function, V> value() { return (Function) VALUE; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Function, L> left() { return (Function) LEFT; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Function, R> right() { return (Function) RIGHT; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Function, Entry> inverse() { return (Function) INVERSE; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static BiFunction> entry() { return (BiFunction) ENTRY; } /** * * @param the key type * @param the value type * @param key * @return * @deprecated replaced by {@code Fn#entryWithKey(Object)} */ @Deprecated public static Function> entry(final K key) { return entryWithKey(key); } /** * * @param the key type * @param the value type * @param keyExtractor * @return * @deprecated replaced by {@code Fn#entryByKeyMapper(Function)} */ @Deprecated public static Function> entry(final java.util.function.Function keyExtractor) { return entryByKeyMapper(keyExtractor); } /** * * @param the key type * @param the value type * @param key * @return */ public static Function> entryWithKey(final K key) { return v -> new ImmutableEntry<>(key, v); } /** * * @param the key type * @param the value type * @param keyExtractor * @return * @throws IllegalArgumentException */ public static Function> entryByKeyMapper(final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); return v -> new ImmutableEntry<>(keyExtractor.apply(v), v); } /** * * @param the key type * @param the value type * @param value * @return */ public static Function> entryWithValue(final V value) { return k -> new ImmutableEntry<>(k, value); } /** * * @param the key type * @param the value type * @param valueExtractor * @return * @throws IllegalArgumentException */ public static Function> entryByValueMapper(final java.util.function.Function valueExtractor) throws IllegalArgumentException { N.checkArgNotNull(valueExtractor); return k -> new ImmutableEntry<>(k, valueExtractor.apply(k)); } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static BiFunction> pair() { return (BiFunction) PAIR; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static TriFunction> triple() { return (TriFunction) TRIPLE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function> tuple1() { return (Function) TUPLE_1; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static BiFunction> tuple2() { return (BiFunction) TUPLE_2; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static TriFunction> tuple3() { return (TriFunction) TUPLE_3; } /** * * @param * @param * @param * @param * @return */ @SuppressWarnings({ "rawtypes" }) public static QuadFunction> tuple4() { return (QuadFunction) TUPLE_4; } public static UnaryOperator trim() { return TRIM; } public static UnaryOperator trimToEmpty() { return TRIM_TO_EMPTY; } public static UnaryOperator trimToNull() { return TRIM_TO_NULL; } public static UnaryOperator strip() { return STRIP; } public static UnaryOperator stripToEmpty() { return STRIP_TO_EMPTY; } public static UnaryOperator stripToNull() { return STRIP_TO_NULL; } public static UnaryOperator nullToEmpty() { return NULL_TO_EMPTY; } /** * * @param * @return * @deprecated replaced by {@code nullToEmptyList} */ @Deprecated @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptyL() { return (UnaryOperator) NULL_TO_EMPTY_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptyList() { return (UnaryOperator) NULL_TO_EMPTY_LIST; } /** * * @param * @return * @deprecated replaced by {@code nullToEmptySet} */ @Deprecated @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptyS() { return (UnaryOperator) NULL_TO_EMPTY_SET; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptySet() { return (UnaryOperator) NULL_TO_EMPTY_SET; } /** * * @param the key type * @param the value type * @return * @deprecated replaced by {@code nullToEmptyMap} */ @Deprecated @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptyM() { return (UnaryOperator) NULL_TO_EMPTY_MAP; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static UnaryOperator> nullToEmptyMap() { return (UnaryOperator) NULL_TO_EMPTY_MAP; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function len() { return (Function) LEN; } /** * * @param * @return */ public static Function length() { return (Function) LENGTH; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function size() { return (Function) SIZE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function sizeM() { return (Function) SIZE_MAP; } /** * * @param * @param * @param clazz * @return * @throws IllegalArgumentException */ public static Function cast(final Class clazz) throws IllegalArgumentException { N.checkArgNotNull(clazz); return t -> (U) t; } /** * * @param * @return */ public static Predicate alwaysTrue() { return ALWAYS_TRUE; } /** * * @param * @return */ public static Predicate alwaysFalse() { return ALWAYS_FALSE; } /** * Checks if is {@code null}. * * @param * @return */ public static Predicate isNull() { return IS_NULL; } /** * Checks if is {@code null}. * * @param * @param valueExtractor * @return */ public static Predicate isNull(final java.util.function.Function valueExtractor) { return t -> valueExtractor.apply(t) == null; } /** * Checks if is {@code null} or empty. * * @param * @return */ public static Predicate isEmpty() { return (Predicate) IS_EMPTY; } /** * * @param * @param valueExtractor * @return */ public static Predicate isEmpty(final java.util.function.Function valueExtractor) { return t -> Strings.isEmpty(valueExtractor.apply(t)); } /** * Checks if is {@code null} or empty or blank. * * @param * @return */ public static Predicate isBlank() { return (Predicate) IS_BLANK; } /** * * @param * @param valueExtractor * @return */ public static Predicate isBlank(final java.util.function.Function valueExtractor) { return t -> Strings.isBlank(valueExtractor.apply(t)); } private static final Predicate IS_EMPTY_A = value -> value == null || value.length == 0; /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate isEmptyA() { return (Predicate) IS_EMPTY_A; } @SuppressWarnings("rawtypes") private static final Predicate IS_EMPTY_C = value -> value == null || value.size() == 0; /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate isEmptyC() { return (Predicate) IS_EMPTY_C; } @SuppressWarnings("rawtypes") private static final Predicate IS_EMPTY_M = value -> value == null || value.isEmpty(); /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate isEmptyM() { return (Predicate) IS_EMPTY_M; } /** * * @param * @return */ public static Predicate notNull() { return NOT_NULL; } /** * * @param * @param valueExtractor * @return */ public static Predicate notNull(final java.util.function.Function valueExtractor) { return t -> valueExtractor.apply(t) != null; } /** * Not {@code null} or empty. * * @param * @return */ public static Predicate notEmpty() { return (Predicate) IS_NOT_EMPTY; } /** * * @param * @param valueExtractor * @return */ public static Predicate notEmpty(final java.util.function.Function valueExtractor) { return t -> Strings.isNotEmpty(valueExtractor.apply(t)); } /** * Not {@code null} or empty or blank. * * @param * @return */ public static Predicate notBlank() { return (Predicate) IS_NOT_BLANK; } /** * * @param * @param valueExtractor * @return */ public static Predicate notBlank(final java.util.function.Function valueExtractor) { return t -> Strings.isNotBlank(valueExtractor.apply(t)); } private static final Predicate NOT_EMPTY_A = value -> value != null && value.length > 0; /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate notEmptyA() { return (Predicate) NOT_EMPTY_A; } @SuppressWarnings("rawtypes") private static final Predicate NOT_EMPTY_C = value -> value != null && value.size() > 0; /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate notEmptyC() { return (Predicate) NOT_EMPTY_C; } @SuppressWarnings("rawtypes") private static final Predicate NOT_EMPTY_M = value -> value != null && !value.isEmpty(); /** * * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate notEmptyM() { return (Predicate) NOT_EMPTY_M; } /** * Checks if is file. * * @return */ public static Predicate isFile() { return IS_FILE; } /** * Checks if is directory. * * @return */ public static Predicate isDirectory() { return IS_DIRECTORY; } /** * * @param * @param target * @return */ public static Predicate equal(final Object target) { return value -> N.equals(value, target); } /** * * @param * @param targetValue1 * @param targetValue2 * @return */ public static Predicate eqOr(final Object targetValue1, final Object targetValue2) { return value -> N.equals(value, targetValue1) || N.equals(value, targetValue2); } /** * * @param * @param targetValue1 * @param targetValue2 * @param targetValue3 * @return */ public static Predicate eqOr(final Object targetValue1, final Object targetValue2, final Object targetValue3) { return value -> N.equals(value, targetValue1) || N.equals(value, targetValue2) || N.equals(value, targetValue3); } /** * * @param * @param target * @return */ public static Predicate notEqual(final Object target) { return value -> !N.equals(value, target); } /** * * @param * @param target * @return */ public static > Predicate greaterThan(final T target) { return value -> N.compare(value, target) > 0; } /** * * @param * @param target * @return */ public static > Predicate greaterEqual(final T target) { return value -> N.compare(value, target) >= 0; } /** * * @param * @param target * @return */ public static > Predicate lessThan(final T target) { return value -> N.compare(value, target) < 0; } /** * * @param * @param target * @return */ public static > Predicate lessEqual(final T target) { return value -> N.compare(value, target) <= 0; } /** * Checks if the value/element: {@code minValue < e < maxValue}. * * @param * @param minValue * @param maxValue * @return */ public static > Predicate gtAndLt(final T minValue, final T maxValue) { return value -> N.compare(value, minValue) > 0 && N.compare(value, maxValue) < 0; } /** * Checks if the value/element: {@code minValue <= e < maxValue}. * * @param * @param minValue * @param maxValue * @return */ public static > Predicate geAndLt(final T minValue, final T maxValue) { return value -> N.compare(value, minValue) >= 0 && N.compare(value, maxValue) < 0; } /** * Checks if the value/element: {@code minValue <= e <= maxValue}. * * @param * @param minValue * @param maxValue * @return */ public static > Predicate geAndLe(final T minValue, final T maxValue) { return value -> N.compare(value, minValue) >= 0 && N.compare(value, maxValue) <= 0; } /** * Checks if the value/element: {@code minValue < e <= maxValue}. * * @param * @param minValue * @param maxValue * @return */ public static > Predicate gtAndLe(final T minValue, final T maxValue) { return value -> N.compare(value, minValue) > 0 && N.compare(value, maxValue) <= 0; } /** * Checks if the value/element: {@code minValue < e < maxValue}. * * @param * @param minValue * @param maxValue * @return * @deprecated replaced by {@code gtAndLt}. */ @Deprecated public static > Predicate between(final T minValue, final T maxValue) { return value -> N.compare(value, minValue) > 0 && N.compare(value, maxValue) < 0; } /** * * @param * @param c * @return * @throws IllegalArgumentException */ public static Predicate in(final Collection c) throws IllegalArgumentException { N.checkArgNotNull(c); final boolean isNotEmpty = N.notEmpty(c); return value -> isNotEmpty && c.contains(value); } /** * * @param * @param c * @return * @throws IllegalArgumentException */ public static Predicate notIn(final Collection c) throws IllegalArgumentException { N.checkArgNotNull(c); final boolean isEmpty = N.isEmpty(c); return value -> isEmpty || !c.contains(value); } /** * * @param * @param clazz * @return * @throws IllegalArgumentException */ public static Predicate instanceOf(final Class clazz) throws IllegalArgumentException { N.checkArgNotNull(clazz); return clazz::isInstance; } /** * * @param clazz * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static Predicate subtypeOf(final Class clazz) throws IllegalArgumentException { N.checkArgNotNull(clazz); return clazz::isAssignableFrom; } /** * * @param prefix * @return * @throws IllegalArgumentException */ public static Predicate startsWith(final String prefix) throws IllegalArgumentException { N.checkArgNotNull(prefix); return value -> value != null && value.startsWith(prefix); } /** * * @param suffix * @return * @throws IllegalArgumentException */ public static Predicate endsWith(final String suffix) throws IllegalArgumentException { N.checkArgNotNull(suffix); return value -> value != null && value.endsWith(suffix); } /** * * @param valueToFind * @return * @throws IllegalArgumentException */ public static Predicate contains(final String valueToFind) throws IllegalArgumentException { N.checkArgNotNull(valueToFind); return value -> value != null && value.contains(valueToFind); } /** * Not starts with. * * @param prefix * @return * @throws IllegalArgumentException */ public static Predicate notStartsWith(final String prefix) throws IllegalArgumentException { N.checkArgNotNull(prefix); return value -> value == null || !value.startsWith(prefix); } /** * Not ends with. * * @param suffix * @return * @throws IllegalArgumentException */ public static Predicate notEndsWith(final String suffix) throws IllegalArgumentException { N.checkArgNotNull(suffix); return value -> value == null || !value.endsWith(suffix); } /** * * @param str * @return * @throws IllegalArgumentException */ public static Predicate notContains(final String str) throws IllegalArgumentException { N.checkArgNotNull(str); return value -> value == null || !value.contains(str); } /** * * @param pattern * @return * @throws IllegalArgumentException */ public static Predicate matches(final Pattern pattern) throws IllegalArgumentException { N.checkArgNotNull(pattern); return value -> pattern.matcher(value).find(); } /** * * @param * @param * @return */ public static BiPredicate equal() { return BiPredicates.EQUAL; } /** * * @param * @param * @return */ public static BiPredicate notEqual() { return BiPredicates.NOT_EQUAL; } /** * * @param * @return */ public static > BiPredicate greaterThan() { return (BiPredicate) BiPredicates.GREATER_THAN; } /** * * @param * @return */ public static > BiPredicate greaterEqual() { return (BiPredicate) BiPredicates.GREATER_EQUAL; } /** * * @param * @return */ public static > BiPredicate lessThan() { return (BiPredicate) BiPredicates.LESS_THAN; } /** * * @param * @return */ public static > BiPredicate lessEqual() { return (BiPredicate) BiPredicates.LESS_EQUAL; } /** * * @param * @param predicate * @return * @throws IllegalArgumentException */ public static Predicate not(final java.util.function.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return t -> !predicate.test(t); } /** * * @param * @param * @param biPredicate * @return * @throws IllegalArgumentException */ public static BiPredicate not(final java.util.function.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return (t, u) -> !biPredicate.test(t, u); } /** * * @param * @param * @param * @param triPredicate * @return * @throws IllegalArgumentException */ public static TriPredicate not(final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (a, b, c) -> !triPredicate.test(a, b, c); } /** * * @param first * @param second * @return * @throws IllegalArgumentException */ public static BooleanSupplier and(final java.util.function.BooleanSupplier first, final java.util.function.BooleanSupplier second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return () -> first.getAsBoolean() && second.getAsBoolean(); } /** * * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static BooleanSupplier and(final java.util.function.BooleanSupplier first, final java.util.function.BooleanSupplier second, final java.util.function.BooleanSupplier third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return () -> first.getAsBoolean() && second.getAsBoolean() && third.getAsBoolean(); } /** * * @param * @param first * @param second * @return * @throws IllegalArgumentException */ public static Predicate and(final java.util.function.Predicate first, final java.util.function.Predicate second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return t -> first.test(t) && second.test(t); } /** * * @param * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static Predicate and(final java.util.function.Predicate first, final java.util.function.Predicate second, final java.util.function.Predicate third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return t -> first.test(t) && second.test(t) && third.test(t); } /** * * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is {@code null} or empty. */ public static Predicate and(final Collection> c) throws IllegalArgumentException { N.checkArgNotEmpty(c, cs.c); return t -> { for (final java.util.function.Predicate p : c) { if (!p.test(t)) { return false; } } return true; }; } /** * * @param * @param * @param first * @param second * @return * @throws IllegalArgumentException */ public static BiPredicate and(final java.util.function.BiPredicate first, final java.util.function.BiPredicate second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return (t, u) -> first.test(t, u) && second.test(t, u); } /** * * @param * @param * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static BiPredicate and(final java.util.function.BiPredicate first, final java.util.function.BiPredicate second, final BiPredicate third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return (t, u) -> first.test(t, u) && second.test(t, u) && third.test(t, u); } /** * * @param * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is {@code null} or empty. */ public static BiPredicate and(final List> c) throws IllegalArgumentException { N.checkArgNotEmpty(c, cs.c); return (t, u) -> { for (final java.util.function.BiPredicate p : c) { if (!p.test(t, u)) { return false; } } return true; }; } /** * * @param first * @param second * @return * @throws IllegalArgumentException */ public static BooleanSupplier or(final java.util.function.BooleanSupplier first, final java.util.function.BooleanSupplier second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return () -> first.getAsBoolean() || second.getAsBoolean(); } /** * * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static BooleanSupplier or(final java.util.function.BooleanSupplier first, final java.util.function.BooleanSupplier second, final java.util.function.BooleanSupplier third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return () -> first.getAsBoolean() || second.getAsBoolean() || third.getAsBoolean(); } /** * * @param * @param first * @param second * @return * @throws IllegalArgumentException */ public static Predicate or(final java.util.function.Predicate first, final java.util.function.Predicate second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return t -> first.test(t) || second.test(t); } /** * * @param * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static Predicate or(final java.util.function.Predicate first, final java.util.function.Predicate second, final java.util.function.Predicate third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return t -> first.test(t) || second.test(t) || third.test(t); } /** * * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is {@code null} or empty. */ public static Predicate or(final Collection> c) throws IllegalArgumentException { N.checkArgNotEmpty(c, cs.c); return t -> { for (final java.util.function.Predicate p : c) { if (p.test(t)) { return true; } } return false; }; } /** * * @param * @param * @param first * @param second * @return * @throws IllegalArgumentException */ public static BiPredicate or(final java.util.function.BiPredicate first, final java.util.function.BiPredicate second) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); return (t, u) -> first.test(t, u) || second.test(t, u); } /** * * @param * @param * @param first * @param second * @param third * @return * @throws IllegalArgumentException */ public static BiPredicate or(final java.util.function.BiPredicate first, final java.util.function.BiPredicate second, final java.util.function.BiPredicate third) throws IllegalArgumentException { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return (t, u) -> first.test(t, u) || second.test(t, u) || third.test(t, u); } /** * * @param * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is {@code null} or empty. */ public static BiPredicate or(final List> c) throws IllegalArgumentException { N.checkArgNotEmpty(c, cs.c); return (t, u) -> { for (final java.util.function.BiPredicate p : c) { if (p.test(t, u)) { return true; } } return false; }; } /** * Test by key. * * @param the key type * @param the value type * @param predicate * @return * @throws IllegalArgumentException */ public static Predicate> testByKey(final java.util.function.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return entry -> predicate.test(entry.getKey()); } /** * Test by value. * * @param the key type * @param the value type * @param predicate * @return * @throws IllegalArgumentException */ public static Predicate> testByValue(final java.util.function.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return entry -> predicate.test(entry.getValue()); } /** * Accept by key. * * @param the key type * @param the value type * @param consumer * @return * @throws IllegalArgumentException */ public static Consumer> acceptByKey(final java.util.function.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return entry -> consumer.accept(entry.getKey()); } /** * Accept by value. * * @param the key type * @param the value type * @param consumer * @return * @throws IllegalArgumentException */ public static Consumer> acceptByValue(final java.util.function.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return entry -> consumer.accept(entry.getValue()); } /** * Apply by key. * * @param the key type * @param the value type * @param * @param func * @return * @throws IllegalArgumentException */ public static Function, R> applyByKey(final java.util.function.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> func.apply(entry.getKey()); } /** * Apply by value. * * @param the key type * @param the value type * @param * @param func * @return * @throws IllegalArgumentException */ public static Function, R> applyByValue(final java.util.function.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> func.apply(entry.getValue()); } /** * * @param the key type * @param the value type * @param * @param func * @return * @throws IllegalArgumentException */ public static Function, Map.Entry> mapKey(final java.util.function.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> new ImmutableEntry<>(func.apply(entry.getKey()), entry.getValue()); } /** * * @param the key type * @param the value type * @param * @param func * @return * @throws IllegalArgumentException */ public static Function, Map.Entry> mapValue(final java.util.function.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> new ImmutableEntry<>(entry.getKey(), func.apply(entry.getValue())); } /** * Test key val. * * @param the key type * @param the value type * @param predicate * @return * @throws IllegalArgumentException */ public static Predicate> testKeyVal(final java.util.function.BiPredicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return entry -> predicate.test(entry.getKey(), entry.getValue()); } /** * Accept key val. * * @param the key type * @param the value type * @param consumer * @return * @throws IllegalArgumentException */ public static Consumer> acceptKeyVal(final java.util.function.BiConsumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return entry -> consumer.accept(entry.getKey(), entry.getValue()); } /** * Apply key val. * * @param the key type * @param the value type * @param * @param func * @return * @throws IllegalArgumentException */ public static Function, R> applyKeyVal(final java.util.function.BiFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> func.apply(entry.getKey(), entry.getValue()); } /** * * @param * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer acceptIfNotNull(final java.util.function.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return t -> { if (t != null) { consumer.accept(t); } }; } /** * * @param * @param predicate * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer acceptIf(final java.util.function.Predicate predicate, final java.util.function.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNull(consumer); return t -> { if (predicate.test(t)) { consumer.accept(t); } }; } /** * * @param * @param predicate * @param consumerForTrue * @param consumerForFalse * @return * @throws IllegalArgumentException */ @Beta public static Consumer acceptIfOrElse(final java.util.function.Predicate predicate, final java.util.function.Consumer consumerForTrue, final java.util.function.Consumer consumerForFalse) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNull(consumerForTrue); N.checkArgNotNull(consumerForFalse); return t -> { if (predicate.test(t)) { consumerForTrue.accept(t); } else { consumerForFalse.accept(t); } }; } /** * * @param * @param * @param mapper * @return */ @Beta public static Function> applyIfNotNullOrEmpty(final java.util.function.Function> mapper) { return t -> t == null ? N.emptyList() : mapper.apply(t); } /** * Apply if not {@code null} or default. * * @param * @param * @param * @param mapperA * @param mapperB * @param defaultValue * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrDefault(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final R defaultValue) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); return a -> { if (a == null) { return defaultValue; } final B b = mapperA.apply(a); if (b == null) { return defaultValue; } else { return mapperB.apply(b); } }; } /** * Apply if not {@code null} or default. * * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param defaultValue * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrDefault(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final java.util.function.Function mapperC, final R defaultValue) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); return a -> { if (a == null) { return defaultValue; } final B b = mapperA.apply(a); if (b == null) { return defaultValue; } final C c = mapperB.apply(b); if (c == null) { return defaultValue; } else { return mapperC.apply(c); } }; } /** * Apply if not {@code null} or default. * * @param * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param mapperD * @param defaultValue * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrDefault(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final java.util.function.Function mapperC, final java.util.function.Function mapperD, final R defaultValue) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); N.checkArgNotNull(mapperD); return a -> { if (a == null) { return defaultValue; } final B b = mapperA.apply(a); if (b == null) { return defaultValue; } final C c = mapperB.apply(b); if (c == null) { return defaultValue; } final D d = mapperC.apply(c); if (d == null) { return defaultValue; } else { return mapperD.apply(d); } }; } /** * Apply if not {@code null} or get. * * @param * @param * @param * @param mapperA * @param mapperB * @param supplier * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrElseGet(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); return a -> { if (a == null) { return supplier.get(); } final B b = mapperA.apply(a); if (b == null) { return supplier.get(); } else { return mapperB.apply(b); } }; } /** * Apply if not {@code null} or get. * * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param supplier * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrElseGet(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final java.util.function.Function mapperC, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); return a -> { if (a == null) { return supplier.get(); } final B b = mapperA.apply(a); if (b == null) { return supplier.get(); } final C c = mapperB.apply(b); if (c == null) { return supplier.get(); } else { return mapperC.apply(c); } }; } /** * Apply if not {@code null} or get. * * @param * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param mapperD * @param supplier * @return * @throws IllegalArgumentException */ public static Function applyIfNotNullOrElseGet(final java.util.function.Function mapperA, final java.util.function.Function mapperB, final java.util.function.Function mapperC, final java.util.function.Function mapperD, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); N.checkArgNotNull(mapperD); return a -> { if (a == null) { return supplier.get(); } final B b = mapperA.apply(a); if (b == null) { return supplier.get(); } final C c = mapperB.apply(b); if (c == null) { return supplier.get(); } final D d = mapperC.apply(c); if (d == null) { return supplier.get(); } else { return mapperD.apply(d); } }; } /** * * @param * @param * @param predicate * @param func * @param defaultValue * @return * @throws IllegalArgumentException */ @Beta public static Function applyIfOrElseDefault(final java.util.function.Predicate predicate, final java.util.function.Function func, final R defaultValue) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNull(func); return t -> { if (predicate.test(t)) { return func.apply(t); } else { return defaultValue; } }; } /** * * @param * @param * @param predicate * @param func * @param supplier * @return * @throws IllegalArgumentException */ @Beta public static Function applyIfOrElseGet(final java.util.function.Predicate predicate, final java.util.function.Function func, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNull(func); N.checkArgNotNull(supplier); return t -> { if (predicate.test(t)) { return func.apply(t); } else { return supplier.get(); } }; } private static final Function>, List>> FLAT_MAP_VALUE_FUNC = Maps::flatToMap; /** * {a=[1, 2, 3], b=[4, 5, 6], c=[7, 8]} -> [{a=1, b=4, c=7}, {a=2, b=5, c=8}, {a=3, b=6}]. * * @param the key type * @param the value type * @return * @see Maps#flatToMap(Map) */ @Beta @SuppressWarnings("rawtypes") public static Function>, List>> flatmapValue() { return (Function) FLAT_MAP_VALUE_FUNC; } private static final ToByteFunction PARSE_BYTE_FUNC = Numbers::toByte; /** * Parses the byte. * * @return */ public static ToByteFunction parseByte() { return PARSE_BYTE_FUNC; } private static final ToShortFunction PARSE_SHORT_FUNC = Numbers::toShort; /** * Parses the short. * * @return */ public static ToShortFunction parseShort() { return PARSE_SHORT_FUNC; } private static final ToIntFunction PARSE_INT_FUNC = Numbers::toInt; /** * Parses the int. * * @return */ public static ToIntFunction parseInt() { return PARSE_INT_FUNC; } private static final ToLongFunction PARSE_LONG_FUNC = Numbers::toLong; /** * Parses the long. * * @return */ public static ToLongFunction parseLong() { return PARSE_LONG_FUNC; } private static final ToFloatFunction PARSE_FLOAT_FUNC = Numbers::toFloat; /** * Parses the float. * * @return */ public static ToFloatFunction parseFloat() { return PARSE_FLOAT_FUNC; } private static final ToDoubleFunction PARSE_DOUBLE_FUNC = Numbers::toDouble; /** * Parses the double. * * @return */ public static ToDoubleFunction parseDouble() { return PARSE_DOUBLE_FUNC; } private static final Function CREATE_NUMBER_FUNC = t -> Strings.isEmpty(t) ? null : Numbers.createNumber(t); /** * Creates the number. * * @return * @see Numbers#createNumber(String) */ public static Function createNumber() { return CREATE_NUMBER_FUNC; } /** * Num to int. * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToIntFunction numToInt() { return (ToIntFunction) ToIntFunction.FROM_NUM; } /** * Num to long. * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToLongFunction numToLong() { return (ToLongFunction) ToLongFunction.FROM_NUM; } /** * Num to double. * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToDoubleFunction numToDouble() { return (ToDoubleFunction) ToDoubleFunction.FROM_NUM; } /** * * @param * @param count * @return * @throws IllegalArgumentException */ @Beta @Stateful public static Predicate atMost(final int count) throws IllegalArgumentException { // TODO cnt or atMost? skip(atMost(n)/limit(atMots(n)/dropWhile(atMost(n)/takeWhile(atMost(n) // TODO cnt or atMost? skip(cnt(n)/limit(cnt(n)/dropWhile(cnt(n)/takeWhile(cnt(n) // public static Predicate cnt(final int count) { N.checkArgNotNegative(count, cs.count); return new Predicate<>() { private final AtomicInteger counter = new AtomicInteger(count); @Override public boolean test(final T t) { return counter.getAndDecrement() > 0; } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param limit * @param predicate * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Predicate limitThenFilter(final int limit, final java.util.function.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNegative(limit, cs.limit); N.checkArgNotNull(predicate); return new Predicate<>() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(final T t) { return counter.getAndDecrement() > 0 && predicate.test(t); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param limit * @param predicate * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static BiPredicate limitThenFilter(final int limit, final java.util.function.BiPredicate predicate) throws IllegalArgumentException { N.checkArgNotNegative(limit, cs.limit); N.checkArgNotNull(predicate); return new BiPredicate<>() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(final T t, final U u) { return counter.getAndDecrement() > 0 && predicate.test(t, u); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param predicate * @param limit * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Predicate filterThenLimit(final java.util.function.Predicate predicate, final int limit) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNegative(limit, cs.limit); return new Predicate<>() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(final T t) { return predicate.test(t) && counter.getAndDecrement() > 0; } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param predicate * @param limit * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static BiPredicate filterThenLimit(final java.util.function.BiPredicate predicate, final int limit) throws IllegalArgumentException { N.checkArgNotNull(predicate); N.checkArgNotNegative(limit, cs.limit); return new BiPredicate<>() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(final T t, final U u) { return predicate.test(t, u) && counter.getAndDecrement() > 0; } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param timeInMillis * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Predicate timeLimit(final long timeInMillis) throws IllegalArgumentException { N.checkArgNotNegative(timeInMillis, cs.timeInMillis); if (timeInMillis == 0) { return Fn.alwaysFalse(); } final MutableBoolean ongoing = MutableBoolean.of(true); final TimerTask task = new TimerTask() { @Override public void run() { ongoing.setFalse(); } }; timer.schedule(task, timeInMillis); return t -> ongoing.value(); } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param duration * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Predicate timeLimit(final Duration duration) throws IllegalArgumentException { N.checkArgNotNull(duration, cs.duration); return timeLimit(duration.toMillis()); } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @return */ @Beta @SequentialOnly @Stateful public static Function> indexed() { return new Function<>() { private final MutableLong idx = new MutableLong(0); @Override public Indexed apply(final T t) { return Indexed.of(t, idx.getAndIncrement()); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param predicate * @return */ @Beta @SequentialOnly @Stateful public static Predicate indexed(final IntObjPredicate predicate) { return Predicates.indexed(predicate); } /** The Constant RETURN_FIRST. */ private static final BinaryOperator RETURN_FIRST = (a, b) -> a; /** * * @param * @return */ public static BinaryOperator selectFirst() { return (BinaryOperator) RETURN_FIRST; } /** The Constant RETURN_SECOND. */ private static final BinaryOperator RETURN_SECOND = (a, b) -> b; /** * * @param * @return */ public static BinaryOperator selectSecond() { return (BinaryOperator) RETURN_SECOND; } /** The Constant MIN. */ @SuppressWarnings({ "rawtypes" }) private static final BinaryOperator MIN = (a, b) -> Comparators.NULL_LAST_COMPARATOR.compare(a, b) <= 0 ? a : b; /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator min() { return (BinaryOperator) MIN; } /** * * @param * @param comparator * @return * @throws IllegalArgumentException */ public static BinaryOperator min(final Comparator comparator) throws IllegalArgumentException { N.checkArgNotNull(comparator); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } /** * * @param * @param keyExtractor * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static BinaryOperator minBy(final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); final Comparator comparator = Comparators.nullsLastBy(keyExtractor); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } /** The Constant MIN_BY_KEY. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MIN_BY_KEY = new BinaryOperator<>() { private final Comparator cmp = Comparators.NULL_LAST_COMPARATOR; @Override public Entry apply(final Entry a, final Entry b) { return cmp.compare(a.getKey(), b.getKey()) <= 0 ? a : b; } }; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , V> BinaryOperator> minByKey() { return (BinaryOperator) MIN_BY_KEY; } /** The Constant MIN_BY_VALUE. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MIN_BY_VALUE = new BinaryOperator<>() { private final Comparator cmp = Comparators.NULL_LAST_COMPARATOR; @Override public Entry apply(final Entry a, final Entry b) { return cmp.compare(a.getValue(), b.getValue()) <= 0 ? a : b; } }; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator> minByValue() { return (BinaryOperator) MIN_BY_VALUE; } /** The Constant MAX. */ @SuppressWarnings("rawtypes") private static final BinaryOperator MAX = (a, b) -> Comparators.NULL_FIRST_COMPARATOR.compare(a, b) >= 0 ? a : b; /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator max() { return (BinaryOperator) MAX; } /** * * @param * @param comparator * @return * @throws IllegalArgumentException */ public static BinaryOperator max(final Comparator comparator) throws IllegalArgumentException { N.checkArgNotNull(comparator); return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; } /** * * @param * @param keyExtractor * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static BinaryOperator maxBy(final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); final Comparator comparator = Comparators.nullsFirstBy(keyExtractor); return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; } /** The Constant MAX_BY_KEY. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MAX_BY_KEY = new BinaryOperator<>() { private final Comparator cmp = Comparators.NULL_FIRST_COMPARATOR; @Override public Entry apply(final Entry a, final Entry b) { return cmp.compare(a.getKey(), b.getKey()) >= 0 ? a : b; } }; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , V> BinaryOperator> maxByKey() { return (BinaryOperator) MAX_BY_KEY; } /** The Constant MAX_BY_VALUE. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MAX_BY_VALUE = new BinaryOperator<>() { private final Comparator cmp = Comparators.NULL_FIRST_COMPARATOR; @Override public Entry apply(final Entry a, final Entry b) { return cmp.compare(a.getValue(), b.getValue()) >= 0 ? a : b; } }; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator> maxByValue() { return (BinaryOperator) MAX_BY_VALUE; } /** * * @param * @param target * @return */ public static > Function compareTo(final T target) { return t -> N.compare(t, target); } /** * * @param * @param target * @param cmp * @return * @throws IllegalArgumentException */ public static Function compareTo(final T target, final Comparator cmp) throws IllegalArgumentException { // N.checkArgNotNull(cmp); final Comparator cmpToUse = cmp == null ? (Comparator) Comparators.naturalOrder() : cmp; return t -> cmpToUse.compare(t, target); } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static > BiFunction compare() { return (BiFunction) COMPARE; } /** * * @param * @param cmp * @return * @throws IllegalArgumentException */ public static BiFunction compare(final Comparator cmp) throws IllegalArgumentException { // N.checkArgNotNull(cmp); if (cmp == null || cmp == Comparators.naturalOrder()) { // NOSONAR return (BiFunction) COMPARE; } return cmp::compare; } /** * * @param * @param defaultValue * @return */ @Beta public static Function, T> futureGetOrDefaultOnError(final T defaultValue) { return f -> { try { return f.get(); } catch (InterruptedException | ExecutionException e) { Thread.currentThread().interrupt(); // throw ExceptionUtil.toRuntimeException(e, true); return defaultValue; } }; } private static final Function, Object> FUTURE_GETTER = f -> { try { return f.get(); } catch (InterruptedException | ExecutionException e) { return ExceptionUtil.toRuntimeException(e, true); } }; /** * * @param * @return */ @SuppressWarnings("rawtypes") @Beta public static Function, T> futureGet() { return (Function) FUTURE_GETTER; } /** * * @param * @param supplier * @return */ @Beta @SuppressWarnings("rawtypes") public static Supplier from(final java.util.function.Supplier supplier) { return supplier instanceof Supplier ? ((Supplier) supplier) : supplier::get; } /** * * @param * @param func * @return */ @Beta @SuppressWarnings("rawtypes") public static IntFunction from(final java.util.function.IntFunction func) { return func instanceof IntFunction ? ((IntFunction) func) : func::apply; } /** * * @param * @param predicate * @return */ @Beta @SuppressWarnings("rawtypes") public static Predicate from(final java.util.function.Predicate predicate) { return predicate instanceof Predicate ? ((Predicate) predicate) : predicate::test; } /** * * @param * @param * @param predicate * @return */ @Beta @SuppressWarnings("rawtypes") public static BiPredicate from(final java.util.function.BiPredicate predicate) { return predicate instanceof BiPredicate ? ((BiPredicate) predicate) : predicate::test; } /** * * @param * @param consumer * @return */ @Beta @SuppressWarnings("rawtypes") public static Consumer from(final java.util.function.Consumer consumer) { return consumer instanceof Consumer ? ((Consumer) consumer) : consumer::accept; } /** * * @param * @param * @param consumer * @return */ @Beta @SuppressWarnings("rawtypes") public static BiConsumer from(final java.util.function.BiConsumer consumer) { return consumer instanceof BiConsumer ? ((BiConsumer) consumer) : consumer::accept; } /** * * @param * @param * @param function * @return */ @Beta @SuppressWarnings("rawtypes") public static Function from(final java.util.function.Function function) { return function instanceof Function ? ((Function) function) : function::apply; } /** * * @param * @param * @param * @param function * @return */ @Beta @SuppressWarnings("rawtypes") public static BiFunction from(final java.util.function.BiFunction function) { return function instanceof BiFunction ? ((BiFunction) function) : function::apply; } /** * * @param * @param op * @return */ @Beta @SuppressWarnings("rawtypes") public static UnaryOperator from(final java.util.function.UnaryOperator op) { return op instanceof UnaryOperator ? ((UnaryOperator) op) : op::apply; } /** * * @param * @param op * @return */ @Beta @SuppressWarnings("rawtypes") public static BinaryOperator from(final java.util.function.BinaryOperator op) { return op instanceof BinaryOperator ? ((BinaryOperator) op) : op::apply; } /** * * @param * @param predicate * @return */ @Beta public static Predicate p(final Predicate predicate) { return predicate; } /** * * @param * @param * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate p(final A a, final java.util.function.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return t -> biPredicate.test(a, t); } /** * * @param * @param * @param * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate p(final A a, final B b, final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return t -> triPredicate.test(a, b, t); } /** * * @param * @param * @param biPredicate * @return */ @Beta public static BiPredicate p(final BiPredicate biPredicate) { return biPredicate; } /** * * @param * @param * @param * @param a * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static BiPredicate p(final A a, final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (t, u) -> triPredicate.test(a, t, u); } /** * * @param * @param * @param * @param triPredicate * @return */ @Beta public static TriPredicate p(final TriPredicate triPredicate) { return triPredicate; } /** * * @param * @param predicate * @return */ @Beta public static Consumer c(final Consumer predicate) { return predicate; } /** * * @param * @param * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer c(final A a, final java.util.function.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return t -> biConsumer.accept(a, t); } /** * * @param * @param * @param * @param a * @param b * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer c(final A a, final B b, final TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return t -> triConsumer.accept(a, b, t); } /** * * @param * @param * @param biConsumer * @return */ @Beta public static BiConsumer c(final BiConsumer biConsumer) { return biConsumer; } /** * * @param * @param * @param * @param a * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static BiConsumer c(final A a, final TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (t, u) -> triConsumer.accept(a, t, u); } /** * * @param * @param * @param * @param triConsumer * @return */ @Beta public static TriConsumer c(final TriConsumer triConsumer) { return triConsumer; } /** * * @param * @param * @param predicate * @return */ @Beta public static Function f(final Function predicate) { return predicate; } /** * * @param * @param * @param * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Function f(final A a, final java.util.function.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return t -> biFunction.apply(a, t); } /** * * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Function f(final A a, final B b, final TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return t -> triFunction.apply(a, b, t); } /** * * @param * @param * @param * @param biFunction * @return */ @Beta public static BiFunction f(final BiFunction biFunction) { return biFunction; } /** * * @param * @param * @param * @param * @param a * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static BiFunction f(final A a, final TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (t, u) -> triFunction.apply(a, t, u); } /** * * @param * @param * @param * @param * @param triFunction * @return */ @Beta public static TriFunction f(final TriFunction triFunction) { return triFunction; } /** * * @param * @param predicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate pp(final Throwables.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return value -> { try { return predicate.test(value); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate pp(final A a, final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return t -> { try { return biPredicate.test(a, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate pp(final A a, final B b, final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return t -> { try { return triPredicate.test(a, b, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static BiPredicate pp(final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return (t, u) -> { try { return biPredicate.test(t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param a * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static BiPredicate pp(final A a, final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (t, u) -> { try { return triPredicate.test(a, t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static TriPredicate pp(final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (a, b, c) -> { try { return triPredicate.test(a, b, c); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer cc(final Throwables.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return t -> { try { consumer.accept(t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer cc(final A a, final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return t -> { try { biConsumer.accept(a, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param a * @param b * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer cc(final A a, final B b, final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return t -> { try { triConsumer.accept(a, b, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static BiConsumer cc(final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return (t, u) -> { try { biConsumer.accept(t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param a * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static BiConsumer cc(final A a, final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (t, u) -> { try { triConsumer.accept(a, t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static TriConsumer cc(final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (a, b, c) -> { try { triConsumer.accept(a, b, c); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param function * @return * @throws IllegalArgumentException */ @Beta public static Function ff(final Throwables.Function function) throws IllegalArgumentException { N.checkArgNotNull(function); return t -> { try { return function.apply(t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param function * @param defaultOnError * @return * @throws IllegalArgumentException */ @Beta public static Function ff(final Throwables.Function function, final R defaultOnError) throws IllegalArgumentException { N.checkArgNotNull(function); return t -> { try { return function.apply(t); } catch (final Exception e) { return defaultOnError; } }; } /** * * @param * @param * @param * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Function ff(final A a, final Throwables.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return t -> { try { return biFunction.apply(a, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Function ff(final A a, final B b, final Throwables.TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return t -> { try { return triFunction.apply(a, b, t); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static BiFunction ff(final Throwables.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return (t, u) -> { try { return biFunction.apply(t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param biFunction * @param defaultOnError * @return * @throws IllegalArgumentException */ @Beta public static BiFunction ff(final Throwables.BiFunction biFunction, final R defaultOnError) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return (t, u) -> { try { return biFunction.apply(t, u); } catch (final Exception e) { return defaultOnError; } }; } /** * * @param * @param * @param * @param * @param a * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static BiFunction ff(final A a, final Throwables.TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (t, u) -> { try { return triFunction.apply(a, t, u); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static TriFunction ff(final Throwables.TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (a, b, c) -> { try { return triFunction.apply(a, b, c); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param * @param * @param * @param triFunction * @param defaultOnError * @return * @throws IllegalArgumentException */ @Beta public static TriFunction ff(final Throwables.TriFunction triFunction, final R defaultOnError) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (a, b, c) -> { try { return triFunction.apply(a, b, c); } catch (final Exception e) { return defaultOnError; } }; } /** * Synchronized {@code Predicate}. * * @param * @param mutex to synchronized on * @param predicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate sp(final Object mutex, final java.util.function.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(predicate, cs.Predicate); return t -> { synchronized (mutex) { return predicate.test(t); } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param mutex to synchronized on * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate sp(final Object mutex, final A a, final java.util.function.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biPredicate, cs.BiPredicate); return t -> { synchronized (mutex) { return biPredicate.test(a, t); } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Predicate sp(final Object mutex, final A a, final B b, final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(triPredicate, cs.TriPredicate); return t -> { synchronized (mutex) { return triPredicate.test(a, b, t); } }; } /** * Synchronized {@code BiPredicate}. * * @param * @param * @param mutex to synchronized on * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static BiPredicate sp(final Object mutex, final java.util.function.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biPredicate, cs.BiPredicate); return (t, u) -> { synchronized (mutex) { return biPredicate.test(t, u); } }; } /** * Synchronized {@code Consumer}. * * @param * @param mutex to synchronized on * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer sc(final Object mutex, final java.util.function.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(consumer, cs.Consumer); return t -> { synchronized (mutex) { consumer.accept(t); } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param mutex to synchronized on * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Consumer sc(final Object mutex, final A a, final java.util.function.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biConsumer, cs.BiConsumer); return t -> { synchronized (mutex) { biConsumer.accept(a, t); } }; } /** * Synchronized {@code BiConsumer}. * * @param * @param * @param mutex to synchronized on * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static BiConsumer sc(final Object mutex, final java.util.function.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biConsumer, cs.BiConsumer); return (t, u) -> { synchronized (mutex) { biConsumer.accept(t, u); } }; } /** * Synchronized {@code Function}. * * @param * @param * @param mutex to synchronized on * @param function * @return * @throws IllegalArgumentException */ @Beta public static Function sf(final Object mutex, final java.util.function.Function function) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(function, cs.function); return t -> { synchronized (mutex) { return function.apply(t); } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Function sf(final Object mutex, final A a, final java.util.function.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biFunction, cs.BiFunction); return t -> { synchronized (mutex) { return biFunction.apply(a, t); } }; } /** * Synchronized {@code BiFunction}. * * @param * @param * @param * @param mutex to synchronized on * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static BiFunction sf(final Object mutex, final java.util.function.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biFunction, cs.BiFunction); return (t, u) -> { synchronized (mutex) { return biFunction.apply(t, u); } }; } /** * * @param * @param action * @return * @throws IllegalArgumentException */ public static Function c2f(final java.util.function.Consumer action) throws IllegalArgumentException { N.checkArgNotNull(action); return t -> { action.accept(t); return null; }; } /** * * @param * @param * @param action * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Function c2f(final java.util.function.Consumer action, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(action); return t -> { action.accept(t); return valueToReturn; }; } /** * * @param * @param * @param action * @return * @throws IllegalArgumentException */ public static BiFunction c2f(final java.util.function.BiConsumer action) throws IllegalArgumentException { N.checkArgNotNull(action); return (t, u) -> { action.accept(t, u); return null; }; } /** * * @param * @param * @param * @param action * @param valueToReturn * @return * @throws IllegalArgumentException */ public static BiFunction c2f(final java.util.function.BiConsumer action, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(action); return (t, u) -> { action.accept(t, u); return valueToReturn; }; } /** * * @param * @param * @param * @param action * @return * @throws IllegalArgumentException */ public static TriFunction c2f(final TriConsumer action) throws IllegalArgumentException { N.checkArgNotNull(action); return (a, b, c) -> { action.accept(a, b, c); return null; }; } /** * * @param * @param * @param * @param * @param action * @param valueToReturn * @return * @throws IllegalArgumentException */ public static TriFunction c2f(final TriConsumer action, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(action); return (a, b, c) -> { action.accept(a, b, c); return valueToReturn; }; } /** * Returns a {@code Consumer} which calls the specified {@code func}. * * @param * @param func * @return * @throws IllegalArgumentException */ public static Consumer f2c(final java.util.function.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static BiConsumer f2c(final java.util.function.BiFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static TriConsumer f2c(final TriFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param runnable * @return */ public static Runnable rr(final Throwables.Runnable runnable) { return () -> { try { runnable.run(); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param callable * @return */ public static Callable cc(final Throwables.Callable callable) { return () -> { try { return callable.call(); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param runnable * @return * @throws IllegalArgumentException */ public static Runnable r(final Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); return runnable; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static Callable c(final Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return callable; } /** * * @param runnable * @return * @throws IllegalArgumentException */ public static java.lang.Runnable jr(final java.lang.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); return runnable; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static java.util.concurrent.Callable jc(final java.util.concurrent.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return callable; } /** * * @param runnable * @return * @throws IllegalArgumentException */ public static Callable r2c(final java.lang.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); return () -> { runnable.run(); return null; }; } /** * * @param * @param runnable * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Callable r2c(final java.lang.Runnable runnable, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(runnable); return () -> { runnable.run(); return valueToReturn; }; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static Runnable c2r(final Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return callable::call; } /** * * @param runnable * @return * @throws IllegalArgumentException */ public static Runnable jr2r(final java.lang.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); if (runnable instanceof Runnable) { return (Runnable) runnable; } return runnable::run; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static Callable jc2c(final java.util.concurrent.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); if (callable instanceof Callable) { return (Callable) callable; } return () -> { try { return callable.call(); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param callable * @return * @throws IllegalArgumentException */ public static Runnable jc2r(final java.util.concurrent.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return () -> { try { callable.call(); } catch (final Exception e) { throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @return */ public static BinaryOperator throwingMerger() { return BinaryOperators.THROWING_MERGER; } /** * * @param * @return */ public static BinaryOperator ignoringMerger() { return BinaryOperators.IGNORING_MERGER; } /** * * @param * @return */ public static BinaryOperator replacingMerger() { return BinaryOperators.REPLACING_MERGER; } @SuppressWarnings("rawtypes") static final Function GET_AS_IT = it -> it.orElse(null); /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, T> getIfPresentOrElseNull() { return (Function) GET_AS_IT; } @SuppressWarnings("rawtypes") static final Function GET_AS_IT_JDK = it -> it.orElse(null); /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, T> getIfPresentOrElseNullJdk() { return (Function) GET_AS_IT_JDK; } @SuppressWarnings("rawtypes") static final Predicate IS_PRESENT_IT = Optional::isPresent; /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Predicate> isPresent() { return (Predicate) IS_PRESENT_IT; } @SuppressWarnings("rawtypes") static final Predicate IS_PRESENT_IT_JDK = java.util.Optional::isPresent; /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Predicate> isPresentJdk() { return (Predicate) IS_PRESENT_IT_JDK; } /** * Returns a stateful {@code BiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @param * @return */ @Beta @SequentialOnly @Stateful public static BiFunction switchOnNext() { return new BiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final T t, final T u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class LongSuppliers { private LongSuppliers() { // utility class } private static final LongSupplier CURRENT_TIME = System::currentTimeMillis; public static LongSupplier ofCurrentTimeMillis() { return CURRENT_TIME; } } public static final class Suppliers { /** The Constant UUID. */ private static final Supplier UUID = Strings::uuid; /** The Constant GUID. */ private static final Supplier GUID = Strings::guid; /** The Constant EMPTY_BOOLEAN_ARRAY. */ private static final Supplier EMPTY_BOOLEAN_ARRAY = () -> N.EMPTY_BOOLEAN_ARRAY; /** The Constant EMPTY_CHAR_ARRAY. */ private static final Supplier EMPTY_CHAR_ARRAY = () -> N.EMPTY_CHAR_ARRAY; /** The Constant EMPTY_BYTE_ARRAY. */ private static final Supplier EMPTY_BYTE_ARRAY = () -> N.EMPTY_BYTE_ARRAY; /** The Constant EMPTY_SHORT_ARRAY. */ private static final Supplier EMPTY_SHORT_ARRAY = () -> N.EMPTY_SHORT_ARRAY; /** The Constant EMPTY_INT_ARRAY. */ private static final Supplier EMPTY_INT_ARRAY = () -> N.EMPTY_INT_ARRAY; /** The Constant EMPTY_LONG_ARRAY. */ private static final Supplier EMPTY_LONG_ARRAY = () -> N.EMPTY_LONG_ARRAY; /** The Constant EMPTY_FLOAT_ARRAY. */ private static final Supplier EMPTY_FLOAT_ARRAY = () -> N.EMPTY_FLOAT_ARRAY; /** The Constant EMPTY_DOUBLE_ARRAY. */ private static final Supplier EMPTY_DOUBLE_ARRAY = () -> N.EMPTY_DOUBLE_ARRAY; /** The Constant EMPTY_STRING_ARRAY. */ private static final Supplier EMPTY_STRING_ARRAY = () -> N.EMPTY_STRING_ARRAY; /** The Constant EMPTY_OBJECT_ARRAY. */ private static final Supplier EMPTY_OBJECT_ARRAY = () -> N.EMPTY_OBJECT_ARRAY; /** The Constant EMPTY_STRING. */ private static final Supplier EMPTY_STRING = () -> Strings.EMPTY_STRING; /** The Constant BOOLEAN_LIST. */ private static final Supplier BOOLEAN_LIST = BooleanList::new; /** The Constant CHAR_LIST. */ private static final Supplier CHAR_LIST = CharList::new; /** The Constant BYTE_LIST. */ private static final Supplier BYTE_LIST = ByteList::new; /** The Constant SHORT_LIST. */ private static final Supplier SHORT_LIST = ShortList::new; /** The Constant INT_LIST. */ private static final Supplier INT_LIST = IntList::new; /** The Constant LONG_LIST. */ private static final Supplier LONG_LIST = LongList::new; /** The Constant FLOAT_LIST. */ private static final Supplier FLOAT_LIST = FloatList::new; /** The Constant DOUBLE_LIST. */ private static final Supplier DOUBLE_LIST = DoubleList::new; /** The Constant LIST. */ @SuppressWarnings("rawtypes") private static final Supplier LIST = ArrayList::new; /** The Constant LINKED_LIST. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_LIST = LinkedList::new; /** The Constant SET. */ @SuppressWarnings("rawtypes") private static final Supplier SET = N::newHashSet; /** The Constant LINKED_HASH_SET. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_SET = N::newLinkedHashSet; /** The Constant TREE_SET. */ @SuppressWarnings("rawtypes") private static final Supplier TREE_SET = TreeSet::new; /** The Constant QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier QUEUE = LinkedList::new; /** The Constant DEQUE. */ @SuppressWarnings("rawtypes") private static final Supplier DEQUE = LinkedList::new; /** The Constant ARRAY_DEQUE. */ @SuppressWarnings("rawtypes") private static final Supplier ARRAY_DEQUE = ArrayDeque::new; /** The Constant LINKED_BLOCKING_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_BLOCKING_QUEUE = LinkedBlockingQueue::new; /** The Constant LINKED_BLOCKING_DEQUE. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_BLOCKING_DEQUE = LinkedBlockingDeque::new; /** The Constant CONCURRENT_LINKED_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_LINKED_QUEUE = ConcurrentLinkedQueue::new; /** The Constant PRIORITY_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier PRIORITY_QUEUE = PriorityQueue::new; /** The Constant MAP. */ @SuppressWarnings("rawtypes") private static final Supplier MAP = N::newHashMap; /** The Constant LINKED_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_MAP = N::newLinkedHashMap; /** The Constant IDENTITY_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier IDENTITY_HASH_MAP = IdentityHashMap::new; /** The Constant TREE_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier TREE_MAP = TreeMap::new; /** The Constant CONCURRENT_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_HASH_MAP = ConcurrentHashMap::new; /** The Constant CONCURRENT_HASH_SET. */ @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_HASH_SET = ConcurrentHashMap::newKeySet; /** The Constant BI_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier BI_MAP = BiMap::new; /** The Constant MULTISET. */ @SuppressWarnings("rawtypes") private static final Supplier MULTISET = Multiset::new; /** The Constant LIST_MULTIMAP. */ @SuppressWarnings("rawtypes") private static final Supplier LIST_MULTIMAP = N::newListMultimap; /** The Constant SET_MULTIMAP. */ @SuppressWarnings("rawtypes") private static final Supplier SET_MULTIMAP = N::newSetMultimap; /** The Constant STRING_BUILDER. */ private static final Supplier STRING_BUILDER = StringBuilder::new; private Suppliers() { } /** * Returns a supplier that always supplies {@code instance}. * * @param * @param instance * @return */ public static Supplier ofInstance(final T instance) { return () -> instance; } public static Supplier ofUUID() { return UUID; } public static Supplier ofGUID() { return GUID; } /** * Of empty boolean array. * * @return */ public static Supplier ofEmptyBooleanArray() { return EMPTY_BOOLEAN_ARRAY; } /** * Of empty char array. * * @return */ public static Supplier ofEmptyCharArray() { return EMPTY_CHAR_ARRAY; } /** * Of empty byte array. * * @return */ public static Supplier ofEmptyByteArray() { return EMPTY_BYTE_ARRAY; } /** * Of empty short array. * * @return */ public static Supplier ofEmptyShortArray() { return EMPTY_SHORT_ARRAY; } /** * Of empty int array. * * @return */ public static Supplier ofEmptyIntArray() { return EMPTY_INT_ARRAY; } /** * Of empty long array. * * @return */ public static Supplier ofEmptyLongArray() { return EMPTY_LONG_ARRAY; } /** * Of empty float array. * * @return */ public static Supplier ofEmptyFloatArray() { return EMPTY_FLOAT_ARRAY; } /** * Of empty double array. * * @return */ public static Supplier ofEmptyDoubleArray() { return EMPTY_DOUBLE_ARRAY; } /** * Of empty string array. * * @return */ public static Supplier ofEmptyStringArray() { return EMPTY_STRING_ARRAY; } /** * Of empty object array. * * @return */ public static Supplier ofEmptyObjectArray() { return EMPTY_OBJECT_ARRAY; } /** * Of empty String. * * @return */ public static Supplier ofEmptyString() { return EMPTY_STRING; } /** * Of boolean list. * * @return */ public static Supplier ofBooleanList() { return BOOLEAN_LIST; } /** * Of char list. * * @return */ public static Supplier ofCharList() { return CHAR_LIST; } /** * Of byte list. * * @return */ public static Supplier ofByteList() { return BYTE_LIST; } /** * Of short list. * * @return */ public static Supplier ofShortList() { return SHORT_LIST; } /** * Of int list. * * @return */ public static Supplier ofIntList() { return INT_LIST; } /** * Of long list. * * @return */ public static Supplier ofLongList() { return LONG_LIST; } /** * Of float list. * * @return */ public static Supplier ofFloatList() { return FLOAT_LIST; } /** * Of double list. * * @return */ public static Supplier ofDoubleList() { return DOUBLE_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofList() { return (Supplier) LIST; } /** * Of linked list. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofLinkedList() { return (Supplier) LINKED_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSet() { return (Supplier) SET; } /** * Of linked hash set. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofLinkedHashSet() { return (Supplier) LINKED_HASH_SET; } /** * Of sorted set. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSortedSet() { return (Supplier) TREE_SET; } /** * Of navigable set. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofNavigableSet() { return (Supplier) TREE_SET; } /** * Of tree set. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofTreeSet() { return (Supplier) TREE_SET; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofQueue() { return (Supplier) QUEUE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofDeque() { return (Supplier) DEQUE; } /** * Of array deque. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofArrayDeque() { return (Supplier) ARRAY_DEQUE; } /** * Of linked blocking queue. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofLinkedBlockingQueue() { return (Supplier) LINKED_BLOCKING_QUEUE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofLinkedBlockingDeque() { return (Supplier) LINKED_BLOCKING_DEQUE; } /** * Of concurrent linked queue. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentLinkedQueue() { return (Supplier) CONCURRENT_LINKED_QUEUE; } /** * Of priority queue. * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofPriorityQueue() { return (Supplier) PRIORITY_QUEUE; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofMap() { return (Supplier) MAP; } /** * Of linked hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofLinkedHashMap() { return (Supplier) LINKED_HASH_MAP; } /** * Of identity hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofIdentityHashMap() { return (Supplier) IDENTITY_HASH_MAP; } /** * Of sorted map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSortedMap() { return (Supplier) TREE_MAP; } /** * Of navigable map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofNavigableMap() { return (Supplier) TREE_MAP; } /** * Of tree map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofTreeMap() { return (Supplier) TREE_MAP; } /** * Of concurrent map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentMap() { return (Supplier) CONCURRENT_HASH_MAP; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentHashMap() { return (Supplier) CONCURRENT_HASH_MAP; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentHashSet() { return (Supplier) CONCURRENT_HASH_SET; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofBiMap() { return (Supplier) BI_MAP; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofMultiset() { return (Supplier) MULTISET; } /** * * @param * @param valueMapType * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofMultiset(final Class valueMapType) { return () -> N.newMultiset(valueMapType); } /** * * @param * @param mapSupplier * @return */ public static Supplier> ofMultiset(final java.util.function.Supplier> mapSupplier) { return () -> N.newMultiset(mapSupplier); } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofListMultimap() { return (Supplier) LIST_MULTIMAP; } /** * * @param * @param * @param mapType * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofListMultimap(final Class mapType) { return () -> N.newListMultimap(mapType); } /** * * @param * @param * @param mapType * @param valueType * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofListMultimap(final Class mapType, final Class valueType) { return () -> N.newListMultimap(mapType, valueType); } /** * * @param * @param * @param mapSupplier * @param valueSupplier * @return */ public static Supplier> ofListMultimap(final java.util.function.Supplier>> mapSupplier, final java.util.function.Supplier> valueSupplier) { return () -> N.newListMultimap(mapSupplier, valueSupplier); } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSetMultimap() { return (Supplier) SET_MULTIMAP; } /** * * @param * @param * @param mapType * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSetMultimap(final Class mapType) { return () -> N.newSetMultimap(mapType); } /** * * @param * @param * @param mapType * @param valueType * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSetMultimap(final Class mapType, final Class valueType) { return () -> N.newSetMultimap(mapType, valueType); } /** * * @param * @param * @param mapSupplier * @param valueSupplier * @return */ public static Supplier> ofSetMultimap(final java.util.function.Supplier>> mapSupplier, final java.util.function.Supplier> valueSupplier) { return () -> N.newSetMultimap(mapSupplier, valueSupplier); } /** * * @param * @param * @param * @param mapSupplier * @param valueSupplier * @return */ public static > Supplier> ofMultimap(final java.util.function.Supplier> mapSupplier, final java.util.function.Supplier valueSupplier) { return () -> N.newMultimap(mapSupplier, valueSupplier); } /** * Of string builder. * * @return */ public static Supplier ofStringBuilder() { return STRING_BUILDER; } @SuppressWarnings("rawtypes") private static final Map, Supplier> collectionSupplierPool = new ConcurrentHashMap<>(); /** * * @param * @param targetType * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static java.util.function.Supplier> ofCollection(final Class targetType) throws IllegalArgumentException { Supplier ret = collectionSupplierPool.get(targetType); if (ret == null) { N.checkArgument(Collection.class.isAssignableFrom(targetType), "'targetType': {} is not a Collection class", targetType); if (Collection.class.equals(targetType) || AbstractCollection.class.equals(targetType) || List.class.equals(targetType) || AbstractList.class.equals(targetType) || ArrayList.class.equals(targetType)) { ret = ofList(); } else if (LinkedList.class.equals(targetType)) { ret = ofLinkedList(); } else if (Set.class.equals(targetType) || AbstractSet.class.equals(targetType) || HashSet.class.equals(targetType)) { ret = ofSet(); } else if (LinkedHashSet.class.equals(targetType)) { ret = ofLinkedHashSet(); } else if (SortedSet.class.isAssignableFrom(targetType)) { ret = ofSortedSet(); } else if (Queue.class.equals(targetType) || AbstractQueue.class.equals(targetType) || Deque.class.equals(targetType)) { return ofDeque(); } else if (BlockingQueue.class.equals(targetType) || LinkedBlockingQueue.class.equals(targetType)) { return ofLinkedBlockingQueue(); } else if (BlockingDeque.class.equals(targetType) || LinkedBlockingDeque.class.equals(targetType)) { return ofLinkedBlockingDeque(); } else if (ConcurrentLinkedQueue.class.equals(targetType)) { return ofConcurrentLinkedQueue(); } else if (PriorityQueue.class.equals(targetType)) { return ofPriorityQueue(); } else if (ImmutableList.class.isAssignableFrom(targetType)) { ret = ofList(); } else if (ImmutableSet.class.isAssignableFrom(targetType)) { ret = ofSet(); } else if (Modifier.isAbstract(targetType.getModifiers())) { throw new IllegalArgumentException("Can't create instance for abstract class: " + targetType); } else { try { if (N.newInstance(targetType) != null) { ret = () -> N.newInstance(targetType); } } catch (final Throwable e) { // NOSONAR // ignore } if (ret == null) { if (targetType.isAssignableFrom(LinkedHashSet.class)) { ret = ofLinkedHashSet(); } else if (targetType.isAssignableFrom(HashSet.class)) { ret = ofSet(); } else if (targetType.isAssignableFrom(LinkedList.class)) { ret = ofLinkedList(); } else if (targetType.isAssignableFrom(ArrayList.class)) { ret = ofList(); } else { throw new IllegalArgumentException("Not able to create instance for collection: " + ClassUtil.getCanonicalClassName(targetType)); } } } collectionSupplierPool.put(targetType, ret); } return ret; } @SuppressWarnings("rawtypes") private static final Map, Supplier> mapSupplierPool = new ConcurrentHashMap<>(); /** * * @param * @param * @param targetType * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static Supplier> ofMap(final Class targetType) throws IllegalArgumentException { Supplier ret = mapSupplierPool.get(targetType); if (ret == null) { N.checkArgument(Map.class.isAssignableFrom(targetType), "'targetType': {} is not a Map class", targetType); if (Map.class.equals(targetType) || AbstractMap.class.equals(targetType) || HashMap.class.equals(targetType) || EnumMap.class.equals(targetType)) { ret = ofMap(); } else if (LinkedHashMap.class.equals(targetType)) { ret = ofLinkedHashMap(); } else if (SortedMap.class.isAssignableFrom(targetType)) { ret = ofSortedMap(); } else if (IdentityHashMap.class.isAssignableFrom(targetType)) { ret = ofIdentityHashMap(); } else if (ConcurrentHashMap.class.isAssignableFrom(targetType)) { ret = ofConcurrentHashMap(); } else if (BiMap.class.isAssignableFrom(targetType)) { ret = ofBiMap(); } else if (ImmutableMap.class.isAssignableFrom(targetType)) { ret = ofMap(); } else if (Modifier.isAbstract(targetType.getModifiers())) { throw new IllegalArgumentException("Not able to create instance for abstract Map: " + targetType); } else { try { if (N.newInstance(targetType) != null) { ret = () -> N.newInstance(targetType); } } catch (final Throwable e) { // NOSONAR // ignore } if (ret == null) { if (targetType.isAssignableFrom(TreeMap.class)) { ret = ofTreeMap(); } else if (targetType.isAssignableFrom(LinkedHashMap.class)) { ret = ofLinkedHashMap(); } else if (targetType.isAssignableFrom(HashMap.class)) { ret = ofMap(); } else { throw new IllegalArgumentException("Not able to create instance for Map: " + targetType); } } } mapSupplierPool.put(targetType, ret); } return ret; } /** * * @param * @param targetClass * @param supplier * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static boolean registerForCollection(final Class targetClass, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(targetClass, cs.targetClass); N.checkArgNotNull(supplier, cs.Supplier); if (N.isBuiltinClass(targetClass)) { throw new IllegalArgumentException("Can't register Supplier with built-in class: " + ClassUtil.getCanonicalClassName(targetClass)); } if (collectionSupplierPool.containsKey(targetClass)) { return false; } return collectionSupplierPool.put(targetClass, Fn.from(supplier)) == null; } /** * * @param * @param targetClass * @param supplier * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static boolean registerForMap(final Class targetClass, final java.util.function.Supplier supplier) throws IllegalArgumentException { N.checkArgNotNull(targetClass, cs.targetClass); N.checkArgNotNull(supplier, cs.Supplier); if (N.isBuiltinClass(targetClass)) { throw new IllegalArgumentException("Can't register Supplier with built-in class: " + ClassUtil.getCanonicalClassName(targetClass)); } if (mapSupplierPool.containsKey(targetClass)) { return false; } return mapSupplierPool.put(targetClass, Fn.from(supplier)) == null; } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static Supplier> ofImmutableList() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static Supplier> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static Supplier> ofImmutableMap() { throw new UnsupportedOperationException(); } /** * * @param * @param * @param supplier * @return * @deprecated */ @Deprecated @SequentialOnly @Stateful public static > Supplier single(final java.util.function.Supplier supplier) { return new Supplier<>() { private C c = null; @Override public C get() { if (c == null) { c = supplier.get(); } else { c.clear(); } return c; } }; } private static final Supplier EXCEPTION = Exception::new; @Beta public static Supplier newException() { return EXCEPTION; } private static final Supplier RUNTIME_EXCEPTION = RuntimeException::new; @Beta public static Supplier newRuntimeException() { return RUNTIME_EXCEPTION; } private static final Supplier NO_SUCH_ELEMENT_EXCEPTION = NoSuchElementException::new; @Beta public static Supplier newNoSuchElementException() { return NO_SUCH_ELEMENT_EXCEPTION; } } /** * The Class Factory. */ @SuppressWarnings({ "java:S1694" }) public abstract static class Factory { /** The Constant BOOLEAN_ARRAY. */ private static final IntFunction BOOLEAN_ARRAY = boolean[]::new; /** The Constant CHAR_ARRAY. */ private static final IntFunction CHAR_ARRAY = char[]::new; /** The Constant BYTE_ARRAY. */ private static final IntFunction BYTE_ARRAY = byte[]::new; /** The Constant SHORT_ARRAY. */ private static final IntFunction SHORT_ARRAY = short[]::new; /** The Constant INT_ARRAY. */ private static final IntFunction INT_ARRAY = int[]::new; /** The Constant LONG_ARRAY. */ private static final IntFunction LONG_ARRAY = long[]::new; /** The Constant FLOAT_ARRAY. */ private static final IntFunction FLOAT_ARRAY = float[]::new; /** The Constant DOUBLE_ARRAY. */ private static final IntFunction DOUBLE_ARRAY = double[]::new; /** The Constant STRING_ARRAY. */ private static final IntFunction STRING_ARRAY = String[]::new; /** The Constant OBJECT_ARRAY. */ private static final IntFunction OBJECT_ARRAY = Object[]::new; /** The Constant BOOLEAN_LIST. */ private static final IntFunction BOOLEAN_LIST = BooleanList::new; /** The Constant CHAR_LIST. */ private static final IntFunction CHAR_LIST = CharList::new; /** The Constant BYTE_LIST. */ private static final IntFunction BYTE_LIST = ByteList::new; /** The Constant SHORT_LIST. */ private static final IntFunction SHORT_LIST = ShortList::new; /** The Constant INT_LIST. */ private static final IntFunction INT_LIST = IntList::new; /** The Constant LONG_LIST. */ private static final IntFunction LONG_LIST = LongList::new; /** The Constant FLOAT_LIST. */ private static final IntFunction FLOAT_LIST = FloatList::new; /** The Constant DOUBLE_LIST. */ private static final IntFunction DOUBLE_LIST = DoubleList::new; /** The Constant LIST_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LIST_FACTORY = ArrayList::new; /** The Constant LINKED_LIST_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_LIST_FACTORY = len -> new LinkedList<>(); /** The Constant SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction SET_FACTORY = N::newHashSet; /** The Constant LINKED_HASH_SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_SET_FACTORY = N::newLinkedHashSet; /** The Constant TREE_SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction TREE_SET_FACTORY = len -> new TreeSet<>(); /** The Constant QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction QUEUE_FACTORY = len -> new LinkedList(); /** The Constant DEQUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction DEQUE_FACTORY = len -> new LinkedList(); /** The Constant ARRAY_DEQUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction ARRAY_DEQUE_FACTORY = ArrayDeque::new; /** The Constant LINKED_BLOCKING_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_BLOCKING_QUEUE_FACTORY = LinkedBlockingQueue::new; /** The Constant ARRAY_BLOCKING_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction ARRAY_BLOCKING_QUEUE_FACTORY = ArrayBlockingQueue::new; /** The Constant LINKED_BLOCKING_DEQUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_BLOCKING_DEQUE_FACTORY = LinkedBlockingDeque::new; /** The Constant CONCURRENT_LINKED_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_LINKED_QUEUE_FACTORY = capacity -> new ConcurrentLinkedQueue(); /** The Constant PRIORITY_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction PRIORITY_QUEUE_FACTORY = PriorityQueue::new; /** The Constant MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction MAP_FACTORY = N::newHashMap; /** The Constant LINKED_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_MAP_FACTORY = N::newLinkedHashMap; /** The Constant IDENTITY_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction IDENTITY_HASH_MAP_FACTORY = N::newIdentityHashMap; /** The Constant TREE_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction TREE_MAP_FACTORY = len -> N.newTreeMap(); /** The Constant CONCURRENT_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_HASH_MAP_FACTORY = N::newConcurrentHashMap; /** The Constant BI_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction BI_MAP_FACTORY = N::newBiMap; /** The Constant MULTISET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction MULTISET_FACTORY = N::newMultiset; /** The Constant LIST_MULTIMAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LIST_MULTIMAP_FACTORY = N::newLinkedListMultimap; /** The Constant SET_MULTIMAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction SET_MULTIMAP_FACTORY = N::newSetMultimap; protected Factory() { // for extension } /** * Of boolean array. * * @return */ public static IntFunction ofBooleanArray() { return BOOLEAN_ARRAY; } /** * Of char array. * * @return */ public static IntFunction ofCharArray() { return CHAR_ARRAY; } /** * Of byte array. * * @return */ public static IntFunction ofByteArray() { return BYTE_ARRAY; } /** * Of short array. * * @return */ public static IntFunction ofShortArray() { return SHORT_ARRAY; } /** * Of int array. * * @return */ public static IntFunction ofIntArray() { return INT_ARRAY; } /** * Of long array. * * @return */ public static IntFunction ofLongArray() { return LONG_ARRAY; } /** * Of float array. * * @return */ public static IntFunction ofFloatArray() { return FLOAT_ARRAY; } /** * Of double array. * * @return */ public static IntFunction ofDoubleArray() { return DOUBLE_ARRAY; } /** * Of string array. * * @return */ public static IntFunction ofStringArray() { return STRING_ARRAY; } /** * Of object array. * * @return */ public static IntFunction ofObjectArray() { return OBJECT_ARRAY; } /** * Of boolean list. * * @return */ public static IntFunction ofBooleanList() { return BOOLEAN_LIST; } /** * Of char list. * * @return */ public static IntFunction ofCharList() { return CHAR_LIST; } /** * Of byte list. * * @return */ public static IntFunction ofByteList() { return BYTE_LIST; } /** * Of short list. * * @return */ public static IntFunction ofShortList() { return SHORT_LIST; } /** * Of int list. * * @return */ public static IntFunction ofIntList() { return INT_LIST; } /** * Of long list. * * @return */ public static IntFunction ofLongList() { return LONG_LIST; } /** * Of float list. * * @return */ public static IntFunction ofFloatList() { return FLOAT_LIST; } /** * Of double list. * * @return */ public static IntFunction ofDoubleList() { return DOUBLE_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofList() { return (IntFunction) LIST_FACTORY; } /** * Of linked list. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedList() { return (IntFunction) LINKED_LIST_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofSet() { return (IntFunction) SET_FACTORY; } /** * Of linked hash set. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedHashSet() { return (IntFunction) LINKED_HASH_SET_FACTORY; } /** * Of sorted set. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofSortedSet() { return (IntFunction) TREE_SET_FACTORY; } /** * Of navigable set. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofNavigableSet() { return (IntFunction) TREE_SET_FACTORY; } /** * Of tree set. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofTreeSet() { return (IntFunction) TREE_SET_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofQueue() { return (IntFunction) QUEUE_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofDeque() { return (IntFunction) DEQUE_FACTORY; } /** * Of array deque. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofArrayDeque() { return (IntFunction) ARRAY_DEQUE_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedBlockingQueue() { return (IntFunction) LINKED_BLOCKING_QUEUE_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofArrayBlockingQueue() { return (IntFunction) ARRAY_BLOCKING_QUEUE_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedBlockingDeque() { return (IntFunction) LINKED_BLOCKING_DEQUE_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentLinkedQueue() { return (IntFunction) CONCURRENT_LINKED_QUEUE_FACTORY; } /** * Of priority queue. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofPriorityQueue() { return (IntFunction) PRIORITY_QUEUE_FACTORY; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofMap() { return (IntFunction) MAP_FACTORY; } /** * Of linked hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedHashMap() { return (IntFunction) LINKED_HASH_MAP_FACTORY; } /** * Of identity hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofIdentityHashMap() { return (IntFunction) IDENTITY_HASH_MAP_FACTORY; } /** * Of sorted map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofSortedMap() { return (IntFunction) TREE_MAP_FACTORY; } /** * Of navigable map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofNavigableMap() { return (IntFunction) TREE_MAP_FACTORY; } /** * Of tree map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofTreeMap() { return (IntFunction) TREE_MAP_FACTORY; } /** * Of concurrent map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentMap() { return (IntFunction) CONCURRENT_HASH_MAP_FACTORY; } /** * Of concurrent hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentHashMap() { return (IntFunction) CONCURRENT_HASH_MAP_FACTORY; } /** * Of bi map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofBiMap() { return (IntFunction) BI_MAP_FACTORY; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofMultiset() { return (IntFunction) MULTISET_FACTORY; } /** * Of list multimap. * * @param the key type * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofListMultimap() { return (IntFunction) LIST_MULTIMAP_FACTORY; } /** * Of set multimap. * * @param the key type * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofSetMultimap() { return (IntFunction) SET_MULTIMAP_FACTORY; } /** * * @return a new created {@code IntFunction} whose {@code apply} will return the same {@code DisposableObjArray} which is defined as a private field. */ @Beta @SequentialOnly @Stateful public static IntFunction ofDisposableArray() { return new IntFunction<>() { private DisposableObjArray ret = null; @Override public DisposableObjArray apply(final int len) { if (ret == null) { ret = DisposableObjArray.wrap(new Object[len]); } return ret; } }; } /** * * @param * @param componentType * @return a new created {@code IntFunction} whose {@code apply} will return the same {@code DisposableArray} which is defined as a private field. */ @Beta @SequentialOnly @Stateful public static IntFunction> ofDisposableArray(final Class componentType) { return new IntFunction<>() { private DisposableArray ret = null; @Override public DisposableArray apply(final int len) { if (ret == null) { ret = DisposableArray.wrap(N.newArray(componentType, len)); } return ret; } }; } @SuppressWarnings("rawtypes") private static final Map, IntFunction> collectionCreatorPool = new ConcurrentHashMap<>(); /** * * @param * @param targetType * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static IntFunction> ofCollection(final Class targetType) throws IllegalArgumentException { IntFunction ret = collectionCreatorPool.get(targetType); if (ret == null) { N.checkArgument(Collection.class.isAssignableFrom(targetType), "'targetType': {} is not a Collection class", targetType); if (Collection.class.equals(targetType) || AbstractCollection.class.equals(targetType) || List.class.equals(targetType) || AbstractList.class.equals(targetType) || ArrayList.class.equals(targetType)) { ret = ofList(); } else if (LinkedList.class.equals(targetType)) { ret = ofLinkedList(); } else if (Set.class.equals(targetType) || AbstractSet.class.equals(targetType) || HashSet.class.equals(targetType)) { ret = ofSet(); } else if (LinkedHashSet.class.equals(targetType)) { ret = ofLinkedHashSet(); } else if (SortedSet.class.isAssignableFrom(targetType)) { ret = ofSortedSet(); } else if (Queue.class.equals(targetType) || AbstractQueue.class.equals(targetType) || Deque.class.equals(targetType)) { return ofDeque(); } else if (BlockingQueue.class.equals(targetType) || LinkedBlockingQueue.class.equals(targetType)) { return ofLinkedBlockingQueue(); } else if (ArrayBlockingQueue.class.equals(targetType)) { return ofArrayBlockingQueue(); } else if (BlockingDeque.class.equals(targetType) || LinkedBlockingDeque.class.equals(targetType)) { return ofLinkedBlockingDeque(); } else if (ConcurrentLinkedQueue.class.equals(targetType)) { return ofConcurrentLinkedQueue(); } else if (PriorityQueue.class.equals(targetType)) { return ofPriorityQueue(); } else if (ImmutableList.class.isAssignableFrom(targetType)) { ret = ofList(); } else if (ImmutableSet.class.isAssignableFrom(targetType)) { ret = ofSet(); } else if (Modifier.isAbstract(targetType.getModifiers())) { throw new IllegalArgumentException("Not able to create instance for collection: " + targetType); } else { try { final Constructor constructor = ClassUtil.getDeclaredConstructor(targetType, int.class); //noinspection ConstantValue if (constructor != null && N.invoke(constructor, 9) != null) { // magic number? ret = size -> { try { return (Collection) N.invoke(constructor, size); } catch (final Throwable e) { // NOSONAR throw new IllegalArgumentException("Not able to create instance for collection: " + targetType, e); } }; } } catch (final Throwable e) { // NOSONAR // ignore } try { if (ret == null && N.newInstance(targetType) != null) { ret = size -> { try { return (Collection) N.newInstance(targetType); } catch (final Exception e) { throw new IllegalArgumentException("Not able to create instance for collection: " + targetType, e); } }; } } catch (final Throwable e) { // NOSONAR // ignore } if (ret == null) { if (targetType.isAssignableFrom(LinkedHashSet.class)) { ret = ofLinkedHashSet(); } else if (targetType.isAssignableFrom(HashSet.class)) { ret = ofSet(); } else if (targetType.isAssignableFrom(LinkedList.class)) { ret = ofLinkedList(); } else if (targetType.isAssignableFrom(ArrayList.class)) { ret = ofList(); } else { throw new IllegalArgumentException("Not able to create instance for collection: " + targetType); } } } collectionCreatorPool.put(targetType, ret); } return ret; } @SuppressWarnings("rawtypes") private static final Map, IntFunction> mapCreatorPool = new ConcurrentHashMap<>(); /** * * @param * @param * @param targetType * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static IntFunction> ofMap(final Class targetType) throws IllegalArgumentException { IntFunction ret = mapCreatorPool.get(targetType); if (ret == null) { N.checkArgument(Map.class.isAssignableFrom(targetType), "'targetType': {} is not a Map class", targetType); if (Map.class.equals(targetType) || AbstractMap.class.equals(targetType) || HashMap.class.equals(targetType) || EnumMap.class.equals(targetType)) { ret = ofMap(); } else if (LinkedHashMap.class.equals(targetType)) { ret = ofLinkedHashMap(); } else if (SortedMap.class.isAssignableFrom(targetType)) { ret = ofSortedMap(); } else if (IdentityHashMap.class.isAssignableFrom(targetType)) { ret = ofIdentityHashMap(); } else if (ConcurrentHashMap.class.isAssignableFrom(targetType)) { ret = ofConcurrentHashMap(); } else if (BiMap.class.isAssignableFrom(targetType)) { ret = ofBiMap(); } else if (ImmutableMap.class.isAssignableFrom(targetType)) { ret = ofMap(); } else if (Modifier.isAbstract(targetType.getModifiers())) { throw new IllegalArgumentException("Not able to create instance for abstract Map: " + targetType); } else { try { final Constructor constructor = ClassUtil.getDeclaredConstructor(targetType, int.class); //noinspection ConstantValue if (constructor != null && N.invoke(constructor, 9) != null) { // magic number? ret = size -> { try { return (Map) N.invoke(constructor, size); } catch (final Throwable e) { // NOSONAR throw new IllegalArgumentException("Not able to create instance for Map: " + targetType, e); } }; } } catch (final Throwable e) { // NOSONAR // ignore } try { if (ret == null && N.newInstance(targetType) != null) { ret = size -> { try { return (Map) N.newInstance(targetType); } catch (final Exception e) { throw new IllegalArgumentException("Not able to create instance for Map: " + targetType, e); } }; } } catch (final Throwable e) { // NOSONAR // ignore } if (ret == null) { if (targetType.isAssignableFrom(TreeMap.class)) { ret = ofTreeMap(); } else if (targetType.isAssignableFrom(LinkedHashMap.class)) { ret = ofLinkedHashMap(); } else if (targetType.isAssignableFrom(HashMap.class)) { ret = ofMap(); } else { throw new IllegalArgumentException("Not able to create instance for Map: " + ClassUtil.getCanonicalClassName(targetType)); } } } mapCreatorPool.put(targetType, ret); } return ret; } /** * * @param * @param targetClass * @param creator * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static boolean registerForCollection(final Class targetClass, final java.util.function.IntFunction creator) throws IllegalArgumentException { N.checkArgNotNull(targetClass, cs.targetClass); N.checkArgNotNull(creator, cs.creator); if (N.isBuiltinClass(targetClass)) { throw new IllegalArgumentException("Can't register IntFunction with built-in class: " + ClassUtil.getCanonicalClassName(targetClass)); } if (collectionCreatorPool.containsKey(targetClass)) { return false; } return collectionCreatorPool.put(targetClass, Fn.from(creator)) == null; } /** * * @param * @param targetClass * @param creator * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static boolean registerForMap(final Class targetClass, final java.util.function.IntFunction creator) throws IllegalArgumentException { N.checkArgNotNull(targetClass, cs.targetClass); N.checkArgNotNull(creator, cs.creator); if (N.isBuiltinClass(targetClass)) { throw new IllegalArgumentException("Can't register IntFunction with built-in class: " + ClassUtil.getCanonicalClassName(targetClass)); } if (mapCreatorPool.containsKey(targetClass)) { return false; } return mapCreatorPool.put(targetClass, Fn.from(creator)) == null; } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static IntFunction> ofImmutableList() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static IntFunction> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception * @deprecated unsupported operation. */ @Deprecated public static IntFunction> ofImmutableMap() { throw new UnsupportedOperationException(); } /** * * @param * @param * @param supplier * @return * @deprecated */ @Deprecated @SequentialOnly @Stateful public static > IntFunction single(final java.util.function.IntFunction supplier) { return new IntFunction<>() { private C c = null; @Override public C apply(final int t) { if (c == null) { c = supplier.apply(t); } else { c.clear(); } return c; } }; } } /** * The Class */ public static final class IntFunctions extends Factory { private IntFunctions() { } } /** * The Class Predicates. */ public static final class Predicates { private Predicates() { } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param predicate * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Predicate indexed(final IntObjPredicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return new Predicate<>() { private final MutableInt idx = new MutableInt(0); @Override public boolean test(final T t) { return predicate.test(idx.getAndIncrement(), t); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @return */ @Beta @SequentialOnly @Stateful public static Predicate distinct() { return new Predicate<>() { private final Set set = N.newHashSet(); @Override public boolean test(final T value) { return set.add(value); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param mapper * @return */ @Beta @SequentialOnly @Stateful public static Predicate distinctBy(final java.util.function.Function mapper) { return new Predicate<>() { private final Set set = N.newHashSet(); @Override public boolean test(final T value) { return set.add(mapper.apply(value)); } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @return */ @Beta @SequentialOnly @Stateful public static Predicate concurrentDistinct() { return new Predicate<>() { private final Map map = new ConcurrentHashMap<>(); @Override public boolean test(final T value) { return map.put(value, NONE) == null; } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param mapper * @return */ @Beta @SequentialOnly @Stateful public static Predicate concurrentDistinctBy(final java.util.function.Function mapper) { return new Predicate<>() { private final Map map = new ConcurrentHashMap<>(); @Override public boolean test(final T value) { return map.put(mapper.apply(value), NONE) == null; } }; } /** * Returns a stateful {@code Predicate}. Don't save or cache for reuse or use it in parallel stream. * Remove the continuous repeat elements. * * @param * @return */ @Beta @SequentialOnly @Stateful public static Predicate skipRepeats() { return new Predicate<>() { private T pre = (T) NONE; @Override public boolean test(final T value) { final boolean res = pre == NONE || !N.equals(value, pre); pre = value; return res; } }; } } /** * The Class BiPredicates. */ public static final class BiPredicates { /** The Constant ALWAYS_TRUE. */ @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_TRUE = (t, u) -> true; /** The Constant ALWAYS_FALSE. */ @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_FALSE = (t, u) -> false; /** The Constant EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate EQUAL = N::equals; /** The Constant NOT_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate NOT_EQUAL = (t, u) -> !N.equals(t, u); /** The Constant GREATER_THAN. */ @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_THAN = (t, u) -> N.compare(t, u) > 0; /** The Constant GREATER_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_EQUAL = (t, u) -> N.compare(t, u) >= 0; /** The Constant LESS_THAN. */ @SuppressWarnings("rawtypes") private static final BiPredicate LESS_THAN = (t, u) -> N.compare(t, u) < 0; /** The Constant LESS_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate LESS_EQUAL = (t, u) -> N.compare(t, u) <= 0; private BiPredicates() { } /** * * @param * @param * @return */ public static BiPredicate alwaysTrue() { return ALWAYS_TRUE; } /** * * @param * @param * @return */ public static BiPredicate alwaysFalse() { return ALWAYS_FALSE; } /** * Returns a stateful {@code BiPredicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param predicate * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static BiPredicate indexed(final IntBiObjPredicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return new BiPredicate<>() { private final MutableInt idx = new MutableInt(0); @Override public boolean test(final T t, final U u) { return predicate.test(idx.getAndIncrement(), t, u); } }; } } /** * The Class TriPredicates. */ public static final class TriPredicates { /** The Constant ALWAYS_TRUE. */ @SuppressWarnings("rawtypes") private static final TriPredicate ALWAYS_TRUE = (a, b, c) -> true; /** The Constant ALWAYS_FALSE. */ @SuppressWarnings({ "rawtypes" }) private static final TriPredicate ALWAYS_FALSE = (a, b, c) -> false; private TriPredicates() { } /** * * @param * @param * @param * @return */ public static TriPredicate alwaysTrue() { return ALWAYS_TRUE; } /** * * @param * @param * @param * @return */ public static TriPredicate alwaysFalse() { return ALWAYS_FALSE; } } /** * The Class Consumers. */ public static final class Consumers { private Consumers() { } /** * Returns a stateful {@code BiPredicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param action * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Consumer indexed(final IntObjConsumer action) throws IllegalArgumentException { N.checkArgNotNull(action); return new Consumer<>() { private final MutableInt idx = new MutableInt(0); @Override public void accept(final T t) { action.accept(idx.getAndIncrement(), t); } }; } } /** * The Class BiConsumers. */ public static final class BiConsumers { /** The Constant DO_NOTHING. */ @SuppressWarnings("rawtypes") private static final BiConsumer DO_NOTHING = (t, u) -> { // do nothing. }; /** The Constant ADD. */ private static final BiConsumer, Object> ADD = Collection::add; /** The Constant ADD_ALL. */ private static final BiConsumer, Collection> ADD_ALL = Collection::addAll; /** The Constant ADD_ALL_2. */ @SuppressWarnings("rawtypes") private static final BiConsumer ADD_ALL_2 = PrimitiveList::addAll; /** The Constant REMOVE. */ private static final BiConsumer, Object> REMOVE = Collection::remove; /** The Constant REMOVE_ALL. */ private static final BiConsumer, Collection> REMOVE_ALL = Collection::removeAll; /** The Constant REMOVE_ALL_2. */ @SuppressWarnings("rawtypes") private static final BiConsumer REMOVE_ALL_2 = PrimitiveList::removeAll; /** The Constant PUT. */ private static final BiConsumer, Map.Entry> PUT = (t, u) -> t.put(u.getKey(), u.getValue()); /** The Constant PUT_ALL. */ private static final BiConsumer, Map> PUT_ALL = Map::putAll; /** The Constant REMOVE_BY_KEY. */ private static final BiConsumer, Object> REMOVE_BY_KEY = Map::remove; /** The Constant MERGE. */ private static final BiConsumer MERGE = Joiner::merge; /** The Constant APPEND. */ private static final BiConsumer APPEND = StringBuilder::append; private BiConsumers() { } /** * * @param * @param * @return */ public static BiConsumer doNothing() { return DO_NOTHING; } /** * * @param * @param * @return */ public static > BiConsumer ofAdd() { return (BiConsumer) ADD; } /** * Of add all. * * @param * @param * @return */ public static > BiConsumer ofAddAll() { return (BiConsumer) ADD_ALL; } /** * Of add alll. * * @param * @return */ @SuppressWarnings("rawtypes") public static BiConsumer ofAddAlll() { return (BiConsumer) ADD_ALL_2; } /** * * @param * @param * @return */ public static > BiConsumer ofRemove() { return (BiConsumer) REMOVE; } /** * Of remove all. * * @param * @param * @return */ public static > BiConsumer ofRemoveAll() { return (BiConsumer) REMOVE_ALL; } /** * Of remove alll. * * @param * @return */ @SuppressWarnings("rawtypes") public static BiConsumer ofRemoveAlll() { return (BiConsumer) REMOVE_ALL_2; } /** * * @param the key type * @param the value type * @param * @param * @return */ public static , E extends Map.Entry> BiConsumer ofPut() { return (BiConsumer) PUT; } /** * Of put all. * * @param the key type * @param the value type * @param * @return */ public static > BiConsumer ofPutAll() { return (BiConsumer) PUT_ALL; } /** * Of remove by key. * * @param the key type * @param the value type * @param * @return */ public static > BiConsumer ofRemoveByKey() { return (BiConsumer) REMOVE_BY_KEY; } public static BiConsumer ofMerge() { return MERGE; } /** * * @param * @return */ public static BiConsumer ofAppend() { return (BiConsumer) APPEND; } /** * Returns a stateful {@code BiPredicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param action * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static BiConsumer indexed(final IntBiObjConsumer action) throws IllegalArgumentException { N.checkArgNotNull(action); return new BiConsumer<>() { private final MutableInt idx = new MutableInt(0); @Override public void accept(final T t, final U u) { action.accept(idx.getAndIncrement(), t, u); } }; } } /** * The Class TriConsumers. */ public static final class TriConsumers { private TriConsumers() { } } /** * The Class Functions. */ public static final class Functions { private Functions() { } /** * Returns a stateful {@code Function}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param func * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static Function indexed(final IntObjFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return new Function<>() { private final MutableInt idx = new MutableInt(0); @Override public R apply(final T t) { return func.apply(idx.getAndIncrement(), t); } }; } } /** * The Class BiFunctions. */ public static final class BiFunctions { /** The Constant RETURN_FIRST. */ private static final BiFunction RETURN_FIRST = (t, u) -> t; /** The Constant RETURN_SECOND. */ private static final BiFunction RETURN_SECOND = (t, u) -> u; /** The Constant ADD. */ private static final BiFunction, Object, Collection> ADD = (t, u) -> { t.add(u); return t; }; /** The Constant ADD_ALL. */ private static final BiFunction, Collection, Collection> ADD_ALL = (t, u) -> { t.addAll(u); return t; }; /** The Constant ADD_ALL_2. */ @SuppressWarnings("rawtypes") private static final BiFunction ADD_ALL_2 = (t, u) -> { t.addAll(u); return t; }; /** The Constant REMOVE. */ private static final BiFunction, Object, Collection> REMOVE = (t, u) -> { t.remove(u); return t; }; /** The Constant REMOVE_ALL. */ private static final BiFunction, Collection, Collection> REMOVE_ALL = (t, u) -> { t.removeAll(u); return t; }; /** The Constant REMOVE_ALL_2. */ @SuppressWarnings("rawtypes") private static final BiFunction REMOVE_ALL_2 = (t, u) -> { t.removeAll(u); return t; }; /** The Constant PUT. */ private static final BiFunction, Map.Entry, Map> PUT = (t, u) -> { t.put(u.getKey(), u.getValue()); return t; }; /** The Constant PUT_ALL. */ private static final BiFunction, Map, Map> PUT_ALL = (t, u) -> { t.putAll(u); return t; }; /** The Constant REMOVE_BY_KEY. */ private static final BiFunction, Object, Map> REMOVE_BY_KEY = (t, u) -> { t.remove(u); return t; }; /** The Constant MERGE. */ private static final BiFunction MERGE = Joiner::merge; /** The Constant APPEND. */ private static final BiFunction APPEND = StringBuilder::append; private BiFunctions() { } /** * * @param * @param * @return */ public static BiFunction selectFirst() { return (BiFunction) RETURN_FIRST; } /** * * @param * @param * @return */ public static BiFunction selectSecond() { return (BiFunction) RETURN_SECOND; } /** * * @param * @param * @return */ public static > BiFunction ofAdd() { return (BiFunction) ADD; } /** * Of add all. * * @param * @param * @return */ public static > BiFunction ofAddAll() { return (BiFunction) ADD_ALL; } /** * Of add alll. * * @param * @return */ @SuppressWarnings("rawtypes") public static BiFunction ofAddAlll() { return (BiFunction) ADD_ALL_2; } /** * * @param * @param * @return */ public static > BiFunction ofRemove() { return (BiFunction) REMOVE; } /** * Of remove all. * * @param * @param * @return */ public static > BiFunction ofRemoveAll() { return (BiFunction) REMOVE_ALL; } /** * Of remove alll. * * @param * @return */ @SuppressWarnings("rawtypes") public static BiFunction ofRemoveAlll() { return (BiFunction) REMOVE_ALL_2; } /** * * @param the key type * @param the value type * @param * @param * @return */ public static , E extends Map.Entry> BiFunction ofPut() { return (BiFunction) PUT; } /** * Of put all. * * @param the key type * @param the value type * @param * @return */ public static > BiFunction ofPutAll() { return (BiFunction) PUT_ALL; } /** * Of remove by key. * * @param the key type * @param the value type * @param * @return */ public static > BiFunction ofRemoveByKey() { return (BiFunction) REMOVE_BY_KEY; } public static BiFunction ofMerge() { return MERGE; } /** * * @param * @return */ public static BiFunction ofAppend() { return (BiFunction) APPEND; } /** * Returns a stateful {@code BiPredicate}. Don't save or cache for reuse or use it in parallel stream. * * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ @Beta @SequentialOnly @Stateful public static BiFunction indexed(final IntBiObjFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return new BiFunction<>() { private final MutableInt idx = new MutableInt(0); @Override public R apply(final T t, final U u) { return func.apply(idx.getAndIncrement(), t, u); } }; } } /** * The Class TriFunctions. */ public static final class TriFunctions { private TriFunctions() { } } /** * The Class BinaryOperators. */ public static final class BinaryOperators { /** The Constant THROWING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator THROWING_MERGER = (t, u) -> { throw new IllegalStateException(String.format("Duplicate key (attempted merging values %s and %s)", t, u)); }; /** The Constant IGNORING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator IGNORING_MERGER = (t, u) -> t; /** The Constant REPLACING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator REPLACING_MERGER = (t, u) -> u; /** The Constant ADD_ALL_TO_FIRST. */ private static final BinaryOperator> ADD_ALL_TO_FIRST = (t, u) -> { t.addAll(u); return t; }; /** The Constant ADD_ALL_TO_BIGGER. */ private static final BinaryOperator> ADD_ALL_TO_BIGGER = (t, u) -> { if (t.size() >= u.size()) { t.addAll(u); return t; } else { u.addAll(t); return u; } }; /** The Constant REMOVE_ALL_FROM_FIRST. */ private static final BinaryOperator> REMOVE_ALL_FROM_FIRST = (t, u) -> { t.removeAll(u); return t; }; /** The Constant PUT_ALL_TO_FIRST. */ private static final BinaryOperator> PUT_ALL_TO_FIRST = (t, u) -> { t.putAll(u); return t; }; /** The Constant PUT_ALL_TO_BIGGER. */ private static final BinaryOperator> PUT_ALL_TO_BIGGER = (t, u) -> { if (t.size() >= u.size()) { t.putAll(u); return t; } else { u.putAll(t); return u; } }; /** The Constant MERGE_TO_FIRST. */ private static final BinaryOperator MERGE_TO_FIRST = Joiner::merge; /** The Constant MERGE_TO_BIGGER. */ private static final BinaryOperator MERGE_TO_BIGGER = (t, u) -> { if (t.length() >= u.length()) { return t.merge(u); } else { return u.merge(t); } }; /** The Constant APPEND_TO_FIRST. */ private static final BinaryOperator APPEND_TO_FIRST = StringBuilder::append; /** The Constant APPEND_TO_BIGGER. */ private static final BinaryOperator APPEND_TO_BIGGER = (t, u) -> { if (t.length() >= u.length()) { return t.append(u); } else { return u.append(t); } }; /** The Constant CONCAT. */ private static final BinaryOperator CONCAT = (t, u) -> t + u; /** The Constant ADD_INTEGER. */ private static final BinaryOperator ADD_INTEGER = Integer::sum; /** The Constant ADD_LONG. */ private static final BinaryOperator ADD_LONG = Long::sum; /** The Constant ADD_DOUBLE. */ private static final BinaryOperator ADD_DOUBLE = Double::sum; /** The Constant ADD_BIG_INTEGER. */ private static final BinaryOperator ADD_BIG_INTEGER = BigInteger::add; /** The Constant ADD_BIG_DECIMAL. */ private static final BinaryOperator ADD_BIG_DECIMAL = BigDecimal::add; private BinaryOperators() { } /** * Of add all. * * @param * @param * @return * @deprecated replaced by {@code #ofAddAllToFirst()} */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAll() { return (BinaryOperator) ADD_ALL_TO_FIRST; } /** * Of add all to first. * * @param * @param * @return */ @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAllToFirst() { return (BinaryOperator) ADD_ALL_TO_FIRST; } /** * Of add all to bigger. * * @param * @param * @return */ @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAllToBigger() { return (BinaryOperator) ADD_ALL_TO_BIGGER; } /** * Of remove all. * * @param * @param * @return * @deprecated replaced by {@code #ofRemoveAllFromFirst()}. */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofRemoveAll() { return (BinaryOperator) REMOVE_ALL_FROM_FIRST; } /** * Of remove all from first. * * @param * @param * @return */ @SuppressWarnings("unchecked") public static > BinaryOperator ofRemoveAllFromFirst() { return (BinaryOperator) REMOVE_ALL_FROM_FIRST; } /** * Of put all. * * @param the key type * @param the value type * @param * @return * @deprecated replaced by {@code #ofPutAllToFirst()} */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofPutAll() { return (BinaryOperator) PUT_ALL_TO_FIRST; } /** * Of put all to first. * * @param the key type * @param the value type * @param * @return */ @SuppressWarnings("unchecked") public static > BinaryOperator ofPutAllToFirst() { return (BinaryOperator) PUT_ALL_TO_FIRST; } /** * Of put all to bigger. * * @param the key type * @param the value type * @param * @return */ @SuppressWarnings("unchecked") public static > BinaryOperator ofPutAllToBigger() { return (BinaryOperator) PUT_ALL_TO_BIGGER; } /** * * @return * @deprecated replaced by {@code #ofMergeToFirst}. */ @Deprecated public static BinaryOperator ofMerge() { return MERGE_TO_FIRST; } /** * Of merge to first. * * @return */ public static BinaryOperator ofMergeToFirst() { return MERGE_TO_FIRST; } /** * Of merge to bigger. * * @return */ public static BinaryOperator ofMergeToBigger() { return MERGE_TO_BIGGER; } /** * * @return * @deprecated replaced by {@code #ofAppendToFirst()} */ @Deprecated public static BinaryOperator ofAppend() { return APPEND_TO_FIRST; } /** * Of append to first. * * @return */ public static BinaryOperator ofAppendToFirst() { return APPEND_TO_FIRST; } /** * Of append to bigger. * * @return */ public static BinaryOperator ofAppendToBigger() { return APPEND_TO_BIGGER; } public static BinaryOperator ofConcat() { return CONCAT; } /** * Of add int. * * @return */ public static BinaryOperator ofAddInt() { return ADD_INTEGER; } /** * Of add long. * * @return */ public static BinaryOperator ofAddLong() { return ADD_LONG; } /** * Of add double. * * @return */ public static BinaryOperator ofAddDouble() { return ADD_DOUBLE; } /** * Of add big integer. * * @return */ public static BinaryOperator ofAddBigInteger() { return ADD_BIG_INTEGER; } /** * Of add big decimal. * * @return */ public static BinaryOperator ofAddBigDecimal() { return ADD_BIG_DECIMAL; } } /** * The Class UnaryOperators. */ public static final class UnaryOperators { /** The Constant IDENTITY. */ @SuppressWarnings("rawtypes") private static final UnaryOperator IDENTITY = t -> t; private UnaryOperators() { } /** * * @param * @return */ public static UnaryOperator identity() { return IDENTITY; } } /** * The Class Entries. */ public static final class Entries { private Entries() { } /** * * @param the key type * @param the value type * @param * @param f * @return * @throws IllegalArgumentException */ public static Function, T> f(final java.util.function.BiFunction f) throws IllegalArgumentException { N.checkArgNotNull(f, cs.BiFunction); return e -> f.apply(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param p * @return * @throws IllegalArgumentException */ public static Predicate> p(final java.util.function.BiPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p, cs.BiPredicate); return e -> p.test(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param c * @return * @throws IllegalArgumentException */ public static Consumer> c(final java.util.function.BiConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c, cs.BiConsumer); return e -> c.accept(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param * @param * @param f * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function, T, E> ef( final Throwables.BiFunction f) throws IllegalArgumentException { N.checkArgNotNull(f, cs.BiFunction); return e -> f.apply(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param * @param p * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate, E> ep(final Throwables.BiPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p, cs.BiPredicate); return e -> p.test(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param * @param c * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer, E> ec(final Throwables.BiConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c, cs.BiConsumer); return e -> c.accept(e.getKey(), e.getValue()); } /** * * @param the key type * @param the value type * @param * @param f * @return * @throws IllegalArgumentException */ public static Function, T> ff(final Throwables.BiFunction f) throws IllegalArgumentException { N.checkArgNotNull(f, cs.BiFunction); return e -> { try { return f.apply(e.getKey(), e.getValue()); } catch (final Exception ex) { throw ExceptionUtil.toRuntimeException(ex, true); } }; } /** * * @param the key type * @param the value type * @param p * @return * @throws IllegalArgumentException */ public static Predicate> pp(final Throwables.BiPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p, cs.BiPredicate); return e -> { try { return p.test(e.getKey(), e.getValue()); } catch (final Exception ex) { throw ExceptionUtil.toRuntimeException(ex, true); } }; } /** * * @param the key type * @param the value type * @param c * @return * @throws IllegalArgumentException */ public static Consumer> cc(final Throwables.BiConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c, cs.BiConsumer); return e -> { try { c.accept(e.getKey(), e.getValue()); } catch (final Exception ex) { throw ExceptionUtil.toRuntimeException(ex, true); } }; } } /** * The Class Pairs. */ public static final class Pairs { /** The Constant PAIR_TO_LIST. */ @SuppressWarnings("rawtypes") private static final Function PAIR_TO_LIST = t -> N.asList(t.left, t.right); /** The Constant PAIR_TO_SET. */ @SuppressWarnings("rawtypes") private static final Function PAIR_TO_SET = t -> N.asSet(t.left, t.right); private Pairs() { } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, List> toList() { return (Function) PAIR_TO_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, Set> toSet() { return (Function) PAIR_TO_SET; } } /** * The Class Triples. */ public static final class Triples { /** The Constant TRIPLE_TO_LIST. */ @SuppressWarnings("rawtypes") private static final Function TRIPLE_TO_LIST = t -> N.asList(t.left, t.middle, t.right); /** The Constant TRIPLE_TO_SET. */ @SuppressWarnings("rawtypes") private static final Function TRIPLE_TO_SET = t -> N.asSet(t.left, t.middle, t.right); private Triples() { } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, List> toList() { return (Function) TRIPLE_TO_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, Set> toSet() { return (Function) TRIPLE_TO_SET; } } /** * The Class Disposables. */ public static final class Disposables { /** The Constant CLONE. */ @SuppressWarnings("rawtypes") private static final Function CLONE = DisposableArray::copy; /** The Constant TO_STRING. */ @SuppressWarnings("rawtypes") private static final Function TO_STRING = DisposableArray::toString; private Disposables() { } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static > Function cloneArray() { return (Function) CLONE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function toStr() { return (Function) TO_STRING; } /** * * @param * @param delimiter * @return */ @SuppressWarnings("rawtypes") public static Function join(final String delimiter) { return t -> t.join(delimiter); } } /** * Utility class for {@code CharPredicate/Function/Consumer}. * * */ public static final class FC { /** The Constant IS_ZERO. */ private static final CharPredicate IS_ZERO = t -> t == 0; /** The Constant IS_WHITE_SPACE. */ private static final CharPredicate IS_WHITESPACE = Character::isWhitespace; /** The Constant EQUAL. */ private static final CharBiPredicate EQUAL = (t, u) -> t == u; /** The Constant NOT_EQUAL. */ private static final CharBiPredicate NOT_EQUAL = (t, u) -> t != u; /** The Constant GREATER_THAN. */ private static final CharBiPredicate GREATER_THAN = (t, u) -> t > u; /** The Constant GREATER_EQUAL. */ private static final CharBiPredicate GREATER_EQUAL = (t, u) -> t >= u; /** The Constant LESS_THAN. */ private static final CharBiPredicate LESS_THAN = (t, u) -> t < u; /** The Constant LESS_EQUAL. */ private static final CharBiPredicate LESS_EQUAL = (t, u) -> t <= u; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FC() { } public static CharPredicate isZero() { return IS_ZERO; } public static CharPredicate isWhitespace() { return IS_WHITESPACE; } public static CharBiPredicate equal() { return EQUAL; } public static CharBiPredicate notEqual() { return NOT_EQUAL; } public static CharBiPredicate greaterThan() { return GREATER_THAN; } public static CharBiPredicate greaterEqual() { return GREATER_EQUAL; } public static CharBiPredicate lessThan() { return LESS_THAN; } public static CharBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToCharFunction unbox() { return ToCharFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static CharPredicate p(final CharPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static CharFunction f(final CharFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static CharConsumer c(final CharConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** * Returns a stateful {@code CharBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static CharBiFunction alternated() { return new CharBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final char t, final char u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class CharBinaryOperators { private CharBinaryOperators() { // Singleton for utility class. } public static final CharBinaryOperator MIN = (left, right) -> left <= right ? left : right; public static final CharBinaryOperator MAX = (left, right) -> left >= right ? left : right; } } /** * Utility class for {@code BytePredicate/Function/Consumer}. * * */ public static final class FB { /** The Constant POSITIVE. */ private static final BytePredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final BytePredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final ByteBiPredicate EQUAL = (t, u) -> t == u; /** The Constant NOT_EQUAL. */ private static final ByteBiPredicate NOT_EQUAL = (t, u) -> t != u; /** The Constant GREATER_THAN. */ private static final ByteBiPredicate GREATER_THAN = (t, u) -> t > u; /** The Constant GREATER_EQUAL. */ private static final ByteBiPredicate GREATER_EQUAL = (t, u) -> t >= u; /** The Constant LESS_THAN. */ private static final ByteBiPredicate LESS_THAN = (t, u) -> t < u; /** The Constant LESS_EQUAL. */ private static final ByteBiPredicate LESS_EQUAL = (t, u) -> t <= u; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FB() { } public static BytePredicate positive() { return POSITIVE; } public static BytePredicate notNegative() { return NOT_NEGATIVE; } public static ByteBiPredicate equal() { return EQUAL; } public static ByteBiPredicate notEqual() { return NOT_EQUAL; } public static ByteBiPredicate greaterThan() { return GREATER_THAN; } public static ByteBiPredicate greaterEqual() { return GREATER_EQUAL; } public static ByteBiPredicate lessThan() { return LESS_THAN; } public static ByteBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToByteFunction unbox() { return ToByteFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static BytePredicate p(final BytePredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static ByteFunction f(final ByteFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static ByteConsumer c(final ByteConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code ByteBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static ByteBiFunction alternated() { return new ByteBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final byte t, final byte u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class ByteBinaryOperators { private ByteBinaryOperators() { // Singleton for utility class. } public static final ByteBinaryOperator MIN = (left, right) -> left <= right ? left : right; public static final ByteBinaryOperator MAX = (left, right) -> left >= right ? left : right; } } /** * Utility class for {@code ShortPredicate/Function/Consumer}. * * */ public static final class FS { /** The Constant POSITIVE. */ private static final ShortPredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final ShortPredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final ShortBiPredicate EQUAL = (t, u) -> t == u; /** The Constant NOT_EQUAL. */ private static final ShortBiPredicate NOT_EQUAL = (t, u) -> t != u; /** The Constant GREATER_THAN. */ private static final ShortBiPredicate GREATER_THAN = (t, u) -> t > u; /** The Constant GREATER_EQUAL. */ private static final ShortBiPredicate GREATER_EQUAL = (t, u) -> t >= u; /** The Constant LESS_THAN. */ private static final ShortBiPredicate LESS_THAN = (t, u) -> t < u; /** The Constant LESS_EQUAL. */ private static final ShortBiPredicate LESS_EQUAL = (t, u) -> t <= u; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FS() { } public static ShortPredicate positive() { return POSITIVE; } public static ShortPredicate notNegative() { return NOT_NEGATIVE; } public static ShortBiPredicate equal() { return EQUAL; } public static ShortBiPredicate notEqual() { return NOT_EQUAL; } public static ShortBiPredicate greaterThan() { return GREATER_THAN; } public static ShortBiPredicate greaterEqual() { return GREATER_EQUAL; } public static ShortBiPredicate lessThan() { return LESS_THAN; } public static ShortBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToShortFunction unbox() { return ToShortFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static ShortPredicate p(final ShortPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static ShortFunction f(final ShortFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static ShortConsumer c(final ShortConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code ShortBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static ShortBiFunction alternated() { return new ShortBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final short t, final short u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class ShortBinaryOperators { private ShortBinaryOperators() { // Singleton for utility class. } public static final ShortBinaryOperator MIN = (left, right) -> left <= right ? left : right; public static final ShortBinaryOperator MAX = (left, right) -> left >= right ? left : right; } } /** * Utility class for {@code IntPredicate/Function/Consumer}. * * */ public static final class FI { /** The Constant POSITIVE. */ private static final IntPredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final IntPredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final IntBiPredicate EQUAL = (t, u) -> t == u; /** The Constant NOT_EQUAL. */ private static final IntBiPredicate NOT_EQUAL = (t, u) -> t != u; /** The Constant GREATER_THAN. */ private static final IntBiPredicate GREATER_THAN = (t, u) -> t > u; /** The Constant GREATER_EQUAL. */ private static final IntBiPredicate GREATER_EQUAL = (t, u) -> t >= u; /** The Constant LESS_THAN. */ private static final IntBiPredicate LESS_THAN = (t, u) -> t < u; /** The Constant LESS_EQUAL. */ private static final IntBiPredicate LESS_EQUAL = (t, u) -> t <= u; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FI() { } public static IntPredicate positive() { return POSITIVE; } public static IntPredicate notNegative() { return NOT_NEGATIVE; } public static IntBiPredicate equal() { return EQUAL; } public static IntBiPredicate notEqual() { return NOT_EQUAL; } public static IntBiPredicate greaterThan() { return GREATER_THAN; } public static IntBiPredicate greaterEqual() { return GREATER_EQUAL; } public static IntBiPredicate lessThan() { return LESS_THAN; } public static IntBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToIntFunction unbox() { return ToIntFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static IntPredicate p(final IntPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static IntFunction f(final IntFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static IntConsumer c(final IntConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code IntBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static IntBiFunction alternated() { return new IntBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final int t, final int u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class IntBinaryOperators { private IntBinaryOperators() { // Singleton for utility class. } public static final IntBinaryOperator MIN = Math::min; public static final IntBinaryOperator MAX = Math::max; } } /** * Utility class for {@code LongPredicate/Function/Consumer}. * * */ public static final class FL { /** The Constant POSITIVE. */ private static final LongPredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final LongPredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final LongBiPredicate EQUAL = (t, u) -> t == u; /** The Constant NOT_EQUAL. */ private static final LongBiPredicate NOT_EQUAL = (t, u) -> t != u; /** The Constant GREATER_THAN. */ private static final LongBiPredicate GREATER_THAN = (t, u) -> t > u; /** The Constant GREATER_EQUAL. */ private static final LongBiPredicate GREATER_EQUAL = (t, u) -> t >= u; /** The Constant LESS_THAN. */ private static final LongBiPredicate LESS_THAN = (t, u) -> t < u; /** The Constant LESS_EQUAL. */ private static final LongBiPredicate LESS_EQUAL = (t, u) -> t <= u; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FL() { } public static LongPredicate positive() { return POSITIVE; } public static LongPredicate notNegative() { return NOT_NEGATIVE; } public static LongBiPredicate equal() { return EQUAL; } public static LongBiPredicate notEqual() { return NOT_EQUAL; } public static LongBiPredicate greaterThan() { return GREATER_THAN; } public static LongBiPredicate greaterEqual() { return GREATER_EQUAL; } public static LongBiPredicate lessThan() { return LESS_THAN; } public static LongBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToLongFunction unbox() { return ToLongFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static LongPredicate p(final LongPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static LongFunction f(final LongFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static LongConsumer c(final LongConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code LongBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static LongBiFunction alternated() { return new LongBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final long t, final long u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class LongBinaryOperators { private LongBinaryOperators() { // Singleton for utility class. } public static final LongBinaryOperator MIN = Math::min; public static final LongBinaryOperator MAX = Math::max; } } /** * Utility class for {@code FloatPredicate/Function/Consumer}. * * */ public static final class FF { /** The Constant POSITIVE. */ private static final FloatPredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final FloatPredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final FloatBiPredicate EQUAL = N::equals; /** The Constant NOT_EQUAL. */ private static final FloatBiPredicate NOT_EQUAL = (t, u) -> N.compare(t, u) != 0; /** The Constant GREATER_THAN. */ private static final FloatBiPredicate GREATER_THAN = (t, u) -> N.compare(t, u) > 0; /** The Constant GREATER_EQUAL. */ private static final FloatBiPredicate GREATER_EQUAL = (t, u) -> N.compare(t, u) >= 0; /** The Constant LESS_THAN. */ private static final FloatBiPredicate LESS_THAN = (t, u) -> N.compare(t, u) < 0; /** The Constant LESS_EQUAL. */ private static final FloatBiPredicate LESS_EQUAL = (t, u) -> N.compare(t, u) <= 0; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FF() { } public static FloatPredicate positive() { return POSITIVE; } public static FloatPredicate notNegative() { return NOT_NEGATIVE; } public static FloatBiPredicate equal() { return EQUAL; } public static FloatBiPredicate notEqual() { return NOT_EQUAL; } public static FloatBiPredicate greaterThan() { return GREATER_THAN; } public static FloatBiPredicate greaterEqual() { return GREATER_EQUAL; } public static FloatBiPredicate lessThan() { return LESS_THAN; } public static FloatBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToFloatFunction unbox() { return ToFloatFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static FloatPredicate p(final FloatPredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static FloatFunction f(final FloatFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static FloatConsumer c(final FloatConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code FloatBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static FloatBiFunction alternated() { return new FloatBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final float t, final float u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class FloatBinaryOperators { private FloatBinaryOperators() { // Singleton for utility class. } public static final FloatBinaryOperator MIN = Math::min; public static final FloatBinaryOperator MAX = Math::max; } } /** * Utility class for {@code DoublePredicate/Function/Consumer}. * * */ public static final class FD { /** The Constant POSITIVE. */ private static final DoublePredicate POSITIVE = t -> t > 0; /** The Constant NOT_NEGATIVE. */ private static final DoublePredicate NOT_NEGATIVE = t -> t >= 0; /** The Constant EQUAL. */ private static final DoubleBiPredicate EQUAL = N::equals; /** The Constant NOT_EQUAL. */ private static final DoubleBiPredicate NOT_EQUAL = (t, u) -> N.compare(t, u) != 0; /** The Constant GREATER_THAN. */ private static final DoubleBiPredicate GREATER_THAN = (t, u) -> N.compare(t, u) > 0; /** The Constant GREATER_EQUAL. */ private static final DoubleBiPredicate GREATER_EQUAL = (t, u) -> N.compare(t, u) >= 0; /** The Constant LESS_THAN. */ private static final DoubleBiPredicate LESS_THAN = (t, u) -> N.compare(t, u) < 0; /** The Constant LESS_EQUAL. */ private static final DoubleBiPredicate LESS_EQUAL = (t, u) -> N.compare(t, u) <= 0; /** The Constant LEN. */ private static final Function LEN = t -> t == null ? 0 : t.length; private FD() { } public static DoublePredicate positive() { return POSITIVE; } public static DoublePredicate notNegative() { return NOT_NEGATIVE; } public static DoubleBiPredicate equal() { return EQUAL; } public static DoubleBiPredicate notEqual() { return NOT_EQUAL; } public static DoubleBiPredicate greaterThan() { return GREATER_THAN; } public static DoubleBiPredicate greaterEqual() { return GREATER_EQUAL; } public static DoubleBiPredicate lessThan() { return LESS_THAN; } public static DoubleBiPredicate lessEqual() { return LESS_EQUAL; } @SuppressWarnings("SameReturnValue") public static ToDoubleFunction unbox() { return ToDoubleFunction.UNBOX; } /** * * @param p * @return * @throws IllegalArgumentException */ public static DoublePredicate p(final DoublePredicate p) throws IllegalArgumentException { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return * @throws IllegalArgumentException */ public static DoubleFunction f(final DoubleFunction f) throws IllegalArgumentException { N.checkArgNotNull(f); return f; } /** * * @param c * @return * @throws IllegalArgumentException */ public static DoubleConsumer c(final DoubleConsumer c) throws IllegalArgumentException { N.checkArgNotNull(c); return c; } public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = N::sum; public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = N::average; public static Function average() { return AVERAGE; } /** * Returns a stateful {@code DoubleBiFunction}. Don't save or cache for reuse or use it in parallel stream. * * @return * @deprecated */ @Deprecated @Beta @SequentialOnly @Stateful public static DoubleBiFunction alternated() { return new DoubleBiFunction<>() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(final double t, final double u) { return flag.getAndNegate() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } public static final class DoubleBinaryOperators { private DoubleBinaryOperators() { // Singleton for utility class. } public static final DoubleBinaryOperator MIN = Math::min; public static final DoubleBinaryOperator MAX = Math::max; } } /** * Utility class for exceptional {@code Predicate/Function/Consumer}. * * */ public static final class Fnn { private Fnn() { // Singleton for utility class } /** * Returns a {@code Supplier} which returns a single instance created by calling the specified {@code supplier.get()}. * * @param * @param * @param supplier * @return */ public static Throwables.Supplier memoize(final Throwables.Supplier supplier) { return Throwables.LazyInitializer.of(supplier); } // Copied from Google Guava under Apache License v2. /** * Copied from Google Guava under Apache License v2. *
*
* * Returns a supplier that caches the instance supplied by the delegate and removes the cached * value after the specified time has passed. Subsequent calls to {@code get()} return the cached * value if the expiration time has not passed. After the expiration time, a new value is * retrieved, cached, and returned. See:
memoization * *

The returned supplier is thread-safe. The supplier's serialized form does not contain the * cached value, which will be recalculated when {@code get()} is called on the reserialized * instance. The actual memoization does not happen when the underlying delegate throws an * exception. * *

When the underlying delegate throws an exception then this memorizing supplier will keep * delegating calls until it returns valid data. * * @param * @param * @param supplier * @param duration the length of time after a value is created that it should stop being returned * by subsequent {@code get()} calls * @param unit the unit that {@code duration} is expressed in * @return * @throws IllegalArgumentException if {@code duration} is not positive */ public static Throwables.Supplier memoizeWithExpiration(final Throwables.Supplier supplier, final long duration, final TimeUnit unit) throws IllegalArgumentException { N.checkArgNotNull(supplier, cs.Supplier); N.checkArgument(duration > 0, "duration (%s %s) must be > 0", duration, unit); return new Throwables.Supplier<>() { private final Throwables.Supplier delegate = supplier; private final long durationNanos = unit.toNanos(duration); private volatile T value; // The special value 0 means "not yet initialized". private volatile long expirationNanos = 0; @Override public T get() throws E { // Another variant of Double-Checked Locking. // // We use two volatile reads. We could reduce this to one by // putting our fields into a holder class, but (at least on x86) // the extra memory consumption and indirection are more // expensive than the extra volatile reads. long nanos = expirationNanos; final long now = System.nanoTime(); if (nanos == 0 || now - nanos >= 0) { synchronized (this) { if (nanos == expirationNanos) { // recheck for lost race final T t = delegate.get(); value = t; nanos = now + durationNanos; // In the very unlikely event that nanos is 0, set it to 1; // no one will notice 1 ns of tardiness. expirationNanos = (nanos == 0) ? 1 : nanos; return t; } } } // This is safe because we checked `expirationNanos`. return value; } }; } /** * * @param * @param * @param * @param func * @return */ public static Throwables.Function memoize(final Throwables.Function func) { return new Throwables.Function<>() { private final R none = (R) NONE; private final Map resultMap = new ConcurrentHashMap<>(); private volatile R resultForNull = none; //NOSONAR @SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE") @Override public R apply(final T t) throws E { R result = null; if (t == null) { result = resultForNull; if (result == none) { synchronized (this) { if (resultForNull == none) { resultForNull = func.apply(null); } result = resultForNull; } } } else { result = resultMap.get(t); if (result == null) { result = func.apply(t); resultMap.put(t, result == null ? none : result); } } return result == none ? null : result; } }; } /** * * @param * @param * @return */ public static Throwables.Function identity() { return Fn.IDENTITY; } /** * * @param * @param * @return */ public static Throwables.Predicate alwaysTrue() { return Fn.ALWAYS_TRUE; } /** * * @param * @param * @return */ public static Throwables.Predicate alwaysFalse() { return Fn.ALWAYS_FALSE; } /** * * @param * @param * @return */ public static Throwables.Function toStr() { return TO_STRING; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, K, E> key() { return (Throwables.Function) Fn.KEY; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, V, E> value() { return (Throwables.Function) Fn.VALUE; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, Entry, E> inverse() { return (Throwables.Function) Fn.INVERSE; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> entry() { return (Throwables.BiFunction) Fn.ENTRY; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> pair() { return (Throwables.BiFunction) Fn.PAIR; } /** * * @param * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.TriFunction, E> triple() { return (Throwables.TriFunction) Fn.TRIPLE; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, E> tuple1() { return (Throwables.Function) Fn.TUPLE_1; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> tuple2() { return (Throwables.BiFunction) Fn.TUPLE_2; } /** * * @param * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.TriFunction, E> tuple3() { return (Throwables.TriFunction) Fn.TUPLE_3; } /** * * @param * @return */ public static Throwables.Runnable emptyAction() { return (Throwables.Runnable) Fn.EMPTY_ACTION; } /** * * @param * @param * @return */ public static Throwables.Consumer doNothing() { return Fn.DO_NOTHING; } /** * * @param * @param errorMessage * @return */ public static Throwables.Consumer throwRuntimeException(final String errorMessage) { return t -> { throw new RuntimeException(errorMessage); }; } /** * * @param * @param errorMessage * @return */ public static Throwables.Consumer throwIOException(final String errorMessage) { return t -> { throw new IOException(errorMessage); }; } /** * * @param * @param errorMessage * @return */ public static Throwables.Consumer throwException(final String errorMessage) { return t -> { throw new Exception(errorMessage); }; } /** * * @param * @param * @param exceptionSupplier * @return */ public static Throwables.Consumer throwException(final java.util.function.Supplier exceptionSupplier) { return t -> { throw exceptionSupplier.get(); }; } /** * * @param * @param * @param millis * @return */ public static Throwables.Consumer sleep(final long millis) { return t -> N.sleep(millis); } /** * * @param * @param * @param millis * @return */ public static Throwables.Consumer sleepUninterruptibly(final long millis) { return t -> N.sleepUninterruptibly(millis); } /** * Returns a stateful {@code Consumer}. Don't save or cache for reuse * * @param * @param * @param permitsPerSecond * @return * @see RateLimiter#acquire() * @see RateLimiter#create(double) */ @Stateful public static Throwables.Consumer rateLimiter(final double permitsPerSecond) { return rateLimiter(RateLimiter.create(permitsPerSecond)); } /** * Returns a stateful {@code Consumer}. Don't save or cache for reuse * * @param * @param * @param rateLimiter * @return * @see RateLimiter#acquire() */ @Stateful public static Throwables.Consumer rateLimiter(final RateLimiter rateLimiter) { return t -> rateLimiter.acquire(); } private static final Throwables.Consumer CLOSE = closeable -> { if (closeable != null) { closeable.close(); } }; // public static Throwables.Runnable close(final AutoCloseable closeable) { // return new Throwables.Runnable() { // private volatile boolean isClosed = false; // // @Override // public void run() throws E { // if (isClosed) { // return; // } // // isClosed = true; // IOUtil.close(closeable); // } // }; // } // // public static Throwables.Runnable closeQuietly(final AutoCloseable closeable) { // return new Throwables.Runnable() { // private volatile boolean isClosed = false; // // @Override // public void run() { // if (isClosed) { // return; // } // // isClosed = true; // IOUtil.closeQuietly(closeable); // } // }; // } /** * * @param * @return */ public static Throwables.Consumer close() { return (Throwables.Consumer) CLOSE; } /** * * @param * @param * @return */ public static Throwables.Consumer closeQuietly() { return (Throwables.Consumer) Fn.CLOSE_QUIETLY; } /** * * @param * @param * @return */ public static Throwables.Consumer println() { return Fn.PRINTLN; } /** * * @param * @param * @param * @param separator * @return */ public static Throwables.BiConsumer println(final String separator) { return cc(Fn.println(separator)); } /** * * @param * @param * @return */ @Beta public static Throwables.Predicate isNull() { return Fn.IS_NULL; } /** * * @param * @param * @return */ public static Throwables.Predicate isEmpty() { return (Throwables.Predicate) Fn.IS_EMPTY; } /** * * @param * @param * @return */ public static Throwables.Predicate isBlank() { return (Throwables.Predicate) Fn.IS_BLANK; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate isEmptyA() { return (Throwables.Predicate) Fn.IS_EMPTY_A; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate isEmptyC() { return (Throwables.Predicate) Fn.IS_EMPTY_C; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate isEmptyM() { return (Throwables.Predicate) Fn.IS_EMPTY_M; } /** * * @param * @param * @return */ @Beta public static Throwables.Predicate notNull() { return Fn.NOT_NULL; } /** * * @param * @param * @return */ public static Throwables.Predicate notEmpty() { return (Throwables.Predicate) Fn.IS_NOT_EMPTY; } /** * * @param * @param * @return */ public static Throwables.Predicate notBlank() { return (Throwables.Predicate) Fn.IS_NOT_BLANK; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate notEmptyA() { return (Throwables.Predicate) Fn.NOT_EMPTY_A; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate notEmptyC() { return (Throwables.Predicate) Fn.NOT_EMPTY_C; } /** * * @param * @param * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate notEmptyM() { return (Throwables.Predicate) Fn.NOT_EMPTY_M; } /** * * @param * @param * @return */ public static Throwables.BinaryOperator throwingMerger() { return BinaryOperators.THROWING_MERGER; } /** * * @param * @param * @return */ public static Throwables.BinaryOperator ignoringMerger() { return BinaryOperators.IGNORING_MERGER; } /** * * @param * @param * @return */ public static Throwables.BinaryOperator replacingMerger() { return BinaryOperators.REPLACING_MERGER; } /** * * @param * @param * @param * @param predicate * @return * @throws IllegalArgumentException */ public static Throwables.Predicate, E> testByKey(final Throwables.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return entry -> predicate.test(entry.getKey()); } /** * * @param * @param * @param * @param predicate * @return * @throws IllegalArgumentException */ public static Throwables.Predicate, E> testByValue(final Throwables.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return entry -> predicate.test(entry.getValue()); } /** * * @param * @param * @param * @param consumer * @return * @throws IllegalArgumentException */ public static Throwables.Consumer, E> acceptByKey(final Throwables.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return entry -> consumer.accept(entry.getKey()); } /** * * @param * @param * @param * @param consumer * @return * @throws IllegalArgumentException */ public static Throwables.Consumer, E> acceptByValue(final Throwables.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return entry -> consumer.accept(entry.getValue()); } /** * * @param * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static Throwables.Function, R, E> applyByKey( final Throwables.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> func.apply(entry.getKey()); } /** * * @param * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static Throwables.Function, R, E> applyByValue( final Throwables.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return entry -> func.apply(entry.getValue()); } /** The Constant RETURN_FIRST. */ private static final Throwables.BinaryOperator RETURN_FIRST = (t, u) -> t; /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BinaryOperator selectFirst() { return (Throwables.BinaryOperator) RETURN_FIRST; } /** The Constant RETURN_SECOND. */ private static final Throwables.BinaryOperator RETURN_SECOND = (t, u) -> u; /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BinaryOperator selectSecond() { return (Throwables.BinaryOperator) RETURN_SECOND; } /** The Constant MIN. */ @SuppressWarnings({ "rawtypes" }) private static final Throwables.BinaryOperator MIN = (t, u) -> N.compare(t, u) <= 0 ? t : u; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , E extends Throwable> Throwables.BinaryOperator min() { return (Throwables.BinaryOperator) MIN; } /** * * @param * @param * @param comparator * @return * @throws IllegalArgumentException */ public static Throwables.BinaryOperator min(final Comparator comparator) throws IllegalArgumentException { N.checkArgNotNull(comparator); return (t, u) -> comparator.compare(t, u) <= 0 ? t : u; } /** * * @param * @param * @param keyExtractor * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static Throwables.BinaryOperator minBy( final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); return (t, u) -> N.compare(keyExtractor.apply(t), keyExtractor.apply(u)) <= 0 ? t : u; } /** The Constant MIN_BY_KEY. */ @SuppressWarnings("rawtypes") private static final Throwables.BinaryOperator, Throwable> MIN_BY_KEY = (t, u) -> N.compare(t.getKey(), u.getKey()) <= 0 ? t : u; /** * * @param * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , V, E extends Throwable> Throwables.BinaryOperator, E> minByKey() { return (Throwables.BinaryOperator) MIN_BY_KEY; } /** The Constant MIN_BY_VALUE. */ @SuppressWarnings("rawtypes") private static final Throwables.BinaryOperator, Throwable> MIN_BY_VALUE = (t, u) -> N.compare(t.getValue(), u.getValue()) <= 0 ? t : u; /** * * @param * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , E extends Throwable> Throwables.BinaryOperator, E> minByValue() { return (Throwables.BinaryOperator) MIN_BY_VALUE; } /** The Constant MAX. */ @SuppressWarnings("rawtypes") private static final Throwables.BinaryOperator MAX = (t, u) -> N.compare(t, u) >= 0 ? t : u; /** * * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , E extends Throwable> Throwables.BinaryOperator max() { return (Throwables.BinaryOperator) MAX; } /** * * @param * @param * @param comparator * @return * @throws IllegalArgumentException */ public static Throwables.BinaryOperator max(final Comparator comparator) throws IllegalArgumentException { N.checkArgNotNull(comparator); return (t, u) -> comparator.compare(t, u) >= 0 ? t : u; } /** * * @param * @param * @param keyExtractor * @return * @throws IllegalArgumentException */ @SuppressWarnings("rawtypes") public static Throwables.BinaryOperator maxBy( final java.util.function.Function keyExtractor) throws IllegalArgumentException { N.checkArgNotNull(keyExtractor); return (t, u) -> N.compare(keyExtractor.apply(t), keyExtractor.apply(u)) >= 0 ? t : u; } /** The Constant MAX_BY_KEY. */ @SuppressWarnings("rawtypes") private static final Throwables.BinaryOperator, Throwable> MAX_BY_KEY = (t, u) -> N.compare(t.getKey(), u.getKey()) >= 0 ? t : u; /** * * @param * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , V, E extends Throwable> Throwables.BinaryOperator, E> maxByKey() { return (Throwables.BinaryOperator) MAX_BY_KEY; } /** The Constant MAX_BY_VALUE. */ @SuppressWarnings("rawtypes") private static final Throwables.BinaryOperator, Throwable> MAX_BY_VALUE = (t, u) -> N.compare(t.getValue(), u.getValue()) >= 0 ? t : u; /** * * @param * @param * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static , E extends Throwable> Throwables.BinaryOperator, E> maxByValue() { return (Throwables.BinaryOperator) MAX_BY_VALUE; } /** * * @param * @param * @param predicate * @return * @throws IllegalArgumentException */ public static Throwables.Predicate not(final Throwables.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return t -> !predicate.test(t); } /** * * @param * @param * @param * @param biPredicate * @return * @throws IllegalArgumentException */ public static Throwables.BiPredicate not(final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return (t, u) -> !biPredicate.test(t, u); } /** * * @param * @param * @param * @param * @param triPredicate * @return * @throws IllegalArgumentException */ public static Throwables.TriPredicate not(final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (a, b, c) -> !triPredicate.test(a, b, c); } /** * * @param * @param * @param count * @return * @throws IllegalArgumentException */ @Beta @Stateful public static Throwables.Predicate atMost(final int count) throws IllegalArgumentException { N.checkArgNotNegative(count, cs.count); return new Throwables.Predicate<>() { private final AtomicInteger counter = new AtomicInteger(count); @Override public boolean test(final T t) { return counter.getAndDecrement() > 0; } }; } /** * * @param * @param * @param supplier * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Supplier from(final java.util.function.Supplier supplier) { return supplier instanceof Throwables.Supplier ? ((Throwables.Supplier) supplier) : supplier::get; } /** * * @param * @param * @param func * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.IntFunction from(final java.util.function.IntFunction func) { return func instanceof Throwables.IntFunction ? ((Throwables.IntFunction) func) : func::apply; } /** * * @param * @param * @param predicate * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Predicate from(final java.util.function.Predicate predicate) { return predicate instanceof Throwables.Predicate ? ((Throwables.Predicate) predicate) : predicate::test; } /** * * @param * @param * @param * @param predicate * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.BiPredicate from(final java.util.function.BiPredicate predicate) { return predicate instanceof Throwables.BiPredicate ? ((Throwables.BiPredicate) predicate) : predicate::test; } /** * * @param * @param * @param consumer * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Consumer from(final java.util.function.Consumer consumer) { return consumer instanceof Throwables.Consumer ? ((Throwables.Consumer) consumer) : consumer::accept; } /** * * @param * @param * @param * @param consumer * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.BiConsumer from(final java.util.function.BiConsumer consumer) { return consumer instanceof Throwables.BiConsumer ? ((Throwables.BiConsumer) consumer) : consumer::accept; } /** * * @param * @param * @param * @param function * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.Function from(final java.util.function.Function function) { return function instanceof Throwables.Function ? ((Throwables.Function) function) : function::apply; } /** * * @param * @param * @param * @param * @param function * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.BiFunction from(final java.util.function.BiFunction function) { return function instanceof Throwables.BiFunction ? ((Throwables.BiFunction) function) : function::apply; } /** * * @param * @param * @param op * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.UnaryOperator from(final java.util.function.UnaryOperator op) { return op instanceof Throwables.UnaryOperator ? ((Throwables.UnaryOperator) op) : op::apply; } /** * * @param * @param * @param op * @return */ @Beta @SuppressWarnings("rawtypes") public static Throwables.BinaryOperator from(final java.util.function.BinaryOperator op) { return op instanceof Throwables.BinaryOperator ? ((Throwables.BinaryOperator) op) : op::apply; } /** * * @param * @param * @param predicate * @return */ @Beta public static Throwables.Predicate p(final Throwables.Predicate predicate) { return predicate; } /** * * @param * @param * @param * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate p(final A a, final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return t -> biPredicate.test(a, t); } /** * * @param * @param * @param * @param * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate p(final A a, final B b, final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return t -> triPredicate.test(a, b, t); } /** * * @param * @param * @param * @param biPredicate * @return */ @Beta public static Throwables.BiPredicate p(final Throwables.BiPredicate biPredicate) { return biPredicate; } /** * * @param * @param * @param * @param * @param a * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiPredicate p(final A a, final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (t, u) -> triPredicate.test(a, t, u); } /** * * @param * @param * @param * @param * @param triPredicate * @return */ @Beta public static Throwables.TriPredicate p(final Throwables.TriPredicate triPredicate) { return triPredicate; } /** * * @param * @param * @param predicate * @return */ @Beta public static Throwables.Consumer c(final Throwables.Consumer predicate) { return predicate; } /** * * @param * @param * @param * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer c(final A a, final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return t -> biConsumer.accept(a, t); } /** * * @param * @param * @param * @param * @param a * @param b * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer c(final A a, final B b, final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return t -> triConsumer.accept(a, b, t); } /** * * @param * @param * @param * @param biConsumer * @return */ @Beta public static Throwables.BiConsumer c(final Throwables.BiConsumer biConsumer) { return biConsumer; } /** * * @param * @param * @param * @param * @param a * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiConsumer c(final A a, final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (t, u) -> triConsumer.accept(a, t, u); } /** * * @param * @param * @param * @param * @param triConsumer * @return */ @Beta public static Throwables.TriConsumer c(final Throwables.TriConsumer triConsumer) { return triConsumer; } /** * * @param * @param * @param * @param predicate * @return */ @Beta public static Throwables.Function f(final Throwables.Function predicate) { return predicate; } /** * * @param * @param * @param * @param * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function f(final A a, final Throwables.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return t -> biFunction.apply(a, t); } /** * * @param * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function f(final A a, final B b, final Throwables.TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return t -> triFunction.apply(a, b, t); } /** * * @param * @param * @param * @param * @param biFunction * @return */ @Beta public static Throwables.BiFunction f(final Throwables.BiFunction biFunction) { return biFunction; } /** * * @param * @param * @param * @param * @param * @param a * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiFunction f(final A a, final Throwables.TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (t, u) -> triFunction.apply(a, t, u); } /** * * @param * @param * @param * @param * @param * @param triFunction * @return */ @Beta public static Throwables.TriFunction f(final Throwables.TriFunction triFunction) { return triFunction; } /** * * @param * @param * @param predicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate pp(final Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(predicate); return (Throwables.Predicate) predicate; } /** * * @param * @param * @param * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate pp(final A a, final java.util.function.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return t -> biPredicate.test(a, t); } /** * * @param * @param * @param * @param * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate pp(final A a, final B b, final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return t -> triPredicate.test(a, b, t); } /** * * @param * @param * @param * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiPredicate pp(final BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(biPredicate); return (Throwables.BiPredicate) biPredicate; } /** * * @param * @param * @param * @param * @param a * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiPredicate pp(final A a, final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (t, u) -> triPredicate.test(a, t, u); } /** * * @param * @param * @param * @param * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.TriPredicate pp(final TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(triPredicate); return (Throwables.TriPredicate) triPredicate; } /** * * @param * @param * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer cc(final Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return (Throwables.Consumer) consumer; } /** * * @param * @param * @param * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer cc(final A a, final java.util.function.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return t -> biConsumer.accept(a, t); } /** * * @param * @param * @param * @param * @param a * @param b * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer cc(final A a, final B b, final TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return t -> triConsumer.accept(a, b, t); } /** * * @param * @param * @param * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiConsumer cc(final BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return (Throwables.BiConsumer) biConsumer; } /** * * @param * @param * @param * @param * @param a * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiConsumer cc(final A a, final TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (t, u) -> triConsumer.accept(a, t, u); } /** * * @param * @param * @param * @param * @param triConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.TriConsumer cc(final TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (Throwables.TriConsumer) triConsumer; } /** * * @param * @param * @param * @param function * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function ff(final Function function) throws IllegalArgumentException { N.checkArgNotNull(function); return (Throwables.Function) function; } /** * * @param * @param * @param * @param * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function ff(final A a, final java.util.function.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return t -> biFunction.apply(a, t); } /** * * @param * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function ff(final A a, final B b, final TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return t -> triFunction.apply(a, b, t); } /** * * @param * @param * @param * @param * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiFunction ff(final BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(biFunction); return (Throwables.BiFunction) biFunction; } /** * * @param * @param * @param * @param * @param * @param a * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiFunction ff(final A a, final TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (t, u) -> triFunction.apply(a, t, u); } /** * * @param * @param * @param * @param * @param * @param triFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.TriFunction ff(final TriFunction triFunction) throws IllegalArgumentException { N.checkArgNotNull(triFunction); return (Throwables.TriFunction) triFunction; } /** * Synchronized {@code Predicate}. * * @param * @param * @param mutex to synchronized on * @param predicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate sp(final Object mutex, final Throwables.Predicate predicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(predicate, cs.Predicate); return t -> { synchronized (mutex) { return predicate.test(t); } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate sp(final Object mutex, final A a, final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biPredicate, cs.BiPredicate); return t -> { synchronized (mutex) { return biPredicate.test(a, t); } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param * @param * @param mutex to synchronized on * @param a * @param b * @param triPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Predicate sp(final Object mutex, final A a, final B b, final Throwables.TriPredicate triPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(triPredicate, cs.TriPredicate); return t -> { synchronized (mutex) { return triPredicate.test(a, b, t); } }; } /** * Synchronized {@code BiPredicate}. * * @param * @param * @param * @param mutex to synchronized on * @param biPredicate * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiPredicate sp(final Object mutex, final Throwables.BiPredicate biPredicate) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biPredicate, cs.BiPredicate); return (t, u) -> { synchronized (mutex) { return biPredicate.test(t, u); } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param mutex to synchronized on * @param consumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer sc(final Object mutex, final Throwables.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(consumer, cs.Consumer); return t -> { synchronized (mutex) { consumer.accept(t); } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Consumer sc(final Object mutex, final A a, final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biConsumer, cs.BiConsumer); return t -> { synchronized (mutex) { biConsumer.accept(a, t); } }; } /** * Synchronized {@code BiConsumer}. * * @param * @param * @param * @param mutex to synchronized on * @param biConsumer * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiConsumer sc(final Object mutex, final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biConsumer, cs.BiConsumer); return (t, u) -> { synchronized (mutex) { biConsumer.accept(t, u); } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param mutex to synchronized on * @param function * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function sf(final Object mutex, final Throwables.Function function) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(function, cs.function); return t -> { synchronized (mutex) { return function.apply(t); } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param * @param mutex to synchronized on * @param a * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.Function sf(final Object mutex, final A a, final Throwables.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biFunction, cs.BiFunction); return t -> { synchronized (mutex) { return biFunction.apply(a, t); } }; } /** * Synchronized {@code BiFunction}. * * @param * @param * @param * @param * @param mutex to synchronized on * @param biFunction * @return * @throws IllegalArgumentException */ @Beta public static Throwables.BiFunction sf(final Object mutex, final Throwables.BiFunction biFunction) throws IllegalArgumentException { N.checkArgNotNull(mutex, cs.mutex); N.checkArgNotNull(biFunction, cs.BiFunction); return (t, u) -> { synchronized (mutex) { return biFunction.apply(t, u); } }; } /** * * @param * @param * @param consumer * @return * @throws IllegalArgumentException */ public static Throwables.Function c2f(final Throwables.Consumer consumer) throws IllegalArgumentException { N.checkArgNotNull(consumer); return t -> { consumer.accept(t); return null; }; } /** * * @param * @param * @param * @param consumer * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Throwables.Function c2f(final Throwables.Consumer consumer, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(consumer); return t -> { consumer.accept(t); return valueToReturn; }; } /** * * @param * @param * @param * @param biConsumer * @return * @throws IllegalArgumentException */ public static Throwables.BiFunction c2f(final Throwables.BiConsumer biConsumer) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return (t, u) -> { biConsumer.accept(t, u); return null; }; } /** * * @param * @param * @param * @param * @param biConsumer * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Throwables.BiFunction c2f(final Throwables.BiConsumer biConsumer, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(biConsumer); return (t, u) -> { biConsumer.accept(t, u); return valueToReturn; }; } /** * * @param * @param * @param * @param * @param triConsumer * @return * @throws IllegalArgumentException */ public static Throwables.TriFunction c2f(final Throwables.TriConsumer triConsumer) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (a, b, c) -> { triConsumer.accept(a, b, c); return null; }; } /** * * @param * @param * @param * @param * @param * @param triConsumer * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Throwables.TriFunction c2f(final Throwables.TriConsumer triConsumer, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(triConsumer); return (a, b, c) -> { triConsumer.accept(a, b, c); return valueToReturn; }; } /** * * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static Throwables.Consumer f2c(final Throwables.Function func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static Throwables.BiConsumer f2c(final Throwables.BiFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param * @param * @param * @param * @param * @param func * @return * @throws IllegalArgumentException */ public static Throwables.TriConsumer f2c(final Throwables.TriFunction func) throws IllegalArgumentException { N.checkArgNotNull(func); return func::apply; } /** * * @param * @param runnable * @return * @throws IllegalArgumentException */ public static Throwables.Runnable r(final Throwables.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); return runnable; } /** * * @param * @param * @param callable * @return * @throws IllegalArgumentException */ public static Throwables.Callable c(final Throwables.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return callable; } /** * * @param * @param runnable * @return * @throws IllegalArgumentException */ public static Throwables.Callable r2c(final Throwables.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); return () -> { runnable.run(); return null; }; } /** * * @param * @param * @param runnable * @param valueToReturn * @return * @throws IllegalArgumentException */ public static Throwables.Callable r2c(final Throwables.Runnable runnable, final R valueToReturn) throws IllegalArgumentException { N.checkArgNotNull(runnable); return () -> { runnable.run(); return valueToReturn; }; } /** * * @param * @param * @param callable * @return * @throws IllegalArgumentException */ public static Throwables.Runnable c2r(final Throwables.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); return callable::call; } /** * * @param * @param runnable * @return */ public static Throwables.Runnable rr(final Runnable runnable) { return (Throwables.Runnable) runnable; } /** * * @param * @param * @param callable * @return */ public static Throwables.Callable cc(final Callable callable) { return (Throwables.Callable) callable; } /** * * @param * @param runnable * @return * @throws IllegalArgumentException */ public static Throwables.Runnable jr2r(final java.lang.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); if (runnable instanceof Throwables.Runnable) { return (Throwables.Runnable) runnable; } return runnable::run; } /** * * @param * @param runnable * @return * @throws IllegalArgumentException */ public static java.lang.Runnable r2jr(final Throwables.Runnable runnable) throws IllegalArgumentException { N.checkArgNotNull(runnable); if (runnable instanceof java.lang.Runnable) { return (java.lang.Runnable) runnable; } return () -> { try { runnable.run(); } catch (final Throwable e) { // NOSONAR throw ExceptionUtil.toRuntimeException(e, true); } }; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static Throwables.Callable jc2c(final java.util.concurrent.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); if (callable instanceof Throwables.Callable) { return (Throwables.Callable) callable; } return callable::call; } /** * * @param * @param callable * @return * @throws IllegalArgumentException */ public static java.util.concurrent.Callable c2jc(final Throwables.Callable callable) throws IllegalArgumentException { N.checkArgNotNull(callable); if (callable instanceof java.util.concurrent.Callable) { return (java.util.concurrent.Callable) callable; } return callable::call; } } }