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.

There is a newer version: 2.1.12
Show 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.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
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.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.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
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.exception.DuplicatedResultException;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableArray;
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.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.ByteBiPredicate;
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.CharBiPredicate;
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.DoubleBiPredicate;
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.FloatBiPredicate;
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.IndexedBiConsumer;
import com.landawn.abacus.util.function.IndexedBiFunction;
import com.landawn.abacus.util.function.IndexedBiPredicate;
import com.landawn.abacus.util.function.IndexedConsumer;
import com.landawn.abacus.util.function.IndexedFunction;
import com.landawn.abacus.util.function.IndexedPredicate;
import com.landawn.abacus.util.function.IntBiPredicate;
import com.landawn.abacus.util.function.IntConsumer;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.IntPredicate;
import com.landawn.abacus.util.function.LongBiPredicate;
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.Predicate;
import com.landawn.abacus.util.function.QuadFunction;
import com.landawn.abacus.util.function.ShortBiPredicate;
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.ToDoubleFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
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: Don't save and reuse any Function/Predicat/Consumer/... created by calling the methods in this class. * The method should be called every time. *
* *
 * 
 *
 * Map map = N.asMap("a", 1, "b", 2, "c", 3);
 * // Instead of
 * Stream.of(map).filter(e -> e.getKey().equals("a") || e.getKey().equals("b")).toMap(e -> e.getKey(), e -> e.getValue());
 * // Using Fn
 * Stream.of(map).filter(Fn.testByKey(k -> k.equals("a") || k.equals("b"))).collect(Collectors.toMap());
 *
 * 
 * 
* * * * @author haiyang li * */ public final class Fn extends Comparators { /** The Constant NONE. */ private static final Object NONE = new Object(); /** The Constant timer. */ private static final Timer timer = new Timer(); /** The Constant FACTORY_OF_MAP. */ @SuppressWarnings("rawtypes") public static final IntFunction> FACTORY_OF_MAP = (IntFunction) Factory.MAP_FACTORY; /** The Constant FACTORY_OF_LINKED_HASH_MAP. */ @SuppressWarnings("rawtypes") public static final IntFunction> FACTORY_OF_LINKED_HASH_MAP = (IntFunction) Factory.LINKED_HASH_MAP_FACTORY; /** The Constant SUPPLIER_OF_MAP. */ @SuppressWarnings("rawtypes") public static final Supplier> SUPPLIER_OF_MAP = (Supplier) Suppliers.MAP; /** The Constant SUPPLIER_OF_LINKED_HASH_MAP. */ @SuppressWarnings("rawtypes") public static final Supplier> SUPPLIER_OF_LINKED_HASH_MAP = (Supplier) Suppliers.LINKED_HASH_MAP; /** The Constant EMPTY_ACTION. */ private static final com.landawn.abacus.util.function.Runnable EMPTY_ACTION = new com.landawn.abacus.util.function.Runnable() { @Override public void run() { } }; /** The Constant DO_NOTHING. */ @SuppressWarnings("rawtypes") private static final Consumer DO_NOTHING = new Consumer() { @Override public void accept(Object value) { // do nothing. } }; /** The Constant CLOSE. */ private static final Consumer CLOSE = new Consumer() { @Override public void accept(AutoCloseable closeable) { IOUtil.close(closeable); } }; /** The Constant CLOSE_QUIETLY. */ private static final Consumer CLOSE_QUIETLY = new Consumer() { @Override public void accept(AutoCloseable closeable) { IOUtil.closeQuietly(closeable); } }; /** The Constant PRINTLN_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_EQUAL = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), "=", N.toString(value))); } }; /** The Constant PRINTLN_HYPHEN. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_HYPHEN = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), "-", N.toString(value))); } }; /** The Constant PRINTLN_UNDERSCORE. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_UNDERSCORE = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), "_", N.toString(value))); } }; /** The Constant PRINTLN_COLON. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COLON = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), ":", N.toString(value))); } }; /** The Constant PRINTLN_COLON_SPACE. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COLON_SPACE = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), ": ", N.toString(value))); } }; /** The Constant PRINTLN_COMMA. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COMMA = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), ",", N.toString(value))); } }; /** The Constant PRINTLN_COMMA_SPACE. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_COMMA_SPACE = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), ", ", N.toString(value))); } }; /** The Constant PRINTLN_EMPTY. */ @SuppressWarnings("rawtypes") private static final BiConsumer PRINTLN_EMPTY = new BiConsumer() { @Override public void accept(Object key, Object value) { N.println(StringUtil.concat(N.toString(key), N.toString(value))); } }; /** The Constant PRINTLN. */ @SuppressWarnings("rawtypes") private static final Consumer PRINTLN = new Consumer() { @Override public void accept(Object value) { N.println(value); } }; /** The Constant TO_STRING. */ @SuppressWarnings("rawtypes") private static final Function TO_STRING = new Function() { @Override public Object apply(Object t) { return N.toString(t); } }; /** The Constant TO_CAMEL_CASE. */ private static final Function TO_CAMEL_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toCamelCase(t); } }; /** The Constant TO_LOWER_CASE. */ private static final Function TO_LOWER_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toLowerCase(t); } }; /** The Constant TO_LOWER_CASE_WITH_UNDERSCORE. */ private static final Function TO_LOWER_CASE_WITH_UNDERSCORE = new Function() { @Override public String apply(String t) { return StringUtil.toLowerCaseWithUnderscore(t); } }; /** The Constant TO_UPPER_CASE. */ private static final Function TO_UPPER_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toUpperCase(t); } }; /** The Constant TO_UPPER_CASE_WITH_UNDERSCORE. */ private static final Function TO_UPPER_CASE_WITH_UNDERSCORE = new Function() { @Override public String apply(String t) { return StringUtil.toUpperCaseWithUnderscore(t); } }; /** The Constant COMPARE. */ @SuppressWarnings("rawtypes") private static final BiFunction COMPARE = new BiFunction() { @Override public Integer apply(Comparable a, Comparable b) { return N.compare(a, b); } }; /** The Constant IDENTITY. */ @SuppressWarnings("rawtypes") private static final Function IDENTITY = new Function() { @Override public Object apply(Object t) { return t; } }; /** The Constant TRIM. */ private static final Function TRIM = new Function() { @Override public String apply(String t) { return t == null ? null : t.trim(); } }; /** The Constant TRIM_TO_EMPTY. */ private static final Function TRIM_TO_EMPTY = new Function() { @Override public String apply(String t) { return t == null ? "" : t.trim(); } }; /** The Constant TRIM_TO_NULL. */ private static final Function TRIM_TO_NULL = new Function() { @Override public String apply(String t) { if (t == null || (t = t.trim()).length() == 0) { return null; } return t; } }; /** The Constant NULL_TO_EMPTY. */ private static final Function NULL_TO_EMPTY = new Function() { @Override public String apply(String t) { return t == null ? N.EMPTY_STRING : t; } }; /** The Constant NULL_TO_EMPTY_L. */ @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_LIST = new Function() { @Override public List apply(List t) { return t == null ? N.emptyList() : t; } }; /** The Constant NULL_TO_EMPTY_S. */ @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_SET = new Function() { @Override public Set apply(Set t) { return t == null ? N.emptySet() : t; } }; /** The Constant NULL_TO_EMPTY_M. */ @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_MAP = new Function() { @Override public Map apply(Map t) { return t == null ? N.emptyMap() : t; } }; /** The Constant LENGTH. */ private static final Function LENGTH = new Function() { @Override public Integer apply(CharSequence t) { return t == null ? 0 : t.length(); } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(Object[] t) { return t == null ? 0 : t.length; } }; /** The Constant SIZE. */ @SuppressWarnings("rawtypes") private static final Function SIZE = new Function() { @Override public Integer apply(Collection t) { return t == null ? 0 : t.size(); } }; /** The Constant SIZE_M. */ @SuppressWarnings("rawtypes") private static final Function SIZE_MAP = new Function() { @Override public Integer apply(Map t) { return t == null ? 0 : t.size(); } }; /** The Constant KEY. */ private static final Function, Object> KEY = new Function, Object>() { @Override public Object apply(Map.Entry t) { return t.getKey(); } }; /** The Constant VALUE. */ private static final Function, Object> VALUE = new Function, Object>() { @Override public Object apply(Map.Entry t) { return t.getValue(); } }; private static final Function, Object> LEFT = new Function, Object>() { @Override public Object apply(Pair t) { return t.getLeft(); } }; private static final Function, Object> RIGHT = new Function, Object>() { @Override public Object apply(Pair t) { return t.getRight(); } }; /** The Constant INVERSE. */ private static final Function, Map.Entry> INVERSE = new Function, Map.Entry>() { @Override public Map.Entry apply(Map.Entry t) { return new ImmutableEntry<>(t.getValue(), t.getKey()); } }; /** The Constant ENTRY. */ private static final BiFunction> ENTRY = new BiFunction>() { @Override public Map.Entry apply(Object key, Object value) { return new ImmutableEntry<>(key, value); } }; /** The Constant PAIR. */ private static final BiFunction> PAIR = new BiFunction>() { @Override public Pair apply(Object key, Object value) { return Pair.of(key, value); } }; /** The Constant TRIPLE. */ private static final TriFunction> TRIPLE = new TriFunction>() { @Override public Triple apply(Object a, Object b, Object c) { return Triple.of(a, b, c); } }; /** The Constant TUPLE_1. */ private static final Function> TUPLE_1 = new Function>() { @Override public Tuple1 apply(Object t) { return Tuple.of(t); } }; /** The Constant TUPLE_2. */ private static final BiFunction> TUPLE_2 = new BiFunction>() { @Override public Tuple2 apply(Object t, Object u) { return Tuple.of(t, u); } }; /** The Constant TUPLE_3. */ private static final TriFunction> TUPLE_3 = new TriFunction>() { @Override public Tuple3 apply(Object a, Object b, Object c) { return Tuple.of(a, b, c); } }; /** The Constant TUPLE_4. */ private static final QuadFunction> TUPLE_4 = new QuadFunction>() { @Override public Tuple4 apply(Object a, Object b, Object c, Object d) { return Tuple.of(a, b, c, d); } }; /** The Constant ALWAYS_TRUE. */ @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_TRUE = new Predicate() { @Override public boolean test(Object value) { return true; } }; /** The Constant ALWAYS_FALSE. */ @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_FALSE = new Predicate() { @Override public boolean test(Object value) { return false; } }; /** The Constant IS_NULL. */ @SuppressWarnings("rawtypes") private static final Predicate IS_NULL = new Predicate() { @Override public boolean test(Object value) { return value == null; } }; /** The Constant IS_NULL_OR_EMPTY. */ private static final Predicate IS_NULL_OR_EMPTY = new Predicate() { @Override public boolean test(CharSequence value) { return value == null || value.length() == 0; } }; /** The Constant IS_NULL_OR_EMPTY_OR_BLANK. */ private static final Predicate IS_NULL_OR_EMPTY_OR_BLANK = new Predicate() { @Override public boolean test(CharSequence value) { return N.isNullOrEmptyOrBlank(value); } }; /** The Constant NOT_NULL. */ @SuppressWarnings("rawtypes") private static final Predicate NOT_NULL = new Predicate() { @Override public boolean test(Object value) { return value != null; } }; /** The Constant NOT_NULL_OR_EMPTY. */ private static final Predicate NOT_NULL_OR_EMPTY = new Predicate() { @Override public boolean test(CharSequence value) { return value != null && value.length() > 0; } }; /** The Constant NOT_NULL_OR_EMPTY_OR_BLANK. */ private static final Predicate NOT_NULL_OR_EMPTY_OR_BLANK = new Predicate() { @Override public boolean test(CharSequence value) { return N.notNullOrEmptyOrBlank(value); } }; /** The Constant IS_FILE. */ private static final Predicate IS_FILE = new Predicate() { @Override public boolean test(File value) { return value.isFile(); } }; /** The Constant IS_DIRECTORY. */ private static final Predicate IS_DIRECTORY = new Predicate() { @Override public boolean test(File value) { return value.isDirectory(); } }; /** * Instantiates a new fn. */ protected Fn() { super(); // for extention. } /** * * @param * @param supplier * @return */ public static T get(final 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 */ @Beta @Stateful public static Supplier memoize(final Supplier supplier) { return LazyInitializer.of(supplier); } /** * * @param * @param * @param func * @return */ @Beta @Stateful public static Function memoize(final Function func) { return new Function() { private volatile R resultForNull = (R) NONE; private volatile Map resultMap = null; @Override public R apply(T t) { R result = null; if (t == null) { result = resultForNull; if (result == NONE) { synchronized (this) { if (resultForNull == NONE) { resultForNull = func.apply(t); } result = resultForNull; } } } else { synchronized (this) { if (resultMap == null) { resultMap = new HashMap<>(); } result = resultMap.get(t); if (result == null && resultMap.containsKey(t) == false) { result = func.apply(t); resultMap.put(t, result); } } } return 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 */ @Stateful @Deprecated @Beta @SequentialOnly public static > Supplier reuse(final 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 */ @Stateful @Deprecated @Beta @SequentialOnly public static > IntFunction reuse(final 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 com.landawn.abacus.util.function.Runnable close(final AutoCloseable closeable) { return new com.landawn.abacus.util.function.Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.close(closeable); } }; } /** * * @param a * @return */ @SafeVarargs public static com.landawn.abacus.util.function.Runnable closeAll(final AutoCloseable... a) { return new com.landawn.abacus.util.function.Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAll(a); } }; } /** * * @param c * @return */ public static com.landawn.abacus.util.function.Runnable closeAll(final Collection c) { return new com.landawn.abacus.util.function.Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAll(c); } }; } /** * * @param closeable * @return */ public static com.landawn.abacus.util.function.Runnable closeQuietly(final AutoCloseable closeable) { return new com.landawn.abacus.util.function.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 com.landawn.abacus.util.function.Runnable closeAllQuietly(final AutoCloseable... a) { return new com.landawn.abacus.util.function.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 com.landawn.abacus.util.function.Runnable closeAllQuietly(final Collection c) { return new com.landawn.abacus.util.function.Runnable() { private volatile boolean isClosed = false; @Override public void run() { if (isClosed) { return; } isClosed = true; IOUtil.closeAllQuietly(c); } }; } /** * * @return */ public static com.landawn.abacus.util.function.Runnable emptyAction() { return EMPTY_ACTION; } /** * * @param * @return */ public static Consumer doNothing() { return DO_NOTHING; } /** * * @param * @param errorMessage * @return */ public static Consumer throwRuntimeException(final String errorMessage) { return new Consumer() { @Override public void accept(T t) { throw new RuntimeException(errorMessage); } }; } /** * * @param * @param excpetionSupplier * @return */ public static Consumer throwException(final Supplier excpetionSupplier) { return new Consumer() { @Override public void accept(T t) { throw excpetionSupplier.get(); } }; } /** * * @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 new Consumer() { @Override public void accept(T t) { N.sleep(millis); } }; } /** * * @param * @param millis * @return */ public static Consumer sleepUninterruptibly(final long millis) { return new Consumer() { @Override public void accept(T t) { N.sleepUninterruptibly(millis); } }; } /** * * @param * @return */ public static Consumer println() { return PRINTLN; } /** * * @param * @param * @param separator * @return */ public static BiConsumer println(final String separator) { N.checkArgNotNull(separator); switch (separator) { 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 new BiConsumer() { @Override public void accept(T t, U u) { N.println(t + separator + u); } }; } } /** * * @param * @return */ public static Function toStr() { return TO_STRING; } /** * To camel case. * * @return */ public static Function toCamelCase() { return TO_CAMEL_CASE; } /** * To lower case. * * @return */ public static Function toLowerCase() { return TO_LOWER_CASE; } /** * To lower case with underscore. * * @return */ public static Function toLowerCaseWithUnderscore() { return TO_LOWER_CASE_WITH_UNDERSCORE; } /** * To upper case. * * @return */ public static Function toUpperCase() { return TO_UPPER_CASE; } /** * To upper case with underscore. * * @return */ public static Function toUpperCaseWithUnderscore() { return TO_UPPER_CASE_WITH_UNDERSCORE; } /** * * @param * @return */ public static Function identity() { return IDENTITY; } /** * * @param the key type * @param * @param keyMapper * @return */ public static Function> keyed(final Function keyMapper) { N.checkArgNotNull(keyMapper); return new Function>() { @Override public Keyed apply(T t) { return Keyed.of(keyMapper.apply(t), t); } }; } /** The Constant VAL. */ private static final Function, Object> VAL = new Function, Object>() { @Override public Object apply(Keyed t) { return t.val(); } }; /** The Constant KK. */ private static final Function, Object>, Object> KK = new Function, Object>, Object>() { @Override public Object apply(Map.Entry, Object> t) { return 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> kk() { return (Function) KK; } /** The Constant WRAP. */ private static final Function> WRAP = new Function>() { @Override public Wrapper apply(Object t) { return Wrapper.of(t); } }; /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function> wrap() { return (Function) WRAP; } /** * * @param * @param hashFunction * @param equalsFunction * @return */ public static Function> wrap(final ToIntFunction hashFunction, final BiPredicate equalsFunction) { N.checkArgNotNull(hashFunction); N.checkArgNotNull(equalsFunction); return new Function>() { @Override public Wrapper apply(T t) { return Wrapper.of(t, hashFunction, equalsFunction); } }; } /** The Constant UNWRAP. */ private static final Function, Object> UNWRAP = new Function, Object>() { @Override public Object apply(Wrapper t) { return t.value(); } }; /** * * @param the key type * @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; } @SuppressWarnings("rawtypes") public static Function, L> left() { return (Function) LEFT; } @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 keyMapper * @return * @deprecated replaced by {@code Fn#entryByKeyMapper(Function)} */ @Deprecated public static Function> entry(final Function keyMapper) { return entryByKeyMapper(keyMapper); } /** * * @param the key type * @param the value type * @param key * @return */ public static Function> entryWithKey(final K key) { return new Function>() { @Override public Entry apply(V v) { return new ImmutableEntry<>(key, v); } }; } /** * * @param the key type * @param the value type * @param keyMapper * @return */ public static Function> entryByKeyMapper(final Function keyMapper) { N.checkArgNotNull(keyMapper); return new Function>() { @Override public Entry apply(V v) { return new ImmutableEntry<>(keyMapper.apply(v), v); } }; } /** * * @param the key type * @param the value type * @param value * @return */ public static Function> entryWithValue(final V value) { return new Function>() { @Override public Entry apply(K k) { return new ImmutableEntry<>(k, value); } }; } /** * * @param the key type * @param the value type * @param valueMapper * @return */ public static Function> entryByValueMapper(final Function valueMapper) { N.checkArgNotNull(valueMapper); return new Function>() { @Override public Entry apply(K k) { return new ImmutableEntry<>(k, valueMapper.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; } /** * * @return */ public static Function trim() { return TRIM; } /** * Trim to empty. * * @return */ public static Function trimToEmpty() { return TRIM_TO_EMPTY; } /** * Trim to null. * * @return */ public static Function trimToNull() { return TRIM_TO_NULL; } /** * * @return */ public static Function nullToEmpty() { return NULL_TO_EMPTY; } /** * * @param * @return * @deprecated replaced by {@code nullToEmptyList} */ @Deprecated @SuppressWarnings("rawtypes") public static Function, List> nullToEmptyL() { return (Function) NULL_TO_EMPTY_LIST; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, List> nullToEmptyList() { return (Function) NULL_TO_EMPTY_LIST; } /** * * @param * @return * @deprecated replaced by {@code nullToEmptySet} */ @Deprecated @SuppressWarnings("rawtypes") public static Function, Set> nullToEmptyS() { return (Function) NULL_TO_EMPTY_SET; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function, Set> nullToEmptySet() { return (Function) NULL_TO_EMPTY_SET; } /** * * @param the key type * @param the value type * @return * @deprecated replaced by {@code nullToEmptyMap} */ @Deprecated @SuppressWarnings("rawtypes") public static Function, Map> nullToEmptyM() { return (Function) NULL_TO_EMPTY_MAP; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Function, Map> nullToEmptyMap() { return (Function) NULL_TO_EMPTY_MAP; } /** * * @param * @return */ public static Function length() { return (Function) LENGTH; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Function len() { return (Function) LEN; } /** * * @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 */ public static Function cast(final Class clazz) { N.checkArgNotNull(clazz); return new Function() { @Override public U apply(T t) { return (U) t; } }; } /** * * @param * @return */ public static Predicate alwaysTrue() { return ALWAYS_TRUE; } /** * * @param * @return */ public static Predicate alwaysFalse() { return ALWAYS_FALSE; } /** * Checks if is null. * * @param * @return */ public static Predicate isNull() { return IS_NULL; } /** * Checks if is null. * * @param * @param valueExtractor * @return */ public static Predicate isNull(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return valueExtractor.apply(t) == null; } }; } /** * Checks if is null or empty. * * @param * @return */ public static Predicate isNullOrEmpty() { return (Predicate) IS_NULL_OR_EMPTY; } /** * * @param * @param valueExtractor * @return */ public static Predicate isNullOrEmpty(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return N.isNullOrEmpty(valueExtractor.apply(t)); } }; } /** * Checks if is null or empty or blank. * * @param * @return */ public static Predicate isNullOrEmptyOrBlank() { return (Predicate) IS_NULL_OR_EMPTY_OR_BLANK; } /** * * @param * @param valueExtractor * @return */ public static Predicate isNullOrEmptyOrBlank(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return N.isNullOrEmptyOrBlank(valueExtractor.apply(t)); } }; } /** * * @param * @return */ public static Predicate notNull() { return NOT_NULL; } /** * Not null * * @param * @param valueExtractor * @return */ public static Predicate notNull(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return valueExtractor.apply(t) != null; } }; } /** * Not null or empty. * * @param * @return */ public static Predicate notNullOrEmpty() { return (Predicate) NOT_NULL_OR_EMPTY; } /** * * @param * @param valueExtractor * @return */ public static Predicate notNullOrEmpty(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return N.notNullOrEmpty(valueExtractor.apply(t)); } }; } /** * Not null or empty or blank. * * @param * @return */ public static Predicate notNullOrEmptyOrBlank() { return (Predicate) NOT_NULL_OR_EMPTY_OR_BLANK; } /** * * @param * @param valueExtractor * @return */ public static Predicate notNullOrEmptyOrBlank(final Function valueExtractor) { return new Predicate() { @Override public boolean test(T t) { return N.notNullOrEmptyOrBlank(valueExtractor.apply(t)); } }; } /** * 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 new Predicate() { @Override public boolean test(T value) { return N.equals(value, target); } }; } /** * * @param * @param target * @return */ public static Predicate notEqual(final Object target) { return new Predicate() { @Override public boolean test(T value) { return !N.equals(value, target); } }; } /** * * @param * @param target * @return */ public static > Predicate greaterThan(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) > 0; } }; } /** * * @param * @param target * @return */ public static > Predicate greaterEqual(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) >= 0; } }; } /** * * @param * @param target * @return */ public static > Predicate lessThan(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) < 0; } }; } /** * * @param * @param target * @return */ public static > Predicate lessEqual(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) <= 0; } }; } /** * Checks if the value/element: {@code minValue < e < maxValue}. * * @param * @param minValue * @param maxValue * @return */ public static > Predicate between(final T minValue, final T maxValue) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, minValue) > 0 && N.compare(value, maxValue) < 0; } }; } /** * * @param * @param c * @return */ public static Predicate in(final Collection c) { N.checkArgNotNull(c); final boolean isNotEmpty = N.notNullOrEmpty(c); return new Predicate() { @Override public boolean test(T value) { return isNotEmpty && c.contains(value); } }; } /** * * @param * @param c * @return */ public static Predicate notIn(final Collection c) { N.checkArgNotNull(c); final boolean isEmpty = N.isNullOrEmpty(c); return new Predicate() { @Override public boolean test(T value) { return isEmpty || !c.contains(value); } }; } /** * * @param * @param clazz * @return */ public static Predicate instanceOf(final Class clazz) { N.checkArgNotNull(clazz); return new Predicate() { @Override public boolean test(T value) { return value != null && clazz.isInstance(value); } }; } /** * * @param clazz * @return */ @SuppressWarnings("rawtypes") public static Predicate subtypeOf(final Class clazz) { N.checkArgNotNull(clazz); return new Predicate() { @Override public boolean test(Class value) { return clazz.isAssignableFrom(value); } }; } /** * * @param prefix * @return */ public static Predicate startsWith(final String prefix) { N.checkArgNotNull(prefix); return new Predicate() { @Override public boolean test(String value) { return value != null && value.startsWith(prefix); } }; } /** * * @param suffix * @return */ public static Predicate endsWith(final String suffix) { N.checkArgNotNull(suffix); return new Predicate() { @Override public boolean test(String value) { return value != null && value.endsWith(suffix); } }; } /** * * @param str * @return */ public static Predicate contains(final String str) { N.checkArgNotNull(str); return new Predicate() { @Override public boolean test(String value) { return value != null && value.contains(str); } }; } /** * Not starts with. * * @param prefix * @return */ public static Predicate notStartsWith(final String prefix) { N.checkArgNotNull(prefix); return new Predicate() { @Override public boolean test(String value) { return value == null || !value.startsWith(prefix); } }; } /** * Not ends with. * * @param suffix * @return */ public static Predicate notEndsWith(final String suffix) { N.checkArgNotNull(suffix); return new Predicate() { @Override public boolean test(String value) { return value == null || !value.endsWith(suffix); } }; } /** * * @param str * @return */ public static Predicate notContains(final String str) { N.checkArgNotNull(str); return new Predicate() { @Override public boolean test(String value) { return value == null || !value.contains(str); } }; } /** * * @param pattern * @return */ public static Predicate matches(final Pattern pattern) { N.checkArgNotNull(pattern); return new Predicate() { @Override public boolean test(CharSequence value) { return 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 */ public static Predicate not(final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate() { @Override public boolean test(T t) { return !predicate.test(t); } }; } /** * * @param * @param * @param biPredicate * @return */ public static BiPredicate not(final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new BiPredicate() { @Override public boolean test(T t, U u) { return !biPredicate.test(t, u); } }; } /** * * @param * @param * @param * @param triPredicate * @return */ public static TriPredicate not(final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new TriPredicate() { @Override public boolean test(A a, B b, C c) { return !triPredicate.test(a, b, c); } }; } /** * * @param first * @param second * @return */ public static BooleanSupplier and(final BooleanSupplier first, final BooleanSupplier second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new BooleanSupplier() { @Override public boolean getAsBoolean() { return first.getAsBoolean() && second.getAsBoolean(); } }; } /** * * @param first * @param second * @param third * @return */ public static BooleanSupplier and(final BooleanSupplier first, final BooleanSupplier second, final BooleanSupplier third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new BooleanSupplier() { @Override public boolean getAsBoolean() { return first.getAsBoolean() && second.getAsBoolean() && third.getAsBoolean(); } }; } /** * * @param * @param first * @param second * @return */ public static Predicate and(final Predicate first, final Predicate second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new Predicate() { @Override public boolean test(T t) { return first.test(t) && second.test(t); } }; } /** * * @param * @param first * @param second * @param third * @return */ public static Predicate and(final Predicate first, final Predicate second, final Predicate third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new Predicate() { @Override public boolean test(T t) { return first.test(t) && second.test(t) && third.test(t); } }; } /** * * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is null or empty. */ public static Predicate and(final Collection> c) throws IllegalArgumentException { N.checkArgNotNullOrEmpty(c, "c"); return new Predicate() { @Override public boolean test(T t) { for (Predicate p : c) { if (p.test(t) == false) { return false; } } return true; } }; } /** * * @param * @param * @param first * @param second * @return */ public static BiPredicate and(final BiPredicate first, final BiPredicate second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new BiPredicate() { @Override public boolean test(T t, U u) { return first.test(t, u) && second.test(t, u); } }; } /** * * @param * @param * @param first * @param second * @param third * @return */ public static BiPredicate and(final BiPredicate first, final BiPredicate second, final BiPredicate third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new BiPredicate() { @Override public boolean test(T t, U u) { return 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 null or empty. */ public static BiPredicate and(final List> c) throws IllegalArgumentException { N.checkArgNotNullOrEmpty(c, "c"); return new BiPredicate() { @Override public boolean test(T t, U u) { for (BiPredicate p : c) { if (p.test(t, u) == false) { return false; } } return true; } }; } /** * * @param first * @param second * @return */ public static BooleanSupplier or(final BooleanSupplier first, final BooleanSupplier second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new BooleanSupplier() { @Override public boolean getAsBoolean() { return first.getAsBoolean() || second.getAsBoolean(); } }; } /** * * @param first * @param second * @param third * @return */ public static BooleanSupplier or(final BooleanSupplier first, final BooleanSupplier second, final BooleanSupplier third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new BooleanSupplier() { @Override public boolean getAsBoolean() { return first.getAsBoolean() || second.getAsBoolean() || third.getAsBoolean(); } }; } /** * * @param * @param first * @param second * @return */ public static Predicate or(final Predicate first, final Predicate second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new Predicate() { @Override public boolean test(T t) { return first.test(t) || second.test(t); } }; } /** * * @param * @param first * @param second * @param third * @return */ public static Predicate or(final Predicate first, final Predicate second, final Predicate third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new Predicate() { @Override public boolean test(T t) { return first.test(t) || second.test(t) || third.test(t); } }; } /** * * @param * @param c * @return * @throws IllegalArgumentException if the specified {@code c} is null or empty. */ public static Predicate or(final Collection> c) throws IllegalArgumentException { N.checkArgNotNullOrEmpty(c, "c"); return new Predicate() { @Override public boolean test(T t) { for (Predicate p : c) { if (p.test(t)) { return true; } } return false; } }; } /** * * @param * @param * @param first * @param second * @return */ public static BiPredicate or(final BiPredicate first, final BiPredicate second) { N.checkArgNotNull(first); N.checkArgNotNull(second); return new BiPredicate() { @Override public boolean test(T t, U u) { return first.test(t, u) || second.test(t, u); } }; } /** * * @param * @param * @param first * @param second * @param third * @return */ public static BiPredicate or(final BiPredicate first, final BiPredicate second, final BiPredicate third) { N.checkArgNotNull(first); N.checkArgNotNull(second); N.checkArgNotNull(third); return new BiPredicate() { @Override public boolean test(T t, U u) { return 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 null or empty. */ public static BiPredicate or(final List> c) throws IllegalArgumentException { N.checkArgNotNullOrEmpty(c, "c"); return new BiPredicate() { @Override public boolean test(T t, U u) { for (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 */ public static Predicate> testByKey(final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate>() { @Override public boolean test(Entry entry) { return predicate.test(entry.getKey()); } }; } /** * Test by value. * * @param the key type * @param the value type * @param predicate * @return */ public static Predicate> testByValue(final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate>() { @Override public boolean test(Entry entry) { return predicate.test(entry.getValue()); } }; } /** * Returns the specified instance. * * @param * @param predicate * @return * @deprecated replaced by {@link Fn#p(Predicate)}. */ @Deprecated static Predicate test(final Predicate predicate) { return predicate; } /** * Returns the specified instance. * * @param * @param * @param predicate * @return * @deprecated replaced by {@link Fn#p(BiPredicate)}. */ @Deprecated static BiPredicate test(final BiPredicate predicate) { return predicate; } /** * Returns the specified instance. * * @param * @param * @param * @param predicate * @return * @deprecated */ @Deprecated static TriPredicate test(final TriPredicate predicate) { return predicate; } /** * Accept by key. * * @param the key type * @param the value type * @param consumer * @return */ public static Consumer> acceptByKey(final Consumer consumer) { N.checkArgNotNull(consumer); return new Consumer>() { @Override public void accept(Entry entry) { consumer.accept(entry.getKey()); } }; } /** * Accept by value. * * @param the key type * @param the value type * @param consumer * @return */ public static Consumer> acceptByValue(final Consumer consumer) { N.checkArgNotNull(consumer); return new Consumer>() { @Override public void accept(Entry entry) { consumer.accept(entry.getValue()); } }; } /** * Apply by key. * * @param the key type * @param the value type * @param * @param func * @return */ public static Function, R> applyByKey(final Function func) { N.checkArgNotNull(func); return new Function, R>() { @Override public R apply(Entry entry) { return func.apply(entry.getKey()); } }; } /** * Apply by value. * * @param the key type * @param the value type * @param * @param func * @return */ public static Function, R> applyByValue(final Function func) { N.checkArgNotNull(func); return new Function, R>() { @Override public R apply(Entry entry) { return func.apply(entry.getValue()); } }; } /** * Apply if not null or default. * * @param * @param * @param * @param mapperA * @param mapperB * @param defaultValue * @return */ public static Function applyIfNotNullOrDefault(final Function mapperA, final Function mapperB, final R defaultValue) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); return new Function() { @Override public R apply(A 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 null or default. * * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param defaultValue * @return */ public static Function applyIfNotNullOrDefault(final Function mapperA, final Function mapperB, final Function mapperC, final R defaultValue) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); return new Function() { @Override public R apply(A 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 null or default. * * @param * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param mapperD * @param defaultValue * @return */ public static Function applyIfNotNullOrDefault(final Function mapperA, final Function mapperB, final Function mapperC, final Function mapperD, final R defaultValue) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); N.checkArgNotNull(mapperD); return new Function() { @Override public R apply(A 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 null or get. * * @param * @param * @param * @param mapperA * @param mapperB * @param supplier * @return */ public static Function applyIfNotNullOrGet(final Function mapperA, final Function mapperB, final Supplier supplier) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); return new Function() { @Override public R apply(A 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 null or get. * * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param supplier * @return */ public static Function applyIfNotNullOrGet(final Function mapperA, final Function mapperB, final Function mapperC, final Supplier supplier) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); return new Function() { @Override public R apply(A 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 null or get. * * @param * @param * @param * @param * @param * @param mapperA * @param mapperB * @param mapperC * @param mapperD * @param supplier * @return */ public static Function applyIfNotNullOrGet(final Function mapperA, final Function mapperB, final Function mapperC, final Function mapperD, final Supplier supplier) { N.checkArgNotNull(mapperA); N.checkArgNotNull(mapperB); N.checkArgNotNull(mapperC); N.checkArgNotNull(mapperD); return new Function() { @Override public R apply(A 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 the key type * @param the value type * @param * @param func * @return */ public static Function, Map.Entry> mapKey(final Function func) { N.checkArgNotNull(func); return new Function, Map.Entry>() { @Override public Map.Entry apply(Entry entry) { return new ImmutableEntry(func.apply(entry.getKey()), entry.getValue()); } }; } /** * * @param the key type * @param the value type * @param * @param func * @return */ public static Function, Map.Entry> mapValue(final Function func) { N.checkArgNotNull(func); return new Function, Map.Entry>() { @Override public Map.Entry apply(Entry entry) { return new ImmutableEntry(entry.getKey(), func.apply(entry.getValue())); } }; } /** * Test key val. * * @param the key type * @param the value type * @param predicate * @return */ public static Predicate> testKeyVal(final BiPredicate predicate) { N.checkArgNotNull(predicate); return new Predicate>() { @Override public boolean test(Entry entry) { return predicate.test(entry.getKey(), entry.getValue()); } }; } /** * Accept key val. * * @param the key type * @param the value type * @param consumer * @return */ public static Consumer> acceptKeyVal(final BiConsumer consumer) { N.checkArgNotNull(consumer); return new Consumer>() { @Override public void accept(Entry entry) { consumer.accept(entry.getKey(), entry.getValue()); } }; } /** * Apply key val. * * @param the key type * @param the value type * @param * @param func * @return */ public static Function, R> applyKeyVal(final BiFunction func) { N.checkArgNotNull(func); return new Function, R>() { @Override public R apply(Entry entry) { return func.apply(entry.getKey(), entry.getValue()); } }; } /** The parse byte func. */ private static Function PARSE_BYTE_FUNC = new Function() { @Override public Byte apply(String t) { return N.parseByte(t); } }; /** * Parses the byte. * * @return */ public static Function parseByte() { return PARSE_BYTE_FUNC; } /** The parse short func. */ private static Function PARSE_SHORT_FUNC = new Function() { @Override public Short apply(String t) { return N.parseShort(t); } }; /** * Parses the short. * * @return */ public static Function parseShort() { return PARSE_SHORT_FUNC; } /** The parse int func. */ private static Function PARSE_INT_FUNC = new Function() { @Override public Integer apply(String t) { return N.parseInt(t); } }; /** * Parses the int. * * @return */ public static Function parseInt() { return PARSE_INT_FUNC; } /** The parse long func. */ private static Function PARSE_LONG_FUNC = new Function() { @Override public Long apply(String t) { return N.parseLong(t); } }; /** * Parses the long. * * @return */ public static Function parseLong() { return PARSE_LONG_FUNC; } /** The parse float func. */ private static Function PARSE_FLOAT_FUNC = new Function() { @Override public Float apply(String t) { return N.parseFloat(t); } }; /** * Parses the float. * * @return */ public static Function parseFloat() { return PARSE_FLOAT_FUNC; } /** The parse double func. */ private static Function PARSE_DOUBLE_FUNC = new Function() { @Override public Double apply(String t) { return N.parseDouble(t); } }; /** * Parses the double. * * @return */ public static Function parseDouble() { return PARSE_DOUBLE_FUNC; } /** The create number func. */ private static Function CREATE_NUMBER_FUNC = new Function() { @Override public Number apply(final String t) { return N.isNullOrEmpty(t) ? null : StringUtil.createNumber(t).orElseThrow(new Supplier() { @Override public NumberFormatException get() { return new NumberFormatException("Invalid number: " + t); } }); } }; /** * Creates the number. * * @return * @see StringUtil#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; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param limit * @param predicate * @return */ @Beta @Stateful public static Predicate limitThenFilter(final int limit, final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(T t) { return counter.getAndDecrement() > 0 && predicate.test(t); } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param * @param limit * @param predicate * @return */ @Beta @Stateful public static BiPredicate limitThenFilter(final int limit, final BiPredicate predicate) { N.checkArgNotNull(predicate); return new BiPredicate() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(T t, U u) { return counter.getAndDecrement() > 0 && predicate.test(t, u); } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param predicate * @param limit * @return */ @Beta @Stateful public static Predicate filterThenLimit(final Predicate predicate, final int limit) { N.checkArgNotNull(predicate); return new Predicate() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(T t) { return predicate.test(t) && counter.getAndDecrement() > 0; } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param * @param predicate * @param limit * @return */ @Beta @Stateful public static BiPredicate filterThenLimit(final BiPredicate predicate, final int limit) { N.checkArgNotNull(predicate); return new BiPredicate() { private final AtomicInteger counter = new AtomicInteger(limit); @Override public boolean test(T t, U u) { return predicate.test(t, u) && counter.getAndDecrement() > 0; } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param timeInMillis * @return */ @Beta @Stateful public static Predicate timeLimit(final long timeInMillis) { N.checkArgNotNegative(timeInMillis, "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 new Predicate() { @Override public boolean test(T t) { return ongoing.value(); } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param duration * @return */ @Beta @Stateful public static Predicate timeLimit(final Duration duration) { N.checkArgNotNull(duration, "duration"); return timeLimit(duration.toMillis()); } /** * Returns a stateful Predicate. Don't save it 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(T t) { return Indexed.of(t, idx.getAndIncrement()); } }; } /** * Returns a stateful Predicate. Don't save it or use it in parallel stream. * * @param * @param predicate * @return */ @Beta @SequentialOnly @Stateful public static Predicate indexed(final IndexedPredicate predicate) { return Predicates.indexed(predicate); } /** * * @param * @param target * @return */ public static > Function compareTo(final T target) { return new Function() { @Override public Integer apply(T t) { return N.compare(t, target); } }; } /** * * @param * @param target * @param cmp * @return */ public static Function compareTo(final T target, final Comparator cmp) { // N.checkArgNotNull(cmp); if (cmp == null) { return new Function() { @Override public Integer apply(T t) { return NATURAL_ORDER.compare(t, target); } }; } else { return new Function() { @Override public Integer apply(T t) { return N.compare(t, target, cmp); } }; } } /** * * @param * @return */ public static > BiFunction compare() { return COMPARE; } /** * * @param * @param cmp * @return */ public static BiFunction compare(final Comparator cmp) { // N.checkArgNotNull(cmp); if (cmp == null || cmp == Comparators.naturalOrder()) { return COMPARE; } return new BiFunction() { @Override public Integer apply(T a, T b) { return N.compare(a, b, cmp); } }; } // /** // * // * @param // * @param supplier // * @return // */ // @Beta // public static Function, T> getOrDefaultOnError(final T defaultValue) { // return new Function, T>() { // @Override // public T apply(Future f) { // try { // return f.get(); // } catch (InterruptedException | ExecutionException e) { // // throw N.toRuntimeException(e); // return defaultValue; // } // } // }; // } // // private static final Function, Object> GET_OR_THROW = new Function, Object>() { // @Override // public Object apply(Future f) { // try { // return f.get(); // } catch (InterruptedException | ExecutionException e) { // return N.toRuntimeException(e); // } // } // }; // // /** // * // * @param // * @param supplier // * @return // */ // @SuppressWarnings("rawtypes") // @Beta // public static Function, T> getOrThrowOnError() { // return (Function) GET_OR_THROW; // } /** * * @param * @param predicate * @return */ @Beta public static Predicate p(final Predicate predicate) { return predicate; } /** * * @param * @param * @param a * @param biPredicate * @return */ @Beta public static Predicate p(final A a, final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Predicate() { @Override public boolean test(T t) { return biPredicate.test(a, t); } }; } /** * * @param * @param * @param * @param a * @param b * @param triPredicate * @return */ @Beta public static Predicate p(final A a, final B b, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Predicate() { @Override public boolean test(T t) { return 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 */ @Beta public static BiPredicate p(final A a, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new BiPredicate() { @Override public boolean test(T t, U u) { return 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 */ @Beta public static Consumer c(final A a, final BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Consumer() { @Override public void accept(T t) { biConsumer.accept(a, t); } }; } /** * * @param * @param * @param * @param a * @param b * @param triConsumer * @return */ @Beta public static Consumer c(final A a, final B b, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Consumer() { @Override public void accept(T 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 */ @Beta public static BiConsumer c(final A a, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new BiConsumer() { @Override public void accept(T t, U 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 */ @Beta public static Function f(final A a, final BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Function() { @Override public R apply(T t) { return biFunction.apply(a, t); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return */ @Beta public static Function f(final A a, final B b, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Function() { @Override public R apply(T t) { return 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 */ @Beta public static BiFunction f(final A a, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new BiFunction() { @Override public R apply(T t, U u) { return triFunction.apply(a, t, u); } }; } /** * * @param * @param * @param * @param * @param triFunction * @return */ @Beta public static TriFunction f(final TriFunction triFunction) { return triFunction; } /** * * @param * @param * @param predicate * @return */ @Beta public static Predicate pp(final Throwables.Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate() { @Override public boolean test(T value) { try { return predicate.test(value); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param a * @param biPredicate * @return */ @Beta public static Predicate pp(final A a, final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Predicate() { @Override public boolean test(T t) { try { return biPredicate.test(a, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triPredicate * @return */ @Beta public static Predicate pp(final A a, final B b, final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Predicate() { @Override public boolean test(T t) { try { return triPredicate.test(a, b, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param biPredicate * @return */ @Beta public static BiPredicate pp(final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new BiPredicate() { @Override public boolean test(T t, U u) { try { return biPredicate.test(t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param a * @param triPredicate * @return */ @Beta public static BiPredicate pp(final A a, final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new BiPredicate() { @Override public boolean test(T t, U u) { try { return triPredicate.test(a, t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param triPredicate * @return */ @Beta public static TriPredicate pp(final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new TriPredicate() { @Override public boolean test(A a, B b, C c) { try { return triPredicate.test(a, b, c); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param consumer * @return */ @Beta public static Consumer cc(final Throwables.Consumer consumer) { N.checkArgNotNull(consumer); return new Consumer() { @Override public void accept(T t) { try { consumer.accept(t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param a * @param biConsumer * @return */ @Beta public static Consumer cc(final A a, final Throwables.BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Consumer() { @Override public void accept(T t) { try { biConsumer.accept(a, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triConsumer * @return */ @Beta public static Consumer cc(final A a, final B b, final Throwables.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Consumer() { @Override public void accept(T t) { try { triConsumer.accept(a, b, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param biConsumer * @return */ @Beta public static BiConsumer cc(final Throwables.BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new BiConsumer() { @Override public void accept(T t, U u) { try { biConsumer.accept(t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param a * @param triConsumer * @return */ @Beta public static BiConsumer cc(final A a, final Throwables.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new BiConsumer() { @Override public void accept(T t, U u) { try { triConsumer.accept(a, t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param triConsumer * @return */ @Beta public static TriConsumer cc(final Throwables.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new TriConsumer() { @Override public void accept(A a, B b, C c) { try { triConsumer.accept(a, b, c); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param function * @return */ @Beta public static Function ff(final Throwables.Function function) { N.checkArgNotNull(function); return new Function() { @Override public R apply(T t) { try { return function.apply(t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param function * @param defaultOnError * @return */ @Beta public static Function ff(final Throwables.Function function, final R defaultOnError) { N.checkArgNotNull(function); return new Function() { @Override public R apply(T t) { try { return function.apply(t); } catch (Exception e) { return defaultOnError; } } }; } /** * * @param * @param * @param * @param * @param a * @param biFunction * @return */ @Beta public static Function ff(final A a, final Throwables.BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Function() { @Override public R apply(T t) { try { return biFunction.apply(a, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return */ @Beta public static Function ff(final A a, final B b, final Throwables.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Function() { @Override public R apply(T t) { try { return triFunction.apply(a, b, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param biFunction * @return */ @Beta public static BiFunction ff(final Throwables.BiFunction biFunction) { N.checkArgNotNull(biFunction); return new BiFunction() { @Override public R apply(T t, U u) { try { return biFunction.apply(t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param biFunction * @param defaultOnError * @return */ @Beta public static BiFunction ff(final Throwables.BiFunction biFunction, final R defaultOnError) { N.checkArgNotNull(biFunction); return new BiFunction() { @Override public R apply(T t, U u) { try { return biFunction.apply(t, u); } catch (Exception e) { return defaultOnError; } } }; } /** * * @param * @param * @param * @param * @param * @param a * @param triFunction * @return */ @Beta public static BiFunction ff(final A a, final Throwables.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new BiFunction() { @Override public R apply(T t, U u) { try { return triFunction.apply(a, t, u); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param * @param triFunction * @return */ @Beta public static TriFunction ff(final Throwables.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new TriFunction() { @Override public R apply(A a, B b, C c) { try { return triFunction.apply(a, b, c); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * * @param * @param * @param * @param * @param * @param triFunction * @param defaultOnError * @return */ @Beta public static TriFunction ff(final Throwables.TriFunction triFunction, final R defaultOnError) { N.checkArgNotNull(triFunction); return new TriFunction() { @Override public R apply(A a, B b, C c) { try { return triFunction.apply(a, b, c); } catch (Exception e) { return defaultOnError; } } }; } /** * Synchronized {@code Predicate}. * * @param * @param mutex to synchronized on * @param predicate * @return */ @Beta public static Predicate sp(final Object mutex, final Predicate predicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(predicate, "predicate"); return new Predicate() { @Override public boolean test(T t) { synchronized (mutex) { return predicate.test(t); } } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param mutex to synchronized on * @param a * @param biPredicate * @return */ @Beta public static Predicate sp(final Object mutex, final A a, final BiPredicate biPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new Predicate() { @Override public boolean test(T 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 */ @Beta public static Predicate sp(final Object mutex, final A a, final B b, final TriPredicate triPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(triPredicate, "triPredicate"); return new Predicate() { @Override public boolean test(T t) { synchronized (mutex) { return triPredicate.test(a, b, t); } } }; } /** * Synchronized {@code BiPredicate}. * * @param * @param * @param mutex to synchronized on * @param biPredicate * @return */ @Beta public static BiPredicate sp(final Object mutex, final BiPredicate biPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new BiPredicate() { @Override public boolean test(T t, U u) { synchronized (mutex) { return biPredicate.test(t, u); } } }; } /** * Synchronized {@code Consumer}. * * @param * @param mutex to synchronized on * @param consumer * @return */ @Beta public static Consumer sc(final Object mutex, final Consumer consumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(consumer, "consumer"); return new Consumer() { @Override public void accept(T t) { synchronized (mutex) { consumer.accept(t); } } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param mutex to synchronized on * @param a * @param biConsumer * @return */ @Beta public static Consumer sc(final Object mutex, final A a, final BiConsumer biConsumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new Consumer() { @Override public void accept(T t) { synchronized (mutex) { biConsumer.accept(a, t); } } }; } /** * Synchronized {@code BiConsumer}. * * @param * @param * @param mutex to synchronized on * @param biConsumer * @return */ @Beta public static BiConsumer sc(final Object mutex, final BiConsumer biConsumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new BiConsumer() { @Override public void accept(T t, U u) { synchronized (mutex) { biConsumer.accept(t, u); } } }; } /** * Synchronized {@code Function}. * * @param * @param * @param mutex to synchronized on * @param function * @return */ @Beta public static Function sf(final Object mutex, final Function function) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(function, "function"); return new Function() { @Override public R apply(T t) { synchronized (mutex) { return function.apply(t); } } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param biFunction * @return */ @Beta public static Function sf(final Object mutex, final A a, final BiFunction biFunction) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new Function() { @Override public R apply(T t) { synchronized (mutex) { return biFunction.apply(a, t); } } }; } /** * Synchronized {@code BiFunction}. * * @param * @param * @param * @param mutex to synchronized on * @param biFunction * @return */ @Beta public static BiFunction sf(final Object mutex, final BiFunction biFunction) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new BiFunction() { @Override public R apply(T t, U u) { synchronized (mutex) { return biFunction.apply(t, u); } } }; } public static com.landawn.abacus.util.function.Runnable rr(final Throwables.Runnable runnbale) { return new com.landawn.abacus.util.function.Runnable() { @Override public void run() { try { runnbale.run(); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } public static com.landawn.abacus.util.function.Callable cc(final Throwables.Callable callable) { return new com.landawn.abacus.util.function.Callable() { @Override public R call() { try { return callable.call(); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } /** * Callable. * * @param * @param callable * @return */ public static com.landawn.abacus.util.function.Callable callable(final com.landawn.abacus.util.function.Callable callable) { N.checkArgNotNull(callable); return callable; } /** * * @param runnable * @return */ public static com.landawn.abacus.util.function.Runnable runnable(final com.landawn.abacus.util.function.Runnable runnable) { N.checkArgNotNull(runnable); return runnable; } /** * * @param runnable * @return */ public static com.landawn.abacus.util.function.Callable toCallable(final com.landawn.abacus.util.function.Runnable runnable) { N.checkArgNotNull(runnable); return new com.landawn.abacus.util.function.Callable() { @Override public Void call() { runnable.run(); return null; } }; } /** * * @param runnable * @param result * @return */ public static com.landawn.abacus.util.function.Callable toCallable(final com.landawn.abacus.util.function.Runnable runnable, final T result) { N.checkArgNotNull(runnable); return new com.landawn.abacus.util.function.Callable() { @Override public T call() { runnable.run(); return result; } }; } /** * * @param * @param callable * @return */ public static com.landawn.abacus.util.function.Runnable toRunnable(final com.landawn.abacus.util.function.Callable callable) { N.checkArgNotNull(callable); return new com.landawn.abacus.util.function.Runnable() { @Override public void run() { callable.call(); } }; } /** * * @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; } /** * Returns a stateful {@code BiFunction}. Don't cache or reuse it. * * @param * @return */ @Beta @SequentialOnly @Stateful public static BiFunction alternated() { return new BiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public MergeResult apply(T t, T u) { return flag.getAndInvert() ? MergeResult.TAKE_FIRST : MergeResult.TAKE_SECOND; } }; } /** * Adds the all. * * @param * @param * @return * @deprecated replaced by {@code BiConsumers#ofAddAll()} */ @Deprecated static > BiConsumer addAll() { return BiConsumers. ofAddAll(); } /** * * @param the key type * @param the value type * @param * @return * @deprecated replaced by {@code BiConsumers#ofPutAll()} */ @Deprecated static > BiConsumer putAll() { return BiConsumers. ofPutAll(); } /** * The Class Factory. */ public static abstract class Factory { /** The Constant BOOLEAN_ARRAY. */ private static final IntFunction BOOLEAN_ARRAY = new IntFunction() { @Override public boolean[] apply(int len) { return new boolean[len]; } }; /** The Constant CHAR_ARRAY. */ private static final IntFunction CHAR_ARRAY = new IntFunction() { @Override public char[] apply(int len) { return new char[len]; } }; /** The Constant BYTE_ARRAY. */ private static final IntFunction BYTE_ARRAY = new IntFunction() { @Override public byte[] apply(int len) { return new byte[len]; } }; /** The Constant SHORT_ARRAY. */ private static final IntFunction SHORT_ARRAY = new IntFunction() { @Override public short[] apply(int len) { return new short[len]; } }; /** The Constant INT_ARRAY. */ private static final IntFunction INT_ARRAY = new IntFunction() { @Override public int[] apply(int len) { return new int[len]; } }; /** The Constant LONG_ARRAY. */ private static final IntFunction LONG_ARRAY = new IntFunction() { @Override public long[] apply(int len) { return new long[len]; } }; /** The Constant FLOAT_ARRAY. */ private static final IntFunction FLOAT_ARRAY = new IntFunction() { @Override public float[] apply(int len) { return new float[len]; } }; /** The Constant DOUBLE_ARRAY. */ private static final IntFunction DOUBLE_ARRAY = new IntFunction() { @Override public double[] apply(int len) { return new double[len]; } }; /** The Constant STRING_ARRAY. */ private static final IntFunction STRING_ARRAY = new IntFunction() { @Override public String[] apply(int len) { return new String[len]; } }; /** The Constant OBJECT_ARRAY. */ private static final IntFunction OBJECT_ARRAY = new IntFunction() { @Override public Object[] apply(int len) { return new Object[len]; } }; /** The Constant LIST_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LIST_FACTORY = new IntFunction() { @Override public List apply(int len) { return new ArrayList<>(len); } }; /** The Constant LINKED_LIST_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_LIST_FACTORY = new IntFunction() { @Override public LinkedList apply(int len) { return new LinkedList<>(); } }; /** The Constant SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction SET_FACTORY = new IntFunction() { @Override public Set apply(int len) { return N.newHashSet(N.initHashCapacity(len)); } }; /** The Constant LINKED_HASH_SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_SET_FACTORY = new IntFunction() { @Override public Set apply(int len) { return N.newLinkedHashSet(N.initHashCapacity(len)); } }; /** The Constant TREE_SET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction TREE_SET_FACTORY = new IntFunction() { @Override public TreeSet apply(int len) { return new TreeSet<>(); } }; /** The Constant QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction QUEUE_FACTORY = new IntFunction() { @Override public Queue apply(int len) { return new LinkedList(); } }; /** The Constant DEQUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction DEQUE_FACTORY = new IntFunction() { @Override public Deque apply(int len) { return new LinkedList(); } }; /** The Constant ARRAY_DEQUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction ARRAY_DEQUE_FACTORY = new IntFunction() { @Override public ArrayDeque apply(int len) { return new ArrayDeque(len); } }; /** The Constant LINKED_BLOCKING_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_BLOCKING_QUEUE_FACTORY = new IntFunction() { @Override public LinkedBlockingQueue apply(int len) { return new LinkedBlockingQueue(len); } }; /** The Constant CONCURRENT_LINKED_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_LINKED_QUEUE_FACTORY = new IntFunction() { @Override public ConcurrentLinkedQueue apply(int len) { return new ConcurrentLinkedQueue(); } }; /** The Constant PRIORITY_QUEUE_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction PRIORITY_QUEUE_FACTORY = new IntFunction() { @Override public PriorityQueue apply(int len) { return new PriorityQueue(len); } }; /** The Constant MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction MAP_FACTORY = new IntFunction() { @Override public Map apply(int len) { return N.newHashMap(N.initHashCapacity(len)); } }; /** The Constant LINKED_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_MAP_FACTORY = new IntFunction() { @Override public Map apply(int len) { return N.newLinkedHashMap(N.initHashCapacity(len)); } }; /** The Constant IDENTITY_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction IDENTITY_HASH_MAP_FACTORY = new IntFunction() { @Override public IdentityHashMap apply(int len) { return new IdentityHashMap<>(N.initHashCapacity(len)); } }; /** The Constant TREE_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction TREE_MAP_FACTORY = new IntFunction() { @Override public TreeMap apply(int len) { return new TreeMap<>(); } }; /** The Constant CONCURRENT_HASH_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_HASH_MAP_FACTORY = new IntFunction() { @Override public ConcurrentHashMap apply(int len) { return new ConcurrentHashMap(N.initHashCapacity(len)); } }; /** The Constant BI_MAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction BI_MAP_FACTORY = new IntFunction() { @Override public BiMap apply(int len) { return new BiMap(N.initHashCapacity(len)); } }; /** The Constant MULTISET_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction MULTISET_FACTORY = new IntFunction() { @Override public Multiset apply(int len) { return new Multiset(N.initHashCapacity(len)); } }; /** The Constant LIST_MULTIMAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction LIST_MULTIMAP_FACTORY = new IntFunction() { @Override public ListMultimap apply(int len) { return new ListMultimap(N.initHashCapacity(len)); } }; /** The Constant SET_MULTIMAP_FACTORY. */ @SuppressWarnings("rawtypes") private static final IntFunction SET_MULTIMAP_FACTORY = new IntFunction() { @Override public SetMultimap apply(int len) { return new SetMultimap(N.initHashCapacity(len)); } }; /** * Instantiates a new factory. */ protected Factory() { // for extention } /** * 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; } /** * * @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; } /** * Of linked blocking queue. * * @param * @return */ @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedBlockingQueue() { return (IntFunction) LINKED_BLOCKING_QUEUE_FACTORY; } /** * Of concurrent linked queue. * * @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 * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static IntFunction> ofImmutableList() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static IntFunction> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static IntFunction> ofImmutableMap() { throw new UnsupportedOperationException(); } /** * * @param * @param * @param supplier * @return */ @Deprecated public static > IntFunction single(final IntFunction supplier) { return new IntFunction() { private C c = null; @Override public C apply(int t) { if (c == null) { c = supplier.apply(t); } else { c.clear(); } return c; } }; } } /** * The Class IntFunctions. */ public static final class IntFunctions extends Factory { /** * Instantiates a new int functions. */ protected IntFunctions() { // for extention. } } /** * The Class Suppliers. */ public static final class Suppliers { /** The Constant UUID. */ private static final Supplier UUID = new Supplier() { @Override public String get() { return N.uuid(); } }; /** The Constant GUID. */ private static final Supplier GUID = new Supplier() { @Override public String get() { return N.guid(); } }; /** The Constant EMPTY_BOOLEAN_ARRAY. */ private static final Supplier EMPTY_BOOLEAN_ARRAY = new Supplier() { @Override public boolean[] get() { return N.EMPTY_BOOLEAN_ARRAY; } }; /** The Constant EMPTY_CHAR_ARRAY. */ private static final Supplier EMPTY_CHAR_ARRAY = new Supplier() { @Override public char[] get() { return N.EMPTY_CHAR_ARRAY; } }; /** The Constant EMPTY_BYTE_ARRAY. */ private static final Supplier EMPTY_BYTE_ARRAY = new Supplier() { @Override public byte[] get() { return N.EMPTY_BYTE_ARRAY; } }; /** The Constant EMPTY_SHORT_ARRAY. */ private static final Supplier EMPTY_SHORT_ARRAY = new Supplier() { @Override public short[] get() { return N.EMPTY_SHORT_ARRAY; } }; /** The Constant EMPTY_INT_ARRAY. */ private static final Supplier EMPTY_INT_ARRAY = new Supplier() { @Override public int[] get() { return N.EMPTY_INT_ARRAY; } }; /** The Constant EMPTY_LONG_ARRAY. */ private static final Supplier EMPTY_LONG_ARRAY = new Supplier() { @Override public long[] get() { return N.EMPTY_LONG_ARRAY; } }; /** The Constant EMPTY_FLOAT_ARRAY. */ private static final Supplier EMPTY_FLOAT_ARRAY = new Supplier() { @Override public float[] get() { return N.EMPTY_FLOAT_ARRAY; } }; /** The Constant EMPTY_DOUBLE_ARRAY. */ private static final Supplier EMPTY_DOUBLE_ARRAY = new Supplier() { @Override public double[] get() { return N.EMPTY_DOUBLE_ARRAY; } }; /** The Constant EMPTY_STRING_ARRAY. */ private static final Supplier EMPTY_STRING_ARRAY = new Supplier() { @Override public String[] get() { return N.EMPTY_STRING_ARRAY; } }; /** The Constant EMPTY_OBJECT_ARRAY. */ private static final Supplier EMPTY_OBJECT_ARRAY = new Supplier() { @Override public Object[] get() { return N.EMPTY_OBJECT_ARRAY; } }; /** The Constant LIST. */ @SuppressWarnings("rawtypes") private static final Supplier LIST = new Supplier() { @Override public List get() { return new ArrayList(); } }; /** The Constant LINKED_LIST. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_LIST = new Supplier() { @Override public LinkedList get() { return new LinkedList(); } }; /** The Constant SET. */ @SuppressWarnings("rawtypes") private static final Supplier SET = new Supplier() { @Override public Set get() { return N.newHashSet(); } }; /** The Constant LINKED_HASH_SET. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_SET = new Supplier() { @Override public Set get() { return N.newLinkedHashSet(); } }; /** The Constant TREE_SET. */ @SuppressWarnings("rawtypes") private static final Supplier TREE_SET = new Supplier() { @Override public TreeSet get() { return new TreeSet(); } }; /** The Constant QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier QUEUE = new Supplier() { @Override public Queue get() { return new LinkedList(); } }; /** The Constant DEQUE. */ @SuppressWarnings("rawtypes") private static final Supplier DEQUE = new Supplier() { @Override public Deque get() { return new LinkedList(); } }; /** The Constant ARRAY_DEQUE. */ @SuppressWarnings("rawtypes") private static final Supplier ARRAY_DEQUE = new Supplier() { @Override public ArrayDeque get() { return new ArrayDeque(); } }; /** The Constant LINKED_BLOCKING_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_BLOCKING_QUEUE = new Supplier() { @Override public LinkedBlockingQueue get() { return new LinkedBlockingQueue(); } }; /** The Constant CONCURRENT_LINKED_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_LINKED_QUEUE = new Supplier() { @Override public ConcurrentLinkedQueue get() { return new ConcurrentLinkedQueue(); } }; /** The Constant PRIORITY_QUEUE. */ @SuppressWarnings("rawtypes") private static final Supplier PRIORITY_QUEUE = new Supplier() { @Override public PriorityQueue get() { return new PriorityQueue(); } }; /** The Constant MAP. */ @SuppressWarnings("rawtypes") private static final Supplier MAP = new Supplier() { @Override public Map get() { return N.newHashMap(); } }; /** The Constant LINKED_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_MAP = new Supplier() { @Override public Map get() { return N.newLinkedHashMap(); } }; /** The Constant IDENTITY_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier IDENTITY_HASH_MAP = new Supplier() { @Override public IdentityHashMap get() { return new IdentityHashMap(); } }; /** The Constant TREE_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier TREE_MAP = new Supplier() { @Override public TreeMap get() { return new TreeMap(); } }; /** The Constant CONCURRENT_HASH_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_HASH_MAP = new Supplier() { @Override public ConcurrentHashMap get() { return new ConcurrentHashMap(); } }; /** The Constant BI_MAP. */ @SuppressWarnings("rawtypes") private static final Supplier BI_MAP = new Supplier() { @Override public BiMap get() { return new BiMap(); } }; /** The Constant MULTISET. */ @SuppressWarnings("rawtypes") private static final Supplier MULTISET = new Supplier() { @Override public Multiset get() { return new Multiset(); } }; /** The Constant LIST_MULTIMAP. */ @SuppressWarnings("rawtypes") private static final Supplier LIST_MULTIMAP = new Supplier() { @Override public ListMultimap get() { return N.newListMultimap(); } }; /** The Constant SET_MULTIMAP. */ @SuppressWarnings("rawtypes") private static final Supplier SET_MULTIMAP = new Supplier() { @Override public SetMultimap get() { return N.newSetMultimap(); } }; /** The Constant STRING_BUILDER. */ private static final Supplier STRING_BUILDER = new Supplier() { @Override public StringBuilder get() { return new StringBuilder(); } }; /** * Instantiates a new suppliers. */ protected Suppliers() { // for extention. } /** * Returns a supplier that always supplies {@code instance}. * * @param * @param instance * @return */ public static Supplier ofInstance(final T instance) { return new Supplier() { @Override public T get() { return instance; } }; } /** * * @return */ public static Supplier ofUUID() { return UUID; } /** * * @return */ 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; } /** * * @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; } /** * 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; } /** * Of concurrent hash map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentHashMap() { return (Supplier) CONCURRENT_HASH_MAP; } /** * Of bi map. * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofBiMap() { return (Supplier) BI_MAP; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofMultiset() { return (Supplier) MULTISET; } /** * Of list multimap. * * @param the key type * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofListMultimap() { return (Supplier) LIST_MULTIMAP; } /** * Of set multimap. * * @param the key type * @param * @return */ @SuppressWarnings("rawtypes") public static Supplier> ofSetMultimap() { return (Supplier) SET_MULTIMAP; } /** * Of string builder. * * @return */ public static Supplier ofStringBuilder() { return STRING_BUILDER; } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static Supplier> ofImmutableList() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static Supplier> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * * @return * @throws UnsupportedOperationException the unsupported operation exception */ @Deprecated public static Supplier> ofImmutableMap() { throw new UnsupportedOperationException(); } /** * * @param * @param * @param supplier * @return */ @Deprecated public static > Supplier single(final Supplier supplier) { return new Supplier() { private C c = null; @Override public C get() { if (c == null) { c = supplier.get(); } else { c.clear(); } return c; } }; } } /** * The Class Predicates. */ public static final class Predicates { /** * Instantiates a new predicates. */ protected Predicates() { // for extention. } /** * Returns a stateful Predicate. Don't save it or use it in parallel stream. * * @param * @param predicate * @return */ @Beta @SequentialOnly @Stateful public static Predicate indexed(final IndexedPredicate predicate) { N.checkArgNotNull(predicate); return new Predicate() { private final MutableInt idx = new MutableInt(0); @Override public boolean test(T t) { return predicate.test(idx.getAndIncrement(), t); } }; } /** * Returns a stateful Predicate. Don't save it 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(T value) { return set.add(value); } }; } /** * Returns a stateful Predicate. Don't save it or use it in parallel stream. * * @param * @param mapper * @return */ @Beta @SequentialOnly @Stateful public static Predicate distinctBy(final Function mapper) { return new Predicate() { private final Set set = N.newHashSet(); @Override public boolean test(T value) { return set.add(mapper.apply(value)); } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @return */ @Beta @Stateful public static Predicate concurrentDistinct() { return new Predicate() { private final Map map = new ConcurrentHashMap<>(); @Override public boolean test(T value) { return map.put(value, NONE) == null; } }; } /** * Returns a stateful Predicate. Don't save it. * * @param * @param mapper * @return */ @Beta @Stateful public static Predicate concurrentDistinctBy(final Function mapper) { return new Predicate() { private final Map map = new ConcurrentHashMap<>(); @Override public boolean test(T value) { return map.put(mapper.apply(value), NONE) == null; } }; } /** * Returns a stateful Predicate. Don't save it 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(T value) { boolean res = pre == NONE || N.equals(value, pre) == false; pre = value; return res; } }; } } /** * The Class BiPredicates. */ public static final class BiPredicates { /** The Constant ALWAYS_TRUE. */ @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_TRUE = new BiPredicate() { @Override public boolean test(Object t, Object u) { return true; } }; /** The Constant ALWAYS_FALSE. */ @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_FALSE = new BiPredicate() { @Override public boolean test(Object t, Object u) { return false; } }; /** The Constant EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate EQUAL = new BiPredicate() { @Override public boolean test(Object t, Object u) { return N.equals(t, u); } }; /** The Constant NOT_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate NOT_EQUAL = new BiPredicate() { @Override public boolean test(Object t, Object u) { return !N.equals(t, u); } }; /** The Constant GREATER_THAN. */ @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_THAN = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) > 0; } }; /** The Constant GREATER_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_EQUAL = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) >= 0; } }; /** The Constant LESS_THAN. */ @SuppressWarnings("rawtypes") private static final BiPredicate LESS_THAN = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) < 0; } }; /** The Constant LESS_EQUAL. */ @SuppressWarnings("rawtypes") private static final BiPredicate LESS_EQUAL = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) <= 0; } }; /** * Instantiates a new bi predicates. */ protected BiPredicates() { // for extention. } /** * * @param * @param * @return */ public static BiPredicate alwaysTrue() { return ALWAYS_TRUE; } /** * * @param * @param * @return */ public static BiPredicate alwaysFalse() { return ALWAYS_FALSE; } /** * Returns a stateful BiPredicate. Don't save it or use it in parallel stream. * * * @param * @param * @param predicate * @return */ @Beta @SequentialOnly @Stateful public static BiPredicate indexed(final IndexedBiPredicate predicate) { N.checkArgNotNull(predicate); return new BiPredicate() { private final MutableInt idx = new MutableInt(0); @Override public boolean test(T t, U u) { return predicate.test(t, idx.getAndIncrement(), u); } }; } } /** * The Class TriPredicates. */ public static final class TriPredicates { /** The Constant ALWAYS_TRUE. */ @SuppressWarnings("rawtypes") private static final TriPredicate ALWAYS_TRUE = new TriPredicate() { @Override public boolean test(Object a, Object b, Object c) { return true; } }; /** The Constant ALWAYS_FALSE. */ @SuppressWarnings("rawtypes") private static final TriPredicate ALWAYS_FALSE = new TriPredicate() { @Override public boolean test(Object a, Object b, Object c) { return false; } }; /** * Instantiates a new tri predicates. */ protected TriPredicates() { // for extention. } /** * * @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 { /** * Instantiates a new consumers. */ protected Consumers() { // for extention. } /** * Returns a Consumer which calls the specified func. * * @param * @param func * @return */ public static Consumer convert(final Function func) { N.checkArgNotNull(func); return new Consumer() { @Override public void accept(T t) { func.apply(t); } }; } /** * Returns a stateful BiPredicate. Don't save it or use it in parallel stream. * * @param * @param action * @return */ @Beta @SequentialOnly @Stateful public static Consumer indexed(final IndexedConsumer action) { N.checkArgNotNull(action); return new Consumer() { private final MutableInt idx = new MutableInt(0); @Override public void accept(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 = new BiConsumer() { @Override public void accept(Object t, Object u) { // do nothing. } }; /** The Constant ADD. */ private static final BiConsumer, Object> ADD = new BiConsumer, Object>() { @Override public void accept(Collection t, Object u) { t.add(u); } }; /** The Constant ADD_ALL. */ private static final BiConsumer, Collection> ADD_ALL = new BiConsumer, Collection>() { @Override public void accept(Collection t, Collection u) { t.addAll(u); } }; /** The Constant REMOVE. */ private static final BiConsumer, Object> REMOVE = new BiConsumer, Object>() { @Override public void accept(Collection t, Object u) { t.remove(u); } }; /** The Constant REMOVE_ALL. */ private static final BiConsumer, Collection> REMOVE_ALL = new BiConsumer, Collection>() { @Override public void accept(Collection t, Collection u) { t.removeAll(u); } }; /** The Constant PUT. */ private static final BiConsumer, Map.Entry> PUT = new BiConsumer, Map.Entry>() { @Override public void accept(Map t, Map.Entry u) { t.put(u.getKey(), u.getValue()); } }; /** The Constant PUT_ALL. */ private static final BiConsumer, Map> PUT_ALL = new BiConsumer, Map>() { @Override public void accept(Map t, Map u) { t.putAll(u); } }; /** The Constant REMOVE_BY_KEY. */ private static final BiConsumer, Object> REMOVE_BY_KEY = new BiConsumer, Object>() { @Override public void accept(Map t, Object u) { t.remove(u); } }; /** The Constant MERGE. */ private static final BiConsumer MERGE = new BiConsumer() { @Override public void accept(Joiner t, Joiner u) { t.merge(u); } }; /** The Constant APPEND. */ private static final BiConsumer APPEND = new BiConsumer() { @Override public void accept(StringBuilder t, Object u) { t.append(u); } }; /** * Instantiates a new bi consumers. */ protected BiConsumers() { // for extention. } /** * * @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; } /** * * @param * @param * @return */ public static > BiConsumer ofRemove() { return (BiConsumer) REMOVE; } /** * Of remove all. * * @param * @param * @return */ public static > BiConsumer ofRemoveAll() { return (BiConsumer) REMOVE_ALL; } /** * * @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; } /** * * @return */ public static BiConsumer ofMerge() { return MERGE; } /** * * @param * @return */ public static BiConsumer ofAppend() { return (BiConsumer) APPEND; } /** * Returns a BiConsumer which calls the specified func. * * @param * @param * @param func * @return */ public static BiConsumer convert(final BiFunction func) { N.checkArgNotNull(func); return new BiConsumer() { @Override public void accept(T t, U u) { func.apply(t, u); } }; } /** * Returns a stateful BiPredicate. Don't save it or use it in parallel stream. * * * @param * @param * @param action * @return */ @Beta @SequentialOnly @Stateful public static BiConsumer indexed(final IndexedBiConsumer action) { N.checkArgNotNull(action); return new BiConsumer() { private final MutableInt idx = new MutableInt(0); @Override public void accept(U u, T t) { action.accept(u, idx.getAndIncrement(), t); } }; } } /** * The Class TriConsumers. */ public static final class TriConsumers { /** * Instantiates a new tri consumers. */ protected TriConsumers() { // for extention. } /** * Returns a TriConsumer which calls the specified func. * * @param * @param * @param * @param func * @return */ public static TriConsumer convert(final TriFunction func) { N.checkArgNotNull(func); return new TriConsumer() { @Override public void accept(A a, B b, C c) { func.apply(a, b, c); } }; } } /** * The Class Functions. */ public static final class Functions { /** * Instantiates a new functions. */ protected Functions() { // for extention. } /** * * @param * @param action * @return */ public static Function convert(final Consumer action) { N.checkArgNotNull(action); return new Function() { @Override public Void apply(T t) { action.accept(t); return null; } }; } /** * Returns a stateful Function. Don't save it or use it in parallel stream. * * * @param * @param * @param func * @return */ @Beta @SequentialOnly @Stateful public static Function indexed(final IndexedFunction func) { N.checkArgNotNull(func); return new Function() { private final MutableInt idx = new MutableInt(0); @Override public R apply(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 = new BiFunction() { @Override public Object apply(Object t, Object u) { return t; } }; /** The Constant RETURN_SECOND. */ private static final BiFunction RETURN_SECOND = new BiFunction() { @Override public Object apply(Object t, Object u) { return u; } }; /** The Constant ADD. */ private static final BiFunction, Object, Collection> ADD = new BiFunction, Object, Collection>() { @Override public Collection apply(Collection t, Object u) { t.add(u); return t; } }; /** The Constant ADD_ALL. */ private static final BiFunction, Collection, Collection> ADD_ALL = new BiFunction, Collection, Collection>() { @Override public Collection apply(Collection t, Collection u) { t.addAll(u); return t; } }; /** The Constant REMOVE. */ private static final BiFunction, Object, Collection> REMOVE = new BiFunction, Object, Collection>() { @Override public Collection apply(Collection t, Object u) { t.remove(u); return t; } }; /** The Constant REMOVE_ALL. */ private static final BiFunction, Collection, Collection> REMOVE_ALL = new BiFunction, Collection, Collection>() { @Override public Collection apply(Collection t, Collection u) { t.removeAll(u); return t; } }; /** The Constant PUT. */ private static final BiFunction, Map.Entry, Map> PUT = new BiFunction, Map.Entry, Map>() { @Override public Map apply(Map t, Map.Entry u) { t.put(u.getKey(), u.getValue()); return t; } }; /** The Constant PUT_ALL. */ private static final BiFunction, Map, Map> PUT_ALL = new BiFunction, Map, Map>() { @Override public Map apply(Map t, Map u) { t.putAll(u); return t; } }; /** The Constant REMOVE_BY_KEY. */ private static final BiFunction, Object, Map> REMOVE_BY_KEY = new BiFunction, Object, Map>() { @Override public Map apply(Map t, Object u) { t.remove(u); return t; } }; /** The Constant MERGE. */ private static final BiFunction MERGE = new BiFunction() { @Override public Joiner apply(Joiner t, Joiner u) { return t.merge(u); } }; /** The Constant APPEND. */ private static final BiFunction APPEND = new BiFunction() { @Override public StringBuilder apply(StringBuilder t, Object u) { return t.append(u); } }; /** * Instantiates a new bi functions. */ protected BiFunctions() { // for extention. } /** * * @param * @param * @return */ public static BiFunction returnFirst() { return (BiFunction) RETURN_FIRST; } /** * * @param * @param * @return */ public static BiFunction returnSecond() { 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; } /** * * @param * @param * @return */ public static > BiFunction ofRemove() { return (BiFunction) REMOVE; } /** * Of remove all. * * @param * @param * @return */ public static > BiFunction ofRemoveAll() { return (BiFunction) REMOVE_ALL; } /** * * @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 * @param * @return */ public static , U> BiFunction ofRemoveByKey() { return (BiFunction) REMOVE_BY_KEY; } /** * * @return */ public static BiFunction ofMerge() { return MERGE; } /** * * @param * @return */ public static BiFunction ofAppend() { return (BiFunction) APPEND; } /** * * @param * @param * @param action * @return */ public static BiFunction convert(final BiConsumer action) { N.checkArgNotNull(action); return new BiFunction() { @Override public Void apply(T t, U u) { action.accept(t, u); return null; } }; } /** * Returns a stateful BiPredicate. Don't save it or use it in parallel stream. * * @param * @param * @param * @param func * @return */ @Beta @SequentialOnly @Stateful public static BiFunction indexed(final IndexedBiFunction func) { N.checkArgNotNull(func); return new BiFunction() { private final MutableInt idx = new MutableInt(0); @Override public R apply(U u, T t) { return func.apply(u, idx.getAndIncrement(), t); } }; } } /** * The Class TriFunctions. */ public static final class TriFunctions { /** * Instantiates a new tri functions. */ protected TriFunctions() { // for extention. } /** * * @param * @param * @param * @param action * @return */ public static TriFunction convert(final TriConsumer action) { N.checkArgNotNull(action); return new TriFunction() { @Override public Void apply(A a, B b, C c) { action.accept(a, b, c); return null; } }; } } /** * The Class BinaryOperators. */ public static final class BinaryOperators { /** The Constant THROWING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator THROWING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { throw new DuplicatedResultException(String.format("Duplicate key (attempted merging values %s and %s)", t, u)); } }; /** The Constant IGNORING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator IGNORING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { return t; } }; /** The Constant REPLACING_MERGER. */ @SuppressWarnings("rawtypes") private static final BinaryOperator REPLACING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { return u; } }; /** The Constant ADD_ALL_TO_FIRST. */ private static final BinaryOperator> ADD_ALL_TO_FIRST = new BinaryOperator>() { @Override public Collection apply(Collection t, Collection u) { t.addAll(u); return t; } }; /** The Constant ADD_ALL_TO_BIGGER. */ private static final BinaryOperator> ADD_ALL_TO_BIGGER = new BinaryOperator>() { @Override public Collection apply(Collection t, Collection 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 = new BinaryOperator>() { @Override public Collection apply(Collection t, Collection u) { t.removeAll(u); return t; } }; /** The Constant PUT_ALL_TO_FIRST. */ private static final BinaryOperator> PUT_ALL_TO_FIRST = new BinaryOperator>() { @Override public Map apply(Map t, Map u) { t.putAll(u); return t; } }; /** The Constant PUT_ALL_TO_BIGGER. */ private static final BinaryOperator> PUT_ALL_TO_BIGGER = new BinaryOperator>() { @Override public Map apply(Map t, Map 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 = new BinaryOperator() { @Override public Joiner apply(Joiner t, Joiner u) { return t.merge(u); } }; /** The Constant MERGE_TO_BIGGER. */ private static final BinaryOperator MERGE_TO_BIGGER = new BinaryOperator() { @Override public Joiner apply(Joiner t, Joiner 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 = new BinaryOperator() { @Override public StringBuilder apply(StringBuilder t, StringBuilder u) { return t.append(u); } }; /** The Constant APPEND_TO_BIGGER. */ private static final BinaryOperator APPEND_TO_BIGGER = new BinaryOperator() { @Override public StringBuilder apply(StringBuilder t, StringBuilder u) { if (t.length() >= u.length()) { return t.append(u); } else { return u.append(t); } } }; /** The Constant CONCAT. */ private static final BinaryOperator CONCAT = new BinaryOperator() { @Override public String apply(String t, String u) { return t + u; } }; /** The Constant ADD_INTEGER. */ private static final BinaryOperator ADD_INTEGER = new BinaryOperator() { @Override public Integer apply(Integer t, Integer u) { return t.intValue() + u.intValue(); } }; /** The Constant ADD_LONG. */ private static final BinaryOperator ADD_LONG = new BinaryOperator() { @Override public Long apply(Long t, Long u) { return t.longValue() + u.longValue(); } }; /** The Constant ADD_DOUBLE. */ private static final BinaryOperator ADD_DOUBLE = new BinaryOperator() { @Override public Double apply(Double t, Double u) { return t.doubleValue() + u.doubleValue(); } }; /** The Constant ADD_BIG_INTEGER. */ private static final BinaryOperator ADD_BIG_INTEGER = new BinaryOperator() { @Override public BigInteger apply(BigInteger t, BigInteger u) { return t.add(u); } }; /** The Constant ADD_BIG_DECIMAL. */ private static final BinaryOperator ADD_BIG_DECIMAL = new BinaryOperator() { @Override public BigDecimal apply(BigDecimal t, BigDecimal u) { return t.add(u); } }; /** The Constant MIN. */ @SuppressWarnings({ "rawtypes" }) private static final BinaryOperator MIN = new BinaryOperator() { @Override public Comparable apply(Comparable t, Comparable u) { return N.compare(t, u) <= 0 ? t : u; } }; /** The Constant MAX. */ @SuppressWarnings("rawtypes") private static final BinaryOperator MAX = new BinaryOperator() { @Override public Comparable apply(Comparable t, Comparable u) { return N.compare(t, u) >= 0 ? t : u; } }; /** * Instantiates a new binary operators. */ protected BinaryOperators() { // for extention. } /** * 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; } /** * * @return */ 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; } /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator min() { return (BinaryOperator) MIN; } /** * * @param * @param comparator * @return */ public static BinaryOperator min(final Comparator comparator) { N.checkArgNotNull(comparator); return new BinaryOperator() { @Override public T apply(T t, T u) { return comparator.compare(t, u) <= 0 ? t : u; } }; } /** * * @param * @param comparator * @return */ @SuppressWarnings("rawtypes") public static BinaryOperator minBy(final Function keyMapper) { N.checkArgNotNull(keyMapper); return new BinaryOperator() { @Override public T apply(T t, T u) { return N.compare(keyMapper.apply(t), keyMapper.apply(u)) <= 0 ? t : u; } }; } /** The Constant MIN_BY_KEY. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MIN_BY_KEY = new BinaryOperator>() { @Override public Map.Entry apply(Map.Entry t, Map.Entry u) { return N.compare(t.getKey(), u.getKey()) <= 0 ? t : u; } }; /** * * @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>() { @Override public Map.Entry apply(Map.Entry t, Map.Entry u) { return N.compare(t.getValue(), u.getValue()) <= 0 ? t : u; } }; /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator> minByValue() { return (BinaryOperator) MIN_BY_VALUE; } /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator max() { return (BinaryOperator) MAX; } /** * * @param * @param comparator * @return */ public static BinaryOperator max(final Comparator comparator) { N.checkArgNotNull(comparator); return new BinaryOperator() { @Override public T apply(T t, T u) { return comparator.compare(t, u) >= 0 ? t : u; } }; } /** * * @param * @param comparator * @return */ @SuppressWarnings("rawtypes") public static BinaryOperator maxBy(final Function keyMapper) { N.checkArgNotNull(keyMapper); return new BinaryOperator() { @Override public T apply(T t, T u) { return N.compare(keyMapper.apply(t), keyMapper.apply(u)) >= 0 ? t : u; } }; } /** The Constant MAX_BY_KEY. */ @SuppressWarnings("rawtypes") private static final BinaryOperator> MAX_BY_KEY = new BinaryOperator>() { @Override public Map.Entry apply(Map.Entry t, Map.Entry u) { return N.compare(t.getKey(), u.getKey()) >= 0 ? t : u; } }; /** * * @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>() { @Override public Map.Entry apply(Map.Entry t, Map.Entry u) { return N.compare(t.getValue(), u.getValue()) >= 0 ? t : u; } }; /** * * @param * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator> maxByValue() { return (BinaryOperator) MAX_BY_VALUE; } } /** * The Class UnaryOperators. */ public static final class UnaryOperators { /** The Constant IDENTITY. */ @SuppressWarnings("rawtypes") private static final UnaryOperator IDENTITY = new UnaryOperator() { @Override public Object apply(Object t) { return t; } }; /** * Instantiates a new unary operators. */ protected UnaryOperators() { // for extention. } /** * * @param * @return */ public static UnaryOperator identity() { return IDENTITY; } } /** * The Class Entries. */ public static final class Entries { /** * Instantiates a new entries. */ protected Entries() { // for extention. } /** * * @param the key type * @param the value type * @param * @param f * @return */ public static Function, T> f(final BiFunction f) { N.checkArgNotNull(f, "BiFunction"); return new Function, T>() { @Override public T apply(Entry e) { return f.apply(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param p * @return */ public static Predicate> p(final BiPredicate p) { N.checkArgNotNull(p, "BiPredicate"); return new Predicate>() { @Override public boolean test(Entry e) { return p.test(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param c * @return */ public static Consumer> c(final BiConsumer c) { N.checkArgNotNull(c, "BiConsumer"); return new Consumer>() { @Override public void accept(Entry e) { c.accept(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param * @param * @param f * @return */ @Beta public static Throwables.Function, T, E> ef( final Throwables.BiFunction f) { N.checkArgNotNull(f, "BiFunction"); return new Throwables.Function, T, E>() { @Override public T apply(Entry e) throws E { return f.apply(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param * @param p * @return */ @Beta public static Throwables.Predicate, E> ep(final Throwables.BiPredicate p) { N.checkArgNotNull(p, "BiPredicate"); return new Throwables.Predicate, E>() { @Override public boolean test(Entry e) throws E { return p.test(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param * @param c * @return */ @Beta public static Throwables.Consumer, E> ec(final Throwables.BiConsumer c) { N.checkArgNotNull(c, "BiConsumer"); return new Throwables.Consumer, E>() { @Override public void accept(Entry e) throws E { c.accept(e.getKey(), e.getValue()); } }; } /** * * @param the key type * @param the value type * @param * @param * @param f * @return */ public static Function, T> ff(final Throwables.BiFunction f) { N.checkArgNotNull(f, "BiFunction"); return new Function, T>() { @Override public T apply(Entry e) { try { return f.apply(e.getKey(), e.getValue()); } catch (Exception ex) { throw N.toRuntimeException(ex); } } }; } /** * * @param the key type * @param the value type * @param * @param p * @return */ public static Predicate> pp(final Throwables.BiPredicate p) { N.checkArgNotNull(p, "BiPredicate"); return new Predicate>() { @Override public boolean test(Entry e) { try { return p.test(e.getKey(), e.getValue()); } catch (Exception ex) { throw N.toRuntimeException(ex); } } }; } /** * * @param the key type * @param the value type * @param * @param c * @return */ public static Consumer> cc(final Throwables.BiConsumer c) { N.checkArgNotNull(c, "BiConsumer"); return new Consumer>() { @Override public void accept(Entry e) { try { c.accept(e.getKey(), e.getValue()); } catch (Exception ex) { throw N.toRuntimeException(ex); } } }; } } /** * The Class Pairs. */ public static final class Pairs { /** The Constant PAIR_TO_LIST. */ @SuppressWarnings("rawtypes") private static final Function PAIR_TO_LIST = new Function() { @Override public List apply(Pair t) { return N.asList(t.left, t.right); } }; /** The Constant PAIR_TO_SET. */ @SuppressWarnings("rawtypes") private static final Function PAIR_TO_SET = new Function() { @Override public Set apply(Pair t) { return N.asSet(t.left, t.right); } }; /** * Instantiates a new pairs. */ protected Pairs() { // for extention. } /** * * @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 = new Function() { @Override public List apply(Triple t) { return N.asList(t.left, t.middle, t.right); } }; /** The Constant TRIPLE_TO_SET. */ @SuppressWarnings("rawtypes") private static final Function TRIPLE_TO_SET = new Function() { @Override public Set apply(Triple t) { return N.asSet(t.left, t.middle, t.right); } }; /** * Instantiates a new triples. */ protected Triples() { // for extention. } /** * * @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 = new Function() { @Override public Object[] apply(DisposableArray t) { return t.clone(); } }; /** The Constant TO_STRING. */ @SuppressWarnings("rawtypes") private static final Function TO_STRING = new Function() { @Override public String apply(DisposableArray t) { return t.toString(); } }; /** * Instantiates a new disposables. */ private Disposables() { // singleton. } /** * * @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 new Function() { @Override public String apply(A t) { return t.join(delimiter); } }; } } /** * Utility class for {@code CharPredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnC { /** The Constant IS_ZERO. */ private static final CharPredicate IS_ZERO = new CharPredicate() { @Override public boolean test(char t) { return t == 0; } }; /** The Constant IS_WHITE_SPACE. */ private static final CharPredicate IS_WHITESPACE = new CharPredicate() { @Override public boolean test(char t) { return Character.isWhitespace(t); } }; /** The Constant EQUAL. */ private static final CharBiPredicate EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final CharBiPredicate NOT_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final CharBiPredicate GREATER_THAN = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final CharBiPredicate GREATER_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final CharBiPredicate LESS_THAN = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final CharBiPredicate LESS_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(char[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn C. */ protected FnC() { // for extention. } /** * * @return */ public static CharPredicate isZero() { return IS_ZERO; } /** * * @return */ public static CharPredicate isWhitespace() { return IS_WHITESPACE; } /** * * @return */ public static CharBiPredicate equal() { return EQUAL; } /** * * @return */ public static CharBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static CharBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static CharBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static CharBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static CharBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @param p * @return */ public static CharPredicate p(final CharPredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static CharFunction f(final CharFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static CharConsumer c(final CharConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } } /** * Utility class for {@code BytePredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnB { /** The Constant POSITIVE. */ private static final BytePredicate POSITIVE = new BytePredicate() { @Override public boolean test(byte t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final BytePredicate NOT_NEGATIVE = new BytePredicate() { @Override public boolean test(byte t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final ByteBiPredicate EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final ByteBiPredicate NOT_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final ByteBiPredicate GREATER_THAN = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final ByteBiPredicate GREATER_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final ByteBiPredicate LESS_THAN = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final ByteBiPredicate LESS_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(byte[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn B. */ protected FnB() { // for extention. } /** * * @return */ public static BytePredicate positve() { return POSITIVE; } /** * * @return */ public static BytePredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static ByteBiPredicate equal() { return EQUAL; } /** * * @return */ public static ByteBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static ByteBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static ByteBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static ByteBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static ByteBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @param p * @return */ public static BytePredicate p(final BytePredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static ByteFunction f(final ByteFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static ByteConsumer c(final ByteConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Integer apply(byte[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(byte[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for {@code ShortPredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnS { /** The Constant POSITIVE. */ private static final ShortPredicate POSITIVE = new ShortPredicate() { @Override public boolean test(short t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final ShortPredicate NOT_NEGATIVE = new ShortPredicate() { @Override public boolean test(short t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final ShortBiPredicate EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final ShortBiPredicate NOT_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final ShortBiPredicate GREATER_THAN = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final ShortBiPredicate GREATER_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final ShortBiPredicate LESS_THAN = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final ShortBiPredicate LESS_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(short[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn S. */ protected FnS() { // for extention. } /** * * @return */ public static ShortPredicate positve() { return POSITIVE; } /** * * @return */ public static ShortPredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static ShortBiPredicate equal() { return EQUAL; } /** * * @return */ public static ShortBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static ShortBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static ShortBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static ShortBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static ShortBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @param p * @return */ public static ShortPredicate p(final ShortPredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static ShortFunction f(final ShortFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static ShortConsumer c(final ShortConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Integer apply(short[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(short[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for {@code IntPredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnI { /** The Constant POSITIVE. */ private static final IntPredicate POSITIVE = new IntPredicate() { @Override public boolean test(int t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final IntPredicate NOT_NEGATIVE = new IntPredicate() { @Override public boolean test(int t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final IntBiPredicate EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final IntBiPredicate NOT_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final IntBiPredicate GREATER_THAN = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final IntBiPredicate GREATER_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final IntBiPredicate LESS_THAN = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final IntBiPredicate LESS_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(int[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn I. */ protected FnI() { // for extention. } /** * * @return */ public static IntPredicate positve() { return POSITIVE; } /** * * @return */ public static IntPredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static IntBiPredicate equal() { return EQUAL; } /** * * @return */ public static IntBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static IntBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static IntBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static IntBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static IntBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @return */ public static ToIntFunction unbox() { return ToIntFunction.UNBOX; } /** * * @param p * @return */ public static IntPredicate p(final IntPredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static IntFunction f(final IntFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static IntConsumer c(final IntConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Integer apply(int[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(int[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for {@code LongPredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnL { /** The Constant POSITIVE. */ private static final LongPredicate POSITIVE = new LongPredicate() { @Override public boolean test(long t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final LongPredicate NOT_NEGATIVE = new LongPredicate() { @Override public boolean test(long t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final LongBiPredicate EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final LongBiPredicate NOT_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final LongBiPredicate GREATER_THAN = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final LongBiPredicate GREATER_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final LongBiPredicate LESS_THAN = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final LongBiPredicate LESS_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(long[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn L. */ protected FnL() { // for extention. } /** * * @return */ public static LongPredicate positve() { return POSITIVE; } /** * * @return */ public static LongPredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static LongBiPredicate equal() { return EQUAL; } /** * * @return */ public static LongBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static LongBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static LongBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static LongBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static LongBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @return */ public static ToLongFunction unbox() { return ToLongFunction.UNBOX; } /** * * @param p * @return */ public static LongPredicate p(final LongPredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static LongFunction f(final LongFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static LongConsumer c(final LongConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Long apply(long[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(long[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for {@code FloatPredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnF { /** The Constant POSITIVE. */ private static final FloatPredicate POSITIVE = new FloatPredicate() { @Override public boolean test(float t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final FloatPredicate NOT_NEGATIVE = new FloatPredicate() { @Override public boolean test(float t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final FloatBiPredicate EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t == u; } }; /** The Constant NOT_EQUAL. */ private static final FloatBiPredicate NOT_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t != u; } }; /** The Constant GREATER_THAN. */ private static final FloatBiPredicate GREATER_THAN = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t > u; } }; /** The Constant GREATER_EQUAL. */ private static final FloatBiPredicate GREATER_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t >= u; } }; /** The Constant LESS_THAN. */ private static final FloatBiPredicate LESS_THAN = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t < u; } }; /** The Constant LESS_EQUAL. */ private static final FloatBiPredicate LESS_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t <= u; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(float[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn F. */ protected FnF() { // for extention. } /** * * @return */ public static FloatPredicate positve() { return POSITIVE; } /** * * @return */ public static FloatPredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static FloatBiPredicate equal() { return EQUAL; } /** * * @return */ public static FloatBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static FloatBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static FloatBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static FloatBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static FloatBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @param p * @return */ public static FloatPredicate p(final FloatPredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static FloatFunction f(final FloatFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static FloatConsumer c(final FloatConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Float apply(float[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(float[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for {@code DoublePredicate/Function/Consumer}. * * @author haiyangl * */ public static final class FnD { /** The Constant POSITIVE. */ private static final DoublePredicate POSITIVE = new DoublePredicate() { @Override public boolean test(double t) { return t > 0; } }; /** The Constant NOT_NEGATIVE. */ private static final DoublePredicate NOT_NEGATIVE = new DoublePredicate() { @Override public boolean test(double t) { return t >= 0; } }; /** The Constant EQUAL. */ private static final DoubleBiPredicate EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.equals(t, u); } }; /** The Constant NOT_EQUAL. */ private static final DoubleBiPredicate NOT_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) != 0; } }; /** The Constant GREATER_THAN. */ private static final DoubleBiPredicate GREATER_THAN = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) > 0; } }; /** The Constant GREATER_EQUAL. */ private static final DoubleBiPredicate GREATER_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) >= 0; } }; /** The Constant LESS_THAN. */ private static final DoubleBiPredicate LESS_THAN = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) < 0; } }; /** The Constant LESS_EQUAL. */ private static final DoubleBiPredicate LESS_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) <= 0; } }; /** The Constant LEN. */ private static final Function LEN = new Function() { @Override public Integer apply(double[] t) { return t == null ? 0 : t.length; } }; /** * Instantiates a new fn D. */ protected FnD() { // for extention. } /** * * @return */ public static DoublePredicate positve() { return POSITIVE; } /** * * @return */ public static DoublePredicate notNegative() { return NOT_NEGATIVE; } /** * * @return */ public static DoubleBiPredicate equal() { return EQUAL; } /** * * @return */ public static DoubleBiPredicate notEqual() { return NOT_EQUAL; } /** * * @return */ public static DoubleBiPredicate greaterThan() { return GREATER_THAN; } /** * * @return */ public static DoubleBiPredicate greaterEqual() { return GREATER_EQUAL; } /** * * @return */ public static DoubleBiPredicate lessThan() { return LESS_THAN; } /** * * @return */ public static DoubleBiPredicate lessEqual() { return LESS_EQUAL; } /** * * @return */ public static ToDoubleFunction unbox() { return ToDoubleFunction.UNBOX; } /** * * @param p * @return */ public static DoublePredicate p(final DoublePredicate p) { N.checkArgNotNull(p); return p; } /** * * @param * @param f * @return */ public static DoubleFunction f(final DoubleFunction f) { N.checkArgNotNull(f); return f; } /** * * @param c * @return */ public static DoubleConsumer c(final DoubleConsumer c) { N.checkArgNotNull(c); return c; } /** * * @return */ public static Function len() { return LEN; } /** The Constant SUM. */ private static final Function SUM = new Function() { @Override public Double apply(double[] t) { return N.sum(t); } }; /** * * @return */ public static Function sum() { return SUM; } /** The Constant AVERAGE. */ private static final Function AVERAGE = new Function() { @Override public Double apply(double[] t) { return N.average(t); } }; /** * * @return */ public static Function average() { return AVERAGE; } } /** * Utility class for exceptional {@code Predicate/Function/Consumer}. * * @author haiyangl * */ public static final class Fnn { /** * Instantiates a new fn. */ 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 supplier * @return */ public static Throwables.LazyInitializer memoize(final Throwables.Supplier supplier) { return Throwables.LazyInitializer.of(supplier); } /** * * @param * @param * @return */ public static Throwables.Function identity() { return Fn.IDENTITY; } /** * * @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 the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, Entry, E> inverse() { return (Throwables.Function) Fn.INVERSE; } /** * * @param the key type * @param the value type * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> entry() { return (Throwables.BiFunction) Fn.ENTRY; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> pair() { return (Throwables.BiFunction) Fn.PAIR; } /** * * @param * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.TriFunction, E> triple() { return (Throwables.TriFunction) Fn.TRIPLE; } /** * * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.Function, E> tuple1() { return (Throwables.Function) Fn.TUPLE_1; } /** * * @param * @param * @return */ @SuppressWarnings("rawtypes") public static Throwables.BiFunction, E> tuple2() { return (Throwables.BiFunction) Fn.TUPLE_2; } /** * * @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 new Throwables.Consumer() { @Override public void accept(T t) throws RuntimeException { throw new RuntimeException(errorMessage); } }; } /** * * @param * @param errorMessage * @return */ public static Throwables.Consumer throwIOException(final String errorMessage) { return new Throwables.Consumer() { @Override public void accept(T t) throws IOException { throw new IOException(errorMessage); } }; } /** * * @param * @param errorMessage * @return */ public static Throwables.Consumer throwException(final String errorMessage) { return new Throwables.Consumer() { @Override public void accept(T t) throws Exception { throw new Exception(errorMessage); } }; } /** * * @param * @param * @param excpetionSupplier * @return */ public static Throwables.Consumer throwException(final Supplier excpetionSupplier) { return new Throwables.Consumer() { @Override public void accept(T t) throws E { throw excpetionSupplier.get(); } }; } /** * * @param * @param * @param millis * @return */ public static Throwables.Consumer sleep(final long millis) { return new Throwables.Consumer() { @Override public void accept(T t) { N.sleep(millis); } }; } /** * * @param * @param * @param millis * @return */ public static Throwables.Consumer sleepUninterruptibly(final long millis) { return new Throwables.Consumer() { @Override public void accept(T t) throws E { N.sleepUninterruptibly(millis); } }; } private static final Throwables.Consumer CLOSE = new Throwables.Consumer() { @Override public void accept(final AutoCloseable closeable) throws Exception { if (closeable != null) { closeable.close(); } } }; 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 notNull() { return Fn.NOT_NULL; } /** * * @param * @param predicate * @return */ public static Throwables.Predicate not(final Throwables.Predicate predicate) { N.checkArgNotNull(predicate); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { return !predicate.test(t); } }; } /** * * @param * @param * @param biPredicate * @return */ public static Throwables.BiPredicate not(final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Throwables.BiPredicate() { @Override public boolean test(T t, U u) throws E { return !biPredicate.test(t, u); } }; } /** * * @param * @param * @param * @param triPredicate * @return */ public static Throwables.TriPredicate not(final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Throwables.TriPredicate() { @Override public boolean test(A a, B b, C c) throws E { return !triPredicate.test(a, b, c); } }; } /** * * @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 */ @Beta public static Throwables.Predicate p(final A a, final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { return biPredicate.test(a, t); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triPredicate * @return */ @Beta public static Throwables.Predicate p(final A a, final B b, final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { return 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 */ @Beta public static Throwables.BiPredicate p(final A a, final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Throwables.BiPredicate() { @Override public boolean test(T t, U u) throws E { return 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 */ @Beta public static Throwables.Consumer c(final A a, final Throwables.BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Throwables.Consumer() { @Override public void accept(T t) throws E { biConsumer.accept(a, t); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triConsumer * @return */ @Beta public static Throwables.Consumer c(final A a, final B b, final Throwables.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Throwables.Consumer() { @Override public void accept(T t) throws E { 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 */ @Beta public static Throwables.BiConsumer c(final A a, final Throwables.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Throwables.BiConsumer() { @Override public void accept(T t, U u) throws E { 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 */ @Beta public static Throwables.Function f(final A a, final Throwables.BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Throwables.Function() { @Override public R apply(T t) throws E { return biFunction.apply(a, t); } }; } /** * * @param * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return */ @Beta public static Throwables.Function f(final A a, final B b, final Throwables.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Throwables.Function() { @Override public R apply(T t) throws E { return 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 */ @Beta public static Throwables.BiFunction f(final A a, final Throwables.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Throwables.BiFunction() { @Override public R apply(T t, U u) throws E { return 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 */ @Beta public static Throwables.Predicate pp(final Predicate predicate) { N.checkArgNotNull(predicate); return (Throwables.Predicate) predicate; } /** * * @param * @param * @param * @param a * @param biPredicate * @return */ @Beta public static Throwables.Predicate pp(final A a, final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Throwables.Predicate() { @Override public boolean test(T t) { return biPredicate.test(a, t); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triPredicate * @return */ @Beta public static Throwables.Predicate pp(final A a, final B b, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Throwables.Predicate() { @Override public boolean test(T t) { return triPredicate.test(a, b, t); } }; } /** * * @param * @param * @param * @param biPredicate * @return */ @Beta public static Throwables.BiPredicate pp(final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return (Throwables.BiPredicate) biPredicate; } /** * * @param * @param * @param * @param * @param a * @param triPredicate * @return */ @Beta public static Throwables.BiPredicate pp(final A a, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Throwables.BiPredicate() { @Override public boolean test(T t, U u) { return triPredicate.test(a, t, u); } }; } /** * * @param * @param * @param * @param * @param triPredicate * @return */ @Beta public static Throwables.TriPredicate pp(final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return (Throwables.TriPredicate) triPredicate; } /** * * @param * @param * @param consumer * @return */ @Beta public static Throwables.Consumer cc(final Consumer consumer) { N.checkArgNotNull(consumer); return (Throwables.Consumer) consumer; } /** * * @param * @param * @param * @param a * @param biConsumer * @return */ @Beta public static Throwables.Consumer cc(final A a, final BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Throwables.Consumer() { @Override public void accept(T t) { biConsumer.accept(a, t); } }; } /** * * @param * @param * @param * @param * @param a * @param b * @param triConsumer * @return */ @Beta public static Throwables.Consumer cc(final A a, final B b, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Throwables.Consumer() { @Override public void accept(T t) { triConsumer.accept(a, b, t); } }; } /** * * @param * @param * @param * @param biConsumer * @return */ @Beta public static Throwables.BiConsumer cc(final BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return (Throwables.BiConsumer) biConsumer; } /** * * @param * @param * @param * @param * @param a * @param triConsumer * @return */ @Beta public static Throwables.BiConsumer cc(final A a, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Throwables.BiConsumer() { @Override public void accept(T t, U u) { triConsumer.accept(a, t, u); } }; } /** * * @param * @param * @param * @param * @param triConsumer * @return */ @Beta public static Throwables.TriConsumer cc(final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return (Throwables.TriConsumer) triConsumer; } /** * * @param * @param * @param * @param function * @return */ @Beta public static Throwables.Function ff(final Function function) { N.checkArgNotNull(function); return (Throwables.Function) function; } /** * * @param * @param * @param * @param * @param a * @param biFunction * @return */ @Beta public static Throwables.Function ff(final A a, final BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Throwables.Function() { @Override public R apply(T t) { return biFunction.apply(a, t); } }; } /** * * @param * @param * @param * @param * @param * @param a * @param b * @param triFunction * @return */ @Beta public static Throwables.Function ff(final A a, final B b, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Throwables.Function() { @Override public R apply(T t) { return triFunction.apply(a, b, t); } }; } /** * * @param * @param * @param * @param * @param biFunction * @return */ @Beta public static Throwables.BiFunction ff(final BiFunction biFunction) { N.checkArgNotNull(biFunction); return (Throwables.BiFunction) biFunction; } /** * * @param * @param * @param * @param * @param * @param a * @param triFunction * @return */ @Beta public static Throwables.BiFunction ff(final A a, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Throwables.BiFunction() { @Override public R apply(T t, U u) { return triFunction.apply(a, t, u); } }; } /** * * @param * @param * @param * @param * @param * @param triFunction * @return */ @Beta public static Throwables.TriFunction ff(final TriFunction triFunction) { N.checkArgNotNull(triFunction); return (Throwables.TriFunction) triFunction; } /** * Synchronized {@code Predicate}. * * @param * @param * @param mutex to synchronized on * @param predicate * @return */ @Beta public static Throwables.Predicate sp(final Object mutex, final Throwables.Predicate predicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(predicate, "predicate"); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { synchronized (mutex) { return predicate.test(t); } } }; } /** * Synchronized {@code Predicate}. * * @param * @param * @param mutex to synchronized on * @param a * @param biPredicate * @return */ @Beta public static Throwables.Predicate sp(final Object mutex, final A a, final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { 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 */ @Beta public static Throwables.Predicate sp(final Object mutex, final A a, final B b, final Throwables.TriPredicate triPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(triPredicate, "triPredicate"); return new Throwables.Predicate() { @Override public boolean test(T t) throws E { synchronized (mutex) { return triPredicate.test(a, b, t); } } }; } /** * Synchronized {@code BiPredicate}. * * @param * @param * @param * @param mutex to synchronized on * @param biPredicate * @return */ @Beta public static Throwables.BiPredicate sp(final Object mutex, final Throwables.BiPredicate biPredicate) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biPredicate, "biPredicate"); return new Throwables.BiPredicate() { @Override public boolean test(T t, U u) throws E { synchronized (mutex) { return biPredicate.test(t, u); } } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param mutex to synchronized on * @param consumer * @return */ @Beta public static Throwables.Consumer sc(final Object mutex, final Throwables.Consumer consumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(consumer, "consumer"); return new Throwables.Consumer() { @Override public void accept(T t) throws E { synchronized (mutex) { consumer.accept(t); } } }; } /** * Synchronized {@code Consumer}. * * @param * @param * @param * @param mutex to synchronized on * @param a * @param biConsumer * @return */ @Beta public static Throwables.Consumer sc(final Object mutex, final A a, final Throwables.BiConsumer biConsumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new Throwables.Consumer() { @Override public void accept(T t) throws E { synchronized (mutex) { biConsumer.accept(a, t); } } }; } /** * Synchronized {@code BiConsumer}. * * @param * @param * @param * @param mutex to synchronized on * @param biConsumer * @return */ @Beta public static Throwables.BiConsumer sc(final Object mutex, final Throwables.BiConsumer biConsumer) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biConsumer, "biConsumer"); return new Throwables.BiConsumer() { @Override public void accept(T t, U u) throws E { synchronized (mutex) { biConsumer.accept(t, u); } } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param mutex to synchronized on * @param function * @return */ @Beta public static Throwables.Function sf(final Object mutex, final Throwables.Function function) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(function, "function"); return new Throwables.Function() { @Override public R apply(T t) throws E { synchronized (mutex) { return function.apply(t); } } }; } /** * Synchronized {@code Function}. * * @param * @param * @param * @param * @param mutex to synchronized on * @param a * @param biFunction * @return */ @Beta public static Throwables.Function sf(final Object mutex, final A a, final Throwables.BiFunction biFunction) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new Throwables.Function() { @Override public R apply(T t) throws E { synchronized (mutex) { return biFunction.apply(a, t); } } }; } /** * Synchronized {@code BiFunction}. * * @param * @param * @param * @param * @param mutex to synchronized on * @param biFunction * @return */ @Beta public static Throwables.BiFunction sf(final Object mutex, final Throwables.BiFunction biFunction) { N.checkArgNotNull(mutex, "mutex"); N.checkArgNotNull(biFunction, "biFunction"); return new Throwables.BiFunction() { @Override public R apply(T t, U u) throws E { synchronized (mutex) { return biFunction.apply(t, u); } } }; } public static Throwables.Runnable rr(final com.landawn.abacus.util.function.Runnable runnable) { return (Throwables.Runnable) runnable; } public static Throwables.Callable cc(final com.landawn.abacus.util.function.Callable callable) { return (Throwables.Callable) callable; } /** * * @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 callable * @return */ public static Throwables.Callable callable(final Throwables.Callable callable) { N.checkArgNotNull(callable); return callable; } /** * * @param * @param runnable * @return */ public static Throwables.Runnable runnable(final Throwables.Runnable runnable) { N.checkArgNotNull(runnable); return runnable; } /** * * @param * @param runnable * @return */ public static Throwables.Callable toCallable(final Throwables.Runnable runnable) { N.checkArgNotNull(runnable); return new Throwables.Callable() { @Override public Void call() throws E { runnable.run(); return null; } }; } /** * * @param * @param runnable * @param result * @return */ public static Throwables.Callable toCallable(final Throwables.Runnable runnable, final T result) { N.checkArgNotNull(runnable); return new Throwables.Callable() { @Override public T call() throws E { runnable.run(); return result; } }; } /** * * @param * @param * @param callable * @return */ public static Throwables.Runnable toRunnable(final Throwables.Callable callable) { N.checkArgNotNull(callable); return new Throwables.Runnable() { @Override public void run() throws E { callable.call(); } }; } } }