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

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

There is a newer version: 1.10.1
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.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractMap.SimpleImmutableEntry;
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.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.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.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.ByteBiFunction;
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.CharBiFunction;
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.DoubleBiFunction;
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.FloatBiFunction;
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.IntBiFunction;
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.LongBiFunction;
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.LongSupplier;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.QuadFunction;
import com.landawn.abacus.util.function.ShortBiFunction;
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.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
import com.landawn.abacus.util.function.TriConsumer;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
import com.landawn.abacus.util.function.UnaryOperator;
import com.landawn.abacus.util.stream.Collector;
import com.landawn.abacus.util.stream.ObjIteratorEx;
import com.landawn.abacus.util.stream.Stream;

/**
 * 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 abstract class Fn extends Comparators { private static final Object NONE = new Object(); private static final Timer timer = new Timer(); @SuppressWarnings("rawtypes") public static final IntFunction> FACTORY_OF_MAP = (IntFunction) Factory.MAP_FACTORY; @SuppressWarnings("rawtypes") public static final IntFunction> FACTORY_OF_LINKED_HASH_MAP = (IntFunction) Factory.LINKED_HASH_MAP_FACTORY; @SuppressWarnings("rawtypes") public static final Supplier> SUPPLIER_OF_MAP = (Supplier) Suppliers.MAP; @SuppressWarnings("rawtypes") public static final Supplier> SUPPLIER_OF_LINKED_HASH_MAP = (Supplier) Suppliers.LINKED_HASH_MAP; private static final com.landawn.abacus.util.function.Runnable EMPTY_ACTION = new com.landawn.abacus.util.function.Runnable() { @Override public void run() { } }; @SuppressWarnings("rawtypes") private static final Consumer DO_NOTHING = new Consumer() { @Override public void accept(Object value) { // do nothing. } }; private static final Consumer CLOSE = new Consumer() { @Override public void accept(AutoCloseable value) { IOUtil.close(value); } }; private static final Consumer CLOSE_QUIETLY = new Consumer() { @Override public void accept(AutoCloseable value) { IOUtil.closeQuietly(value); } }; @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))); } }; @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))); } }; @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))); } }; @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))); } }; @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))); } }; @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))); } }; @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))); } }; @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))); } }; @SuppressWarnings("rawtypes") private static final Consumer PRINTLN = new Consumer() { @Override public void accept(Object value) { N.println(value); } }; @SuppressWarnings("rawtypes") private static final Function TO_STRING = new Function() { @Override public Object apply(Object t) { return N.toString(t); } }; private static final Function TO_CAMEL_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toCamelCase(t); } }; private static final Function TO_LOWER_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toLowerCase(t); } }; private static final Function TO_LOWER_CASE_WITH_UNDERSCORE = new Function() { @Override public String apply(String t) { return StringUtil.toLowerCaseWithUnderscore(t); } }; private static final Function TO_UPPER_CASE = new Function() { @Override public String apply(String t) { return StringUtil.toUpperCase(t); } }; private static final Function TO_UPPER_CASE_WITH_UNDERSCORE = new Function() { @Override public String apply(String t) { return StringUtil.toUpperCaseWithUnderscore(t); } }; @SuppressWarnings("rawtypes") private static final BiFunction COMPARE = new BiFunction() { @Override public Integer apply(Comparable a, Comparable b) { return N.compare(a, b); } }; @SuppressWarnings("rawtypes") private static final Function IDENTITY = new Function() { @Override public Object apply(Object t) { return t; } }; private static final Function TRIM = new Function() { @Override public String apply(String t) { return t == null ? null : t.trim(); } }; private static final Function TRIM_TO_EMPTY = new Function() { @Override public String apply(String t) { return t == null ? "" : t.trim(); } }; 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; } }; private static final Function NULL_TO_EMPTY = new Function() { @Override public String apply(String t) { return t == null ? N.EMPTY_STRING : t; } }; @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_L = new Function() { @Override public List apply(List t) { return t == null ? N.emptyList() : t; } }; @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_S = new Function() { @Override public Set apply(Set t) { return t == null ? N.emptySet() : t; } }; @SuppressWarnings("rawtypes") private static final Function NULL_TO_EMPTY_M = new Function() { @Override public Map apply(Map t) { return t == null ? N.emptyMap() : t; } }; private static final Function LENGTH = new Function() { @Override public Integer apply(CharSequence t) { return t == null ? 0 : t.length(); } }; private static final Function LEN = new Function() { @Override public Integer apply(Object[] t) { return t == null ? 0 : t.length; } }; @SuppressWarnings("rawtypes") private static final Function SIZE = new Function() { @Override public Integer apply(Collection t) { return t == null ? 0 : t.size(); } }; @SuppressWarnings("rawtypes") private static final Function SIZE_M = new Function() { @Override public Integer apply(Map t) { return t == null ? 0 : t.size(); } }; private static final Function, Object> KEY = new Function, Object>() { @Override public Object apply(Map.Entry t) { return t.getKey(); } }; private static final Function, Object> VALUE = new Function, Object>() { @Override public Object apply(Map.Entry t) { return t.getValue(); } }; private static final Function, Map.Entry> INVERSE = new Function, Map.Entry>() { @Override public Map.Entry apply(Map.Entry t) { return new SimpleImmutableEntry<>(t.getValue(), t.getKey()); } }; private static final BiFunction> ENTRY = new BiFunction>() { @Override public Map.Entry apply(Object key, Object value) { return new SimpleImmutableEntry<>(key, value); } }; private static final BiFunction> PAIR = new BiFunction>() { @Override public Pair apply(Object key, Object value) { return Pair.of(key, value); } }; private static final TriFunction> TRIPLE = new TriFunction>() { @Override public Triple apply(Object a, Object b, Object c) { return Triple.of(a, b, c); } }; private static final Function> TUPLE_1 = new Function>() { @Override public Tuple1 apply(Object t) { return Tuple.of(t); } }; private static final BiFunction> TUPLE_2 = new BiFunction>() { @Override public Tuple2 apply(Object t, Object u) { return Tuple.of(t, u); } }; private static final TriFunction> TUPLE_3 = new TriFunction>() { @Override public Tuple3 apply(Object a, Object b, Object c) { return Tuple.of(a, b, c); } }; 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); } }; @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_TRUE = new Predicate() { @Override public boolean test(Object value) { return true; } }; @SuppressWarnings("rawtypes") private static final Predicate ALWAYS_FALSE = new Predicate() { @Override public boolean test(Object value) { return false; } }; @SuppressWarnings("rawtypes") private static final Predicate IS_NULL = new Predicate() { @Override public boolean test(Object value) { return value == null; } }; private static final Predicate IS_NULL_OR_EMPTY = new Predicate() { @Override public boolean test(CharSequence value) { return value == null || value.length() == 0; } }; private static final Predicate IS_NULL_OR_EMPTY_OR_BLANK = new Predicate() { @Override public boolean test(CharSequence value) { return N.isNullOrEmptyOrBlank(value); } }; @SuppressWarnings("rawtypes") private static final Predicate NOT_NULL = new Predicate() { @Override public boolean test(Object value) { return value != null; } }; private static final Predicate NOT_NULL_OR_EMPTY = new Predicate() { @Override public boolean test(CharSequence value) { return value != null && value.length() > 0; } }; private static final Predicate NOT_NULL_OR_EMPTY_OR_BLANK = new Predicate() { @Override public boolean test(CharSequence value) { return N.notNullOrEmptyOrBlank(value); } }; protected Fn() { super(); // for extention. } 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 supplier * @return */ public static Supplier memoize(final Supplier supplier) { return new Supplier() { private volatile boolean initialized = false; private T instance = null; @Override public T get() { if (initialized == false) { synchronized (this) { if (initialized == false) { instance = supplier.get(); } } } return instance; } }; } 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 supplier * @return * @see {@code Stream.split/sliding}; */ @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 supplier * @return * @see {@code Stream.split/sliding}; */ @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; } }; } 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); } }; } @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); } }; } 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); } }; } 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); } }; } @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); } }; } 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); } }; } public static com.landawn.abacus.util.function.Runnable emptyAction() { return EMPTY_ACTION; } public static Consumer doNothing() { return DO_NOTHING; } public static Consumer close() { return (Consumer) CLOSE; } public static Consumer closeQuietly() { return (Consumer) CLOSE_QUIETLY; } public static Consumer sleep(final long millis) { return new Consumer() { @Override public void accept(T t) { N.sleep(millis); } }; } public static Consumer sleepUninterruptibly(final long millis) { return new Consumer() { @Override public void accept(T t) { N.sleepUninterruptibly(millis); } }; } public static Consumer println() { return PRINTLN; } 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); } }; } } public static Function toStr() { return TO_STRING; } public static Function toCamelCase() { return TO_CAMEL_CASE; } public static Function toLowerCase() { return TO_LOWER_CASE; } public static Function toLowerCaseWithUnderscore() { return TO_LOWER_CASE_WITH_UNDERSCORE; } public static Function toUpperCase() { return TO_UPPER_CASE; } public static Function toUpperCaseWithUnderscore() { return TO_UPPER_CASE_WITH_UNDERSCORE; } public static Function identity() { return IDENTITY; } 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); } }; } private static final Function, Object> VAL = new Function, Object>() { @Override public Object apply(Keyed t) { return t.val(); } }; private static final Function, Object>, Object> KK = new Function, Object>, Object>() { @Override public Object apply(Map.Entry, Object> t) { return t.getKey().val(); } }; @SuppressWarnings("rawtypes") public static Function, T> val() { return (Function) VAL; } @SuppressWarnings("rawtypes") public static Function, V>, T> kk() { return (Function) KK; } private static final Function> WRAP = new Function>() { @Override public Wrapper apply(Object t) { return Wrapper.of(t); } }; @SuppressWarnings("rawtypes") public static Function> wrap() { return (Function) WRAP; } 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); } }; } private static final Function, Object> UNWRAP = new Function, Object>() { @Override public Object apply(Wrapper t) { return t.value(); } }; @SuppressWarnings("rawtypes") public static Function, T> unwrap() { return (Function) UNWRAP; } @SuppressWarnings("rawtypes") public static Function, K> key() { return (Function) KEY; } @SuppressWarnings("rawtypes") public static Function, V> value() { return (Function) VALUE; } @SuppressWarnings("rawtypes") public static Function, Entry> inverse() { return (Function) INVERSE; } @SuppressWarnings("rawtypes") public static BiFunction> entry() { return (BiFunction) ENTRY; } public static Function> entry(final K key) { return new Function>() { @Override public Entry apply(T t) { return new SimpleImmutableEntry<>(key, t); } }; } public static Function> entry(final Function keyMapper) { N.checkArgNotNull(keyMapper); return new Function>() { @Override public Entry apply(T t) { return new SimpleImmutableEntry<>(keyMapper.apply(t), t); } }; } @SuppressWarnings("rawtypes") public static BiFunction> pair() { return (BiFunction) PAIR; } @SuppressWarnings("rawtypes") public static TriFunction> triple() { return (TriFunction) TRIPLE; } @SuppressWarnings("rawtypes") public static Function> tuple1() { return (Function) TUPLE_1; } @SuppressWarnings("rawtypes") public static BiFunction> tuple2() { return (BiFunction) TUPLE_2; } @SuppressWarnings("rawtypes") public static TriFunction> tuple3() { return (TriFunction) TUPLE_3; } @SuppressWarnings({ "rawtypes" }) public static QuadFunction> tuple4() { return (QuadFunction) TUPLE_4; } public static Function trim() { return TRIM; } public static Function trimToEmpty() { return TRIM_TO_EMPTY; } public static Function trimToNull() { return TRIM_TO_NULL; } public static Function nullToEmpty() { return NULL_TO_EMPTY; } @SuppressWarnings("rawtypes") public static Function, List> nullToEmptyL() { return (Function) NULL_TO_EMPTY_L; } @SuppressWarnings("rawtypes") public static Function, Set> nullToEmptyS() { return (Function) NULL_TO_EMPTY_S; } @SuppressWarnings("rawtypes") public static Function, Map> nullToEmptyM() { return (Function) NULL_TO_EMPTY_M; } public static Function length() { return (Function) LENGTH; } @SuppressWarnings("rawtypes") public static Function len() { return (Function) LEN; } @SuppressWarnings("rawtypes") public static Function size() { return (Function) SIZE; } @SuppressWarnings("rawtypes") public static Function sizeM() { return (Function) SIZE_M; } public static Function cast(final Class clazz) { N.checkArgNotNull(clazz); return new Function() { @Override public U apply(T t) { return (U) t; } }; } public static Predicate alwaysTrue() { return ALWAYS_TRUE; } public static Predicate alwaysFalse() { return ALWAYS_FALSE; } public static Predicate isNull() { return IS_NULL; } public static Predicate isNullOrEmpty() { return (Predicate) IS_NULL_OR_EMPTY; } public static Predicate isNullOrEmptyOrBlank() { return (Predicate) IS_NULL_OR_EMPTY_OR_BLANK; } public static Predicate notNull() { return NOT_NULL; } public static Predicate notNullOrEmpty() { return (Predicate) NOT_NULL_OR_EMPTY; } public static Predicate notNullOrEmptyOrBlank() { return (Predicate) NOT_NULL_OR_EMPTY_OR_BLANK; } public static Predicate equal(final Object target) { return new Predicate() { @Override public boolean test(T value) { return N.equals(value, target); } }; } public static Predicate notEqual(final Object target) { return new Predicate() { @Override public boolean test(T value) { return !N.equals(value, target); } }; } @SuppressWarnings("rawtypes") public static Predicate greaterThan(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) > 0; } }; } @SuppressWarnings("rawtypes") public static Predicate greaterEqual(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) >= 0; } }; } @SuppressWarnings("rawtypes") public static Predicate lessThan(final T target) { return new Predicate() { @Override public boolean test(T value) { return N.compare(value, target) < 0; } }; } @SuppressWarnings("rawtypes") 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 minValue * @param maxValue * @return */ @SuppressWarnings("rawtypes") 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; } }; } public static Predicate in(final Collection c) { N.checkArgNotNull(c); return new Predicate() { @Override public boolean test(T value) { return c != null && c.size() > 0 && c.contains(value); } }; } public static Predicate notIn(final Collection c) { N.checkArgNotNull(c); return new Predicate() { @Override public boolean test(T value) { return c == null || c.size() == 0 || !c.contains(value); } }; } 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); } }; } @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); } }; } 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); } }; } 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); } }; } 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); } }; } 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); } }; } 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); } }; } 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); } }; } public static Predicate matches(final Pattern pattern) { N.checkArgNotNull(pattern); return new Predicate() { @Override public boolean test(CharSequence value) { return pattern.matcher(value).find(); } }; } public static BiPredicate equal() { return BiPredicates.EQUAL; } public static BiPredicate notEqual() { return BiPredicates.NOT_EQUAL; } @SuppressWarnings("rawtypes") public static BiPredicate greaterThan() { return (BiPredicate) BiPredicates.GREATER_THAN; } @SuppressWarnings("rawtypes") public static BiPredicate greaterEqual() { return (BiPredicate) BiPredicates.GREATER_EQUAL; } @SuppressWarnings("rawtypes") public static BiPredicate lessThan() { return (BiPredicate) BiPredicates.LESS_THAN; } @SuppressWarnings("rawtypes") public static BiPredicate lessEqual() { return (BiPredicate) BiPredicates.LESS_EQUAL; } public static Predicate not(final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate() { @Override public boolean test(T t) { return !predicate.test(t); } }; } 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); } }; } 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); } }; } 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(); } }; } 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(); } }; } 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); } }; } 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); } }; } public static Predicate and(final Collection> c) { 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; } }; } 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); } }; } 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); } }; } public static BiPredicate and(final List> c) { 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; } }; } 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(); } }; } 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(); } }; } 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); } }; } 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); } }; } public static Predicate or(final Collection> c) { 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; } }; } 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); } }; } 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); } }; } public static BiPredicate or(final List> c) { 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; } }; } public static Predicate> testByKey(final Predicate predicate) { N.checkArgNotNull(predicate); return new Predicate>() { @Override public boolean test(Entry entry) { return predicate.test(entry.getKey()); } }; } 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 predicate * @return * @deprecated replaced by {@link Fn#p(Predicate)}. */ @Deprecated static Predicate test(final Predicate predicate) { return predicate; } /** * Returns the specified instance. * * @param predicate * @return * @deprecated replaced by {@link Fn#p(BiPredicate)}. */ @Deprecated static BiPredicate test(final BiPredicate predicate) { return predicate; } /** * Returns the specified instance. * * @param predicate * @return * @deprecated */ @Deprecated static TriPredicate test(final TriPredicate predicate) { return predicate; } public static Consumer> acceptByKey(final Consumer consumer) { N.checkArgNotNull(consumer); return new Consumer>() { @Override public void accept(Entry entry) { consumer.accept(entry.getKey()); } }; } public static Consumer> acceptByValue(final Consumer consumer) { N.checkArgNotNull(consumer); return new Consumer>() { @Override public void accept(Entry entry) { consumer.accept(entry.getValue()); } }; } 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()); } }; } 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()); } }; } 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); } } }; } 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); } } }; } 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); } } }; } 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); } } }; } 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); } } }; } 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); } } }; } 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 SimpleImmutableEntry(func.apply(entry.getKey()), entry.getValue()); } }; } 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 SimpleImmutableEntry(entry.getKey(), func.apply(entry.getValue())); } }; } 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()); } }; } 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()); } }; } 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()); } }; } private static Function PARSE_INT_FUNC = new Function() { @Override public Integer apply(String t) { return N.parseInt(t); } }; private static Function PARSE_BYTE_FUNC = new Function() { @Override public Byte apply(String t) { return N.parseByte(t); } }; public static Function parseByte() { return PARSE_BYTE_FUNC; } private static Function PARSE_SHORT_FUNC = new Function() { @Override public Short apply(String t) { return N.parseShort(t); } }; public static Function parseShort() { return PARSE_SHORT_FUNC; } public static Function parseInt() { return PARSE_INT_FUNC; } private static Function PARSE_LONG_FUNC = new Function() { @Override public Long apply(String t) { return N.parseLong(t); } }; public static Function parseLong() { return PARSE_LONG_FUNC; } private static Function PARSE_FLOAT_FUNC = new Function() { @Override public Float apply(String t) { return N.parseFloat(t); } }; public static Function parseFloat() { return PARSE_FLOAT_FUNC; } private static Function PARSE_DOUBLE_FUNC = new Function() { @Override public Double apply(String t) { return N.parseDouble(t); } }; public static Function parseDouble() { return PARSE_DOUBLE_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); } }); } }; /** * * @return * @see StringUtil#createNumber(String) */ public static Function createNumber() { return CREATE_NUMBER_FUNC; } @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToIntFunction numToInt() { return (ToIntFunction) ToIntFunction.FROM_NUM; } @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToLongFunction numToLong() { return (ToLongFunction) ToLongFunction.FROM_NUM; } @SuppressWarnings({ "unchecked", "rawtypes" }) public static ToDoubleFunction numToDouble() { return (ToDoubleFunction) ToDoubleFunction.FROM_NUM; } /** * * @param limit * @param predicate * @return */ 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); } }; } 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); } }; } 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; } }; } 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; } }; } public static Predicate timeLimit(final long timeInMillis) { N.checkArgNotNegative(timeInMillis, "timeInMillis"); 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(); } }; } public static Predicate timeLimit(final Duration duration) { N.checkArgNotNull(duration, "duration"); return timeLimit(duration.toMillis()); } /** * Returns a stateful Function which should not be used in parallel stream. * * @return */ @SequentialOnly 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 which should not be used in parallel stream. * * @param predicate * @return */ @SequentialOnly public static Predicate indexed(final IndexedPredicate predicate) { return Predicates.indexed(predicate); } /** * Returns a stateful BiPredicate which should not be used in parallel stream. * * @param predicate * @return * @deprecated replaced by {@code BiPredicates#indexed(IndexedBiPredicate)}. */ @Deprecated static BiPredicate indexed(final IndexedBiPredicate predicate) { return BiPredicates.indexed(predicate); } /** * Returns a stateful Function which should not be used in parallel stream. * * @param func * @return * @deprecated replaced by {@code Functions#indexed(IndexedFunction)}. */ @Deprecated static Function indexedd(final IndexedFunction func) { return Functions.indexed(func); } /** * Returns a stateful BiFunction which should not be used in parallel stream. * * @param func * @return * @deprecated replaced by {@code BiFunctions#indexed(IndexedBiFunction)}. */ @Deprecated static BiFunction indexedd(final IndexedBiFunction func) { return BiFunctions.indexed(func); } /** * Returns a stateful Consumer which should not be used in parallel stream. * * @param action * @return * @deprecated replaced by {@code Consumers#indexed(IndexedConsumer)}. */ @Deprecated static Consumer indexeed(final IndexedConsumer action) { return Consumers.indexed(action); } /** * Returns a stateful BiConsumer which should not be used in parallel stream. * * @param action * @return * @deprecated replaced by {@code BiConsumers#indexed(IndexedBiConsumer)}. */ @Deprecated static BiConsumer indexeed(final IndexedBiConsumer action) { return BiConsumers.indexed(action); } @SuppressWarnings("rawtypes") public static Function compareTo(final T target) { return new Function() { @Override public Integer apply(T t) { return N.compare(t, target); } }; } @SuppressWarnings("rawtypes") public static Function compareTo(final T target, final Comparator cmp) { // N.checkArgNotNull(cmp); if (cmp == null || cmp == Comparators.naturalOrder()) { return (Function) compareTo((Comparable) target); } return new Function() { @Override public Integer apply(T t) { return N.compare(t, target, cmp); } }; } @SuppressWarnings("rawtypes") public static BiFunction compare() { return COMPARE; } 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); } }; } @Beta public static Predicate p(final Predicate predicate) { return predicate; } @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); } }; } @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); } }; } @Beta public static BiPredicate p(final BiPredicate biPredicate) { return biPredicate; } @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); } }; } @Beta public static TriPredicate p(final TriPredicate triPredicate) { return triPredicate; } @Beta public static Consumer c(final Consumer predicate) { return predicate; } @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); } }; } @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); } }; } @Beta public static BiConsumer c(final BiConsumer biConsumer) { return biConsumer; } @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); } }; } @Beta public static TriConsumer c(final TriConsumer triConsumer) { return triConsumer; } @Beta public static Function f(final Function predicate) { return predicate; } @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); } }; } @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); } }; } @Beta public static BiFunction f(final BiFunction biFunction) { return biFunction; } @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); } }; } @Beta public static TriFunction f(final TriFunction triFunction) { return triFunction; } @Beta public static Try.Predicate ep(final Try.Predicate predicate) { return predicate; } @Beta public static Try.Predicate ep(final A a, final Try.BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Try.Predicate() { @Override public boolean test(T t) throws E { return biPredicate.test(a, t); } }; } @Beta public static Try.Predicate ep(final A a, final B b, final Try.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Try.Predicate() { @Override public boolean test(T t) throws E { return triPredicate.test(a, b, t); } }; } @Beta public static Try.BiPredicate ep(final Try.BiPredicate biPredicate) { return biPredicate; } @Beta public static Try.BiPredicate ep(final A a, final Try.TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Try.BiPredicate() { @Override public boolean test(T t, U u) throws E { return triPredicate.test(a, t, u); } }; } @Beta public static Try.TriPredicate ep(final Try.TriPredicate triPredicate) { return triPredicate; } @Beta public static Try.Consumer ec(final Try.Consumer predicate) { return predicate; } @Beta public static Try.Consumer ec(final A a, final Try.BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Try.Consumer() { @Override public void accept(T t) throws E { biConsumer.accept(a, t); } }; } @Beta public static Try.Consumer ec(final A a, final B b, final Try.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Try.Consumer() { @Override public void accept(T t) throws E { triConsumer.accept(a, b, t); } }; } @Beta public static Try.BiConsumer ec(final Try.BiConsumer biConsumer) { return biConsumer; } @Beta public static Try.BiConsumer ec(final A a, final Try.TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Try.BiConsumer() { @Override public void accept(T t, U u) throws E { triConsumer.accept(a, t, u); } }; } @Beta public static Try.TriConsumer ec(final Try.TriConsumer triConsumer) { return triConsumer; } @Beta public static Try.Function ef(final Try.Function predicate) { return predicate; } @Beta public static Try.Function ef(final A a, final Try.BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Try.Function() { @Override public R apply(T t) throws E { return biFunction.apply(a, t); } }; } @Beta public static Try.Function ef(final A a, final B b, final Try.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Try.Function() { @Override public R apply(T t) throws E { return triFunction.apply(a, b, t); } }; } @Beta public static Try.BiFunction ef(final Try.BiFunction biFunction) { return biFunction; } @Beta public static Try.BiFunction ef(final A a, final Try.TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Try.BiFunction() { @Override public R apply(T t, U u) throws E { return triFunction.apply(a, t, u); } }; } @Beta public static Try.TriFunction ef(final Try.TriFunction triFunction) { return triFunction; } @Beta public static Predicate pp(final Try.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); } } }; } @Beta public static Predicate pp(final A a, final Try.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); } } }; } @Beta public static Predicate pp(final A a, final B b, final Try.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); } } }; } @Beta public static BiPredicate pp(final Try.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); } } }; } @Beta public static BiPredicate pp(final A a, final Try.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); } } }; } @Beta public static TriPredicate pp(final Try.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); } } }; } @Beta public static Consumer cc(final Try.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); } } }; } @Beta public static Consumer cc(final A a, final Try.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); } } }; } @Beta public static Consumer cc(final A a, final B b, final Try.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); } } }; } @Beta public static BiConsumer cc(final Try.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); } } }; } @Beta public static BiConsumer cc(final A a, final Try.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); } } }; } @Beta public static TriConsumer cc(final Try.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); } } }; } @Beta public static Function ff(final Try.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); } } }; } @Beta public static Function ff(final A a, final Try.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); } } }; } @Beta public static Function ff(final A a, final B b, final Try.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); } } }; } @Beta public static BiFunction ff(final Try.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); } } }; } @Beta public static BiFunction ff(final A a, final Try.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); } } }; } @Beta public static TriFunction ff(final Try.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); } } }; } /** * Synchronized {@code Predicate} * * @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 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 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 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 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 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 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 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 mutex to synchronized on * @param u * @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 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); } } }; } /** * Split this stream by the specified duration. * *
     * 
     * Stream> s = ...;
     * s.__(Fn.window(Duration.ofMinutes(3), () - System.currentTimeMillis()))...// Do your stuffs with Stream>>;
     * 
     * 
* * @param duration * @param startTime * @return */ public static Function>, Stream>>> window(final Duration duration, final LongSupplier startTime) { return window(duration, duration.toMillis(), startTime); } /** * * @param duration * @param startTime * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream>>> windowToList(final Duration duration, final LongSupplier startTime) { return window(duration, startTime, Suppliers.> ofList()); } /** * * @param duration * @param startTime * @return */ public static Function>, Stream>>> windowToSet(final Duration duration, final LongSupplier startTime) { return window(duration, startTime, Suppliers.> ofSet()); } /** * * @param duration * @param startTime * @param collectionSupplier * @return */ public static >> Function>, Stream> window(final Duration duration, final LongSupplier startTime, final Supplier collectionSupplier) { return window(duration, duration.toMillis(), startTime, collectionSupplier); } /** * * @param duration * @param startTime * @param collector * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream> window(final Duration duration, final LongSupplier startTime, final Collector, A, R> collector) { return window(duration, duration.toMillis(), startTime, collector); } /** * * @param duration * @param incrementInMillis * @param startTime * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream>>> window(final Duration duration, final long incrementInMillis, final LongSupplier startTime) { final Function>, Stream>>> mapper = windowToList(duration, incrementInMillis, startTime); return new Function>, Stream>>>() { @Override public Stream>> apply(Stream> t) { return mapper.apply(t).map(new Function>, Stream>>() { @Override public Stream> apply(List> t) { return Stream.of(t); } }); } }; } /** * * @param duration * @param incrementInMillis * @param startTime * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream>>> windowToList(final Duration duration, final long incrementInMillis, final LongSupplier startTime) { return window(duration, incrementInMillis, startTime, Suppliers.> ofList()); } /** * * @param duration * @param incrementInMillis * @param startTime * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream>>> windowToSet(final Duration duration, final long incrementInMillis, final LongSupplier startTime) { return window(duration, incrementInMillis, startTime, Suppliers.> ofSet()); } /** * * @param duration * @param incrementInMillis * @param startTime * @param collectionSupplier * @return * @see #window(Duration, LongSupplier) */ public static >> Function>, Stream> window(final Duration duration, final long incrementInMillis, final LongSupplier startTime, final Supplier collectionSupplier) { return new Function>, Stream>() { @Override public Stream apply(final Stream> s) { final ObjIterator iter = new ObjIteratorEx() { private long durationInMillis; private boolean useQueue; private Deque> queue; private Iterator> queueIter; private ObjIterator> iter; private Timed next = null; private long fromTime; private long endTime; private boolean initialized = false; @Override public boolean hasNext() { if (initialized == false) { init(); } if (useQueue) { if ((queue.size() > 0 && queue.getLast().timestamp() >= endTime) || (next != null && next.timestamp() - fromTime >= incrementInMillis)) { return true; } else { while (iter.hasNext()) { next = iter.next(); if (next.timestamp() - fromTime >= incrementInMillis) { queue.add(next); return true; } } return false; } } else { while ((next == null || next.timestamp() - fromTime < incrementInMillis) && iter.hasNext()) { next = iter.next(); if (next.timestamp() - fromTime >= incrementInMillis) { break; } } return next != null && next.timestamp() - fromTime >= incrementInMillis; } } @Override public C next() { if (hasNext() == false) { throw new NoSuchElementException(); } fromTime += incrementInMillis; endTime = fromTime + durationInMillis; final C result = collectionSupplier.get(); if (useQueue) { queueIter = queue.iterator(); while (queueIter.hasNext()) { next = queueIter.next(); if (next.timestamp() < fromTime) { queueIter.remove(); } else if (next.timestamp() < endTime) { result.add(next); next = null; } else { return result; } } while (iter.hasNext()) { next = iter.next(); if (next.timestamp() >= fromTime) { queue.add(next); if (next.timestamp() < endTime) { result.add(next); next = null; } else { break; } } } } else { if (next != null) { if (next.timestamp() < fromTime) { // ignore } else if (next.timestamp() < endTime) { result.add(next); } else { return result; } } while (iter.hasNext()) { next = iter.next(); if (next.timestamp() < fromTime) { // ignore } else if (next.timestamp() < endTime) { result.add(next); } else { break; } } } return result; } private void init() { if (initialized == false) { initialized = true; N.checkArgNotNull(duration, "duration"); N.checkArgPositive(duration.toMillis(), "duration"); N.checkArgPositive(incrementInMillis, "incrementInMillis"); N.checkArgNotNull(startTime, "startTime"); N.checkArgNotNull(collectionSupplier, "collectionSupplier"); iter = s.iterator(); durationInMillis = duration.toMillis(); useQueue = incrementInMillis < durationInMillis; if (useQueue) { queue = new ArrayDeque<>(); } fromTime = startTime.getAsLong() - incrementInMillis; endTime = fromTime + durationInMillis; } } }; return Stream.of(iter).onClose(new Runnable() { @Override public void run() { s.close(); } }); } }; } /** * * @param duration * @param incrementInMillis * @param startTime * @param collector * @return * @see #window(Duration, LongSupplier) */ public static Function>, Stream> window(final Duration duration, final long incrementInMillis, final LongSupplier startTime, final Collector, A, R> collector) { return new Function>, Stream>() { @Override public Stream apply(final Stream> s) { final ObjIterator iter = new ObjIteratorEx() { private long durationInMillis; private boolean useQueue; private Deque> queue; private Iterator> queueIter; private Supplier supplier; private BiConsumer> accumulator; private Function finisher; private ObjIterator> iter; private Timed next = null; private long fromTime; private long endTime; private boolean initialized = false; @Override public boolean hasNext() { if (initialized == false) { init(); } if (useQueue) { if ((queue.size() > 0 && queue.getLast().timestamp() >= endTime) || (next != null && next.timestamp() - fromTime >= incrementInMillis)) { return true; } else { while (iter.hasNext()) { next = iter.next(); if (next.timestamp() - fromTime >= incrementInMillis) { queue.add(next); return true; } } return false; } } else { while ((next == null || next.timestamp() - fromTime < incrementInMillis) && iter.hasNext()) { next = iter.next(); if (next.timestamp() - fromTime >= incrementInMillis) { break; } } return next != null && next.timestamp() - fromTime >= incrementInMillis; } } @Override public R next() { if (hasNext() == false) { throw new NoSuchElementException(); } fromTime += incrementInMillis; endTime = fromTime + durationInMillis; final A container = supplier.get(); if (useQueue) { queueIter = queue.iterator(); while (queueIter.hasNext()) { next = queueIter.next(); if (next.timestamp() < fromTime) { queueIter.remove(); } else if (next.timestamp() < endTime) { accumulator.accept(container, next); next = null; } else { return finisher.apply(container); } } while (iter.hasNext()) { next = iter.next(); if (next.timestamp() >= fromTime) { queue.add(next); if (next.timestamp() < endTime) { accumulator.accept(container, next); next = null; } else { break; } } } } else { if (next != null) { if (next.timestamp() < fromTime) { // ignore } else if (next.timestamp() < endTime) { accumulator.accept(container, next); } else { return finisher.apply(container); } } while (iter.hasNext()) { next = iter.next(); if (next.timestamp() < fromTime) { // ignore } else if (next.timestamp() < endTime) { accumulator.accept(container, next); } else { break; } } } return finisher.apply(container); } private void init() { if (initialized == false) { initialized = true; N.checkArgNotNull(duration, "duration"); N.checkArgPositive(duration.toMillis(), "duration"); N.checkArgPositive(incrementInMillis, "incrementInMillis"); N.checkArgNotNull(startTime, "startTime"); N.checkArgNotNull(collector, "collector"); durationInMillis = duration.toMillis(); useQueue = incrementInMillis < durationInMillis; if (useQueue) { queue = new ArrayDeque<>(); } supplier = collector.supplier(); accumulator = collector.accumulator(); finisher = collector.finisher(); iter = s.iterator(); fromTime = startTime.getAsLong() - incrementInMillis; endTime = fromTime + durationInMillis; } } }; return Stream.of(iter).onClose(new Runnable() { @Override public void run() { s.close(); } }); } }; } /** * Split this stream by the specified duration. * *
     * 
     * Stream> s = ...;
     * s.__(Fn.window(Duration.ofMinutes(3), () - System.currentTimeMillis()))...// Do your stuffs with Stream>>;
     * 
     * 
* * @param maxWindowSize * @param maxDuration * @param startTime * @return */ public static Function>, Stream>>> window(final int maxWindowSize, final Duration maxDuration, final LongSupplier startTime) { final Function>, Stream>>> mapper = window(maxWindowSize, maxDuration, startTime, Suppliers.> ofList()); return new Function>, Stream>>>() { @Override public Stream>> apply(Stream> t) { return mapper.apply(t).map(new Function>, Stream>>() { @Override public Stream> apply(List> t) { return Stream.of(t); } }); } }; } /** * Split this stream at where {@code maxWindowSize} or {@code maxDuration} reaches first. * * @param maxWindowSize * @param maxDuration * @param startTime * @param collectionSupplier * @return * @see #window(Duration, long, LongSupplier, Supplier) */ public static >> Function>, Stream> window(final int maxWindowSize, final Duration maxDuration, final LongSupplier startTime, final Supplier collectionSupplier) { return new Function>, Stream>() { @Override public Stream apply(final Stream> s) { final ObjIterator iter = new ObjIteratorEx() { private long maxDurationInMillis; private ObjIterator> iter; private Timed next = null; private long fromTime; private long endTime; private boolean initialized = false; @Override public boolean hasNext() { if (initialized == false) { init(); } while ((next == null || next.timestamp() < endTime) && iter.hasNext()) { next = iter.next(); } return next != null && next.timestamp() >= endTime; } @Override public C next() { if (hasNext() == false) { throw new NoSuchElementException(); } fromTime = endTime; endTime = fromTime + maxDurationInMillis; int cnt = 0; final C result = collectionSupplier.get(); if (next != null && next.timestamp() < endTime) { result.add(next); next = null; cnt++; } if (next == null) { while (cnt < maxWindowSize && iter.hasNext()) { next = iter.next(); if (next.timestamp() < endTime) { result.add(next); next = null; cnt++; } else { break; } } } endTime = N.min(endTime, next == null ? System.currentTimeMillis() : next.timestamp()); return result; } private void init() { if (initialized == false) { initialized = true; N.checkArgNotNull(maxDuration, "maxDuration"); N.checkArgPositive(maxDuration.toMillis(), "maxDuration"); N.checkArgPositive(maxWindowSize, "maxWindowSize"); N.checkArgNotNull(startTime, "startTime"); N.checkArgNotNull(collectionSupplier, "collectionSupplier"); iter = s.iterator(); maxDurationInMillis = maxDuration.toMillis(); fromTime = startTime.getAsLong() - maxDurationInMillis; endTime = fromTime + maxDurationInMillis; } } }; return Stream.of(iter).onClose(new Runnable() { @Override public void run() { s.close(); } }); } }; } /** * Split this stream at where {@code maxWindowSize} or {@code maxDuration} reaches first. * * @param maxWindowSize * @param maxDuration * @param startTime * @param collector * @return * @see #window(Duration, long, LongSupplier, Collector) */ public static Function>, Stream> window(final int maxWindowSize, final Duration maxDuration, final LongSupplier startTime, final Collector, A, R> collector) { return new Function>, Stream>() { @Override public Stream apply(final Stream> s) { final ObjIterator iter = new ObjIteratorEx() { private long maxDurationInMillis; private Supplier
supplier; private BiConsumer> accumulator; private Function finisher; private ObjIterator> iter; private Timed next = null; private long fromTime; private long endTime; private boolean initialized = false; @Override public boolean hasNext() { if (initialized == false) { init(); } while ((next == null || next.timestamp() < endTime) && iter.hasNext()) { next = iter.next(); } return next != null && next.timestamp() >= endTime; } @Override public R next() { if (hasNext() == false) { throw new NoSuchElementException(); } fromTime = endTime; endTime = fromTime + maxDurationInMillis; int cnt = 0; final A container = supplier.get(); if (next != null && next.timestamp() < endTime) { accumulator.accept(container, next); next = null; cnt++; } if (next == null) { while (cnt < maxWindowSize && iter.hasNext()) { next = iter.next(); if (next.timestamp() < endTime) { accumulator.accept(container, next); next = null; cnt++; } else { break; } } } endTime = N.min(endTime, next == null ? System.currentTimeMillis() : next.timestamp()); return finisher.apply(container); } private void init() { if (initialized == false) { initialized = true; N.checkArgNotNull(maxDuration, "maxDuration"); N.checkArgPositive(maxDuration.toMillis(), "maxDuration"); N.checkArgPositive(maxWindowSize, "maxWindowSize"); N.checkArgNotNull(startTime, "startTime"); N.checkArgNotNull(collector, "collector"); supplier = collector.supplier(); accumulator = collector.accumulator(); finisher = collector.finisher(); iter = s.iterator(); maxDurationInMillis = maxDuration.toMillis(); fromTime = startTime.getAsLong() - maxDurationInMillis; endTime = fromTime + maxDurationInMillis; } } }; return Stream.of(iter).onClose(new Runnable() { @Override public void run() { s.close(); } }); } }; } public static com.landawn.abacus.util.function.Callable callable(final com.landawn.abacus.util.function.Callable callable) { N.checkArgNotNull(callable); return callable; } public static com.landawn.abacus.util.function.Runnable runnable(final com.landawn.abacus.util.function.Runnable runnable) { N.checkArgNotNull(runnable); return runnable; } 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; } }; } 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(); } }; } public static BinaryOperator throwingMerger() { return BinaryOperators.THROWING_MERGER; } public static BinaryOperator ignoringMerger() { return BinaryOperators.IGNORING_MERGER; } public static BinaryOperator replacingMerger() { return BinaryOperators.REPLACING_MERGER; } /** * * @return * @deprecated replaced by {@code BiConsumers#ofAddAll()} */ @Deprecated static > BiConsumer addAll() { return BiConsumers. ofAddAll(); } /** * * @return * @deprecated replaced by {@code BiConsumers#ofPutAll()} */ @Deprecated static > BiConsumer putAll() { return BiConsumers. ofPutAll(); } public static abstract class Factory { private static final IntFunction BOOLEAN_ARRAY = new IntFunction() { @Override public boolean[] apply(int len) { return new boolean[len]; } }; private static final IntFunction CHAR_ARRAY = new IntFunction() { @Override public char[] apply(int len) { return new char[len]; } }; private static final IntFunction BYTE_ARRAY = new IntFunction() { @Override public byte[] apply(int len) { return new byte[len]; } }; private static final IntFunction SHORT_ARRAY = new IntFunction() { @Override public short[] apply(int len) { return new short[len]; } }; private static final IntFunction INT_ARRAY = new IntFunction() { @Override public int[] apply(int len) { return new int[len]; } }; private static final IntFunction LONG_ARRAY = new IntFunction() { @Override public long[] apply(int len) { return new long[len]; } }; private static final IntFunction FLOAT_ARRAY = new IntFunction() { @Override public float[] apply(int len) { return new float[len]; } }; private static final IntFunction DOUBLE_ARRAY = new IntFunction() { @Override public double[] apply(int len) { return new double[len]; } }; private static final IntFunction STRING_ARRAY = new IntFunction() { @Override public String[] apply(int len) { return new String[len]; } }; private static final IntFunction OBJECT_ARRAY = new IntFunction() { @Override public Object[] apply(int len) { return new Object[len]; } }; private static final IntFunction BOOLEAN_LIST = new IntFunction() { @Override public BooleanList apply(int len) { return new BooleanList(len); } }; private static final IntFunction CHAR_LIST = new IntFunction() { @Override public CharList apply(int len) { return new CharList(len); } }; private static final IntFunction BYTE_LIST = new IntFunction() { @Override public ByteList apply(int len) { return new ByteList(len); } }; private static final IntFunction SHORT_LIST = new IntFunction() { @Override public ShortList apply(int len) { return new ShortList(len); } }; private static final IntFunction INT_LIST = new IntFunction() { @Override public IntList apply(int len) { return new IntList(len); } }; private static final IntFunction LONG_LIST = new IntFunction() { @Override public LongList apply(int len) { return new LongList(len); } }; private static final IntFunction FLOAT_LIST = new IntFunction() { @Override public FloatList apply(int len) { return new FloatList(len); } }; private static final IntFunction DOUBLE_LIST = new IntFunction() { @Override public DoubleList apply(int len) { return new DoubleList(len); } }; @SuppressWarnings("rawtypes") private static final IntFunction LIST_FACTORY = new IntFunction() { @Override public List apply(int len) { return new ArrayList<>(len); } }; @SuppressWarnings("rawtypes") private static final IntFunction LINKED_LIST_FACTORY = new IntFunction() { @Override public LinkedList apply(int len) { return new LinkedList<>(); } }; @SuppressWarnings("rawtypes") private static final IntFunction SET_FACTORY = new IntFunction() { @Override public Set apply(int len) { return new HashSet<>(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_SET_FACTORY = new IntFunction() { @Override public LinkedHashSet apply(int len) { return new LinkedHashSet<>(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction TREE_SET_FACTORY = new IntFunction() { @Override public TreeSet apply(int len) { return new TreeSet<>(); } }; @SuppressWarnings("rawtypes") private static final IntFunction QUEUE_FACTORY = new IntFunction() { @Override public Queue apply(int len) { return new LinkedList(); } }; @SuppressWarnings("rawtypes") private static final IntFunction DEQUE_FACTORY = new IntFunction() { @Override public Deque apply(int len) { return new LinkedList(); } }; @SuppressWarnings("rawtypes") private static final IntFunction ARRAY_DEQUE_FACTORY = new IntFunction() { @Override public ArrayDeque apply(int len) { return new ArrayDeque(len); } }; @SuppressWarnings("rawtypes") private static final IntFunction LINKED_BLOCKING_QUEUE_FACTORY = new IntFunction() { @Override public LinkedBlockingQueue apply(int len) { return new LinkedBlockingQueue(len); } }; @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_LINKED_QUEUE_FACTORY = new IntFunction() { @Override public ConcurrentLinkedQueue apply(int len) { return new ConcurrentLinkedQueue(); } }; @SuppressWarnings("rawtypes") private static final IntFunction PRIORITY_QUEUE_FACTORY = new IntFunction() { @Override public PriorityQueue apply(int len) { return new PriorityQueue(len); } }; @SuppressWarnings("rawtypes") private static final IntFunction MAP_FACTORY = new IntFunction() { @Override public Map apply(int len) { return new HashMap<>(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction LINKED_HASH_MAP_FACTORY = new IntFunction() { @Override public LinkedHashMap apply(int len) { return new LinkedHashMap<>(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction IDENTITY_HASH_MAP_FACTORY = new IntFunction() { @Override public IdentityHashMap apply(int len) { return new IdentityHashMap<>(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction TREE_MAP_FACTORY = new IntFunction() { @Override public TreeMap apply(int len) { return new TreeMap<>(); } }; @SuppressWarnings("rawtypes") private static final IntFunction CONCURRENT_HASH_MAP_FACTORY = new IntFunction() { @Override public ConcurrentHashMap apply(int len) { return new ConcurrentHashMap(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction BI_MAP_FACTORY = new IntFunction() { @Override public BiMap apply(int len) { return new BiMap(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction MULTISET_FACTORY = new IntFunction() { @Override public Multiset apply(int len) { return new Multiset(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction LONG_MULTISET_FACTORY = new IntFunction() { @Override public LongMultiset apply(int len) { return new LongMultiset(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction LIST_MULTIMAP_FACTORY = new IntFunction() { @Override public ListMultimap apply(int len) { return new ListMultimap(N.initHashCapacity(len)); } }; @SuppressWarnings("rawtypes") private static final IntFunction SET_MULTIMAP_FACTORY = new IntFunction() { @Override public SetMultimap apply(int len) { return new SetMultimap(N.initHashCapacity(len)); } }; protected Factory() { // for extention } public static IntFunction ofBooleanArray() { return BOOLEAN_ARRAY; } public static IntFunction ofCharArray() { return CHAR_ARRAY; } public static IntFunction ofByteArray() { return BYTE_ARRAY; } public static IntFunction ofShortArray() { return SHORT_ARRAY; } public static IntFunction ofIntArray() { return INT_ARRAY; } public static IntFunction ofLongArray() { return LONG_ARRAY; } public static IntFunction ofFloatArray() { return FLOAT_ARRAY; } public static IntFunction ofDoubleArray() { return DOUBLE_ARRAY; } public static IntFunction ofStringArray() { return STRING_ARRAY; } public static IntFunction ofObjectArray() { return OBJECT_ARRAY; } public static IntFunction ofBooleanList() { return BOOLEAN_LIST; } public static IntFunction ofCharList() { return CHAR_LIST; } public static IntFunction ofByteList() { return BYTE_LIST; } public static IntFunction ofShortList() { return SHORT_LIST; } public static IntFunction ofIntList() { return INT_LIST; } public static IntFunction ofLongList() { return LONG_LIST; } public static IntFunction ofFloatList() { return FLOAT_LIST; } public static IntFunction ofDoubleList() { return DOUBLE_LIST; } @SuppressWarnings("rawtypes") public static IntFunction> ofList() { return (IntFunction) LIST_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedList() { return (IntFunction) LINKED_LIST_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofSet() { return (IntFunction) SET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedHashSet() { return (IntFunction) LINKED_HASH_SET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofSortedSet() { return (IntFunction) TREE_SET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofNavigableSet() { return (IntFunction) TREE_SET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofTreeSet() { return (IntFunction) TREE_SET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofQueue() { return (IntFunction) QUEUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofDeque() { return (IntFunction) DEQUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofArrayDeque() { return (IntFunction) ARRAY_DEQUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedBlockingQueue() { return (IntFunction) LINKED_BLOCKING_QUEUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentLinkedQueue() { return (IntFunction) CONCURRENT_LINKED_QUEUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofPriorityQueue() { return (IntFunction) PRIORITY_QUEUE_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofMap() { return (IntFunction) MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofLinkedHashMap() { return (IntFunction) LINKED_HASH_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofIdentityHashMap() { return (IntFunction) IDENTITY_HASH_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofSortedMap() { return (IntFunction) TREE_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofNavigableMap() { return (IntFunction) TREE_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofTreeMap() { return (IntFunction) TREE_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentMap() { return (IntFunction) CONCURRENT_HASH_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofConcurrentHashMap() { return (IntFunction) CONCURRENT_HASH_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofBiMap() { return (IntFunction) BI_MAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofMultiset() { return (IntFunction) MULTISET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofLongMultiset() { return (IntFunction) LONG_MULTISET_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofListMultimap() { return (IntFunction) LIST_MULTIMAP_FACTORY; } @SuppressWarnings("rawtypes") public static IntFunction> ofSetMultimap() { return (IntFunction) SET_MULTIMAP_FACTORY; } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static IntFunction> ofImmutableList() { throw new UnsupportedOperationException(); } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static IntFunction> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static IntFunction> ofImmutableMap() { throw new UnsupportedOperationException(); } @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; } }; } } public static abstract class IntFunctions extends Factory { protected IntFunctions() { // for extention. } } public static abstract class Suppliers { private static final Supplier UUID = new Supplier() { @Override public String get() { return N.uuid(); } }; private static final Supplier GUID = new Supplier() { @Override public String get() { return N.guid(); } }; private static final Supplier EMPTY_BOOLEAN_ARRAY = new Supplier() { @Override public boolean[] get() { return N.EMPTY_BOOLEAN_ARRAY; } }; private static final Supplier EMPTY_CHAR_ARRAY = new Supplier() { @Override public char[] get() { return N.EMPTY_CHAR_ARRAY; } }; private static final Supplier EMPTY_BYTE_ARRAY = new Supplier() { @Override public byte[] get() { return N.EMPTY_BYTE_ARRAY; } }; private static final Supplier EMPTY_SHORT_ARRAY = new Supplier() { @Override public short[] get() { return N.EMPTY_SHORT_ARRAY; } }; private static final Supplier EMPTY_INT_ARRAY = new Supplier() { @Override public int[] get() { return N.EMPTY_INT_ARRAY; } }; private static final Supplier EMPTY_LONG_ARRAY = new Supplier() { @Override public long[] get() { return N.EMPTY_LONG_ARRAY; } }; private static final Supplier EMPTY_FLOAT_ARRAY = new Supplier() { @Override public float[] get() { return N.EMPTY_FLOAT_ARRAY; } }; private static final Supplier EMPTY_DOUBLE_ARRAY = new Supplier() { @Override public double[] get() { return N.EMPTY_DOUBLE_ARRAY; } }; private static final Supplier EMPTY_STRING_ARRAY = new Supplier() { @Override public String[] get() { return N.EMPTY_STRING_ARRAY; } }; private static final Supplier EMPTY_OBJECT_ARRAY = new Supplier() { @Override public Object[] get() { return N.EMPTY_OBJECT_ARRAY; } }; private static final Supplier BOOLEAN_LIST = new Supplier() { @Override public BooleanList get() { return new BooleanList(); } }; private static final Supplier CHAR_LIST = new Supplier() { @Override public CharList get() { return new CharList(); } }; private static final Supplier BYTE_LIST = new Supplier() { @Override public ByteList get() { return new ByteList(); } }; private static final Supplier SHORT_LIST = new Supplier() { @Override public ShortList get() { return new ShortList(); } }; private static final Supplier INT_LIST = new Supplier() { @Override public IntList get() { return new IntList(); } }; private static final Supplier LONG_LIST = new Supplier() { @Override public LongList get() { return new LongList(); } }; private static final Supplier FLOAT_LIST = new Supplier() { @Override public FloatList get() { return new FloatList(); } }; private static final Supplier DOUBLE_LIST = new Supplier() { @Override public DoubleList get() { return new DoubleList(); } }; @SuppressWarnings("rawtypes") private static final Supplier LIST = new Supplier() { @Override public List get() { return new ArrayList(); } }; @SuppressWarnings("rawtypes") private static final Supplier LINKED_LIST = new Supplier() { @Override public LinkedList get() { return new LinkedList(); } }; @SuppressWarnings("rawtypes") private static final Supplier SET = new Supplier() { @Override public Set get() { return new HashSet(); } }; @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_SET = new Supplier() { @Override public LinkedHashSet get() { return new LinkedHashSet(); } }; @SuppressWarnings("rawtypes") private static final Supplier TREE_SET = new Supplier() { @Override public TreeSet get() { return new TreeSet(); } }; @SuppressWarnings("rawtypes") private static final Supplier QUEUE = new Supplier() { @Override public Queue get() { return new LinkedList(); } }; @SuppressWarnings("rawtypes") private static final Supplier DEQUE = new Supplier() { @Override public Deque get() { return new LinkedList(); } }; @SuppressWarnings("rawtypes") private static final Supplier ARRAY_DEQUE = new Supplier() { @Override public ArrayDeque get() { return new ArrayDeque(); } }; @SuppressWarnings("rawtypes") private static final Supplier LINKED_BLOCKING_QUEUE = new Supplier() { @Override public LinkedBlockingQueue get() { return new LinkedBlockingQueue(); } }; @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_LINKED_QUEUE = new Supplier() { @Override public ConcurrentLinkedQueue get() { return new ConcurrentLinkedQueue(); } }; @SuppressWarnings("rawtypes") private static final Supplier PRIORITY_QUEUE = new Supplier() { @Override public PriorityQueue get() { return new PriorityQueue(); } }; @SuppressWarnings("rawtypes") private static final Supplier MAP = new Supplier() { @Override public Map get() { return new HashMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier LINKED_HASH_MAP = new Supplier() { @Override public LinkedHashMap get() { return new LinkedHashMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier IDENTITY_HASH_MAP = new Supplier() { @Override public IdentityHashMap get() { return new IdentityHashMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier TREE_MAP = new Supplier() { @Override public TreeMap get() { return new TreeMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier CONCURRENT_HASH_MAP = new Supplier() { @Override public ConcurrentHashMap get() { return new ConcurrentHashMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier BI_MAP = new Supplier() { @Override public BiMap get() { return new BiMap(); } }; @SuppressWarnings("rawtypes") private static final Supplier MULTISET = new Supplier() { @Override public Multiset get() { return new Multiset(); } }; @SuppressWarnings("rawtypes") private static final Supplier LONG_MULTISET = new Supplier() { @Override public LongMultiset get() { return new LongMultiset(); } }; @SuppressWarnings("rawtypes") private static final Supplier LIST_MULTIMAP = new Supplier() { @Override public ListMultimap get() { return N.newListMultimap(); } }; @SuppressWarnings("rawtypes") private static final Supplier SET_MULTIMAP = new Supplier() { @Override public SetMultimap get() { return N.newSetMultimap(); } }; private static final Supplier STRING_BUILDER = new Supplier() { @Override public StringBuilder get() { return new StringBuilder(); } }; protected Suppliers() { // for extention. } /** * Returns a supplier that always supplies {@code instance}. * @param instance. */ public static Supplier ofInstance(final T instance) { return new Supplier() { @Override public T get() { return instance; } }; } public static Supplier ofUUID() { return UUID; } public static Supplier ofGUID() { return GUID; } public static Supplier ofEmptyBooleanArray() { return EMPTY_BOOLEAN_ARRAY; } public static Supplier ofEmptyCharArray() { return EMPTY_CHAR_ARRAY; } public static Supplier ofEmptyByteArray() { return EMPTY_BYTE_ARRAY; } public static Supplier ofEmptyShortArray() { return EMPTY_SHORT_ARRAY; } public static Supplier ofEmptyIntArray() { return EMPTY_INT_ARRAY; } public static Supplier ofEmptyLongArray() { return EMPTY_LONG_ARRAY; } public static Supplier ofEmptyFloatArray() { return EMPTY_FLOAT_ARRAY; } public static Supplier ofEmptyDoubleArray() { return EMPTY_DOUBLE_ARRAY; } public static Supplier ofEmptyStringArray() { return EMPTY_STRING_ARRAY; } public static Supplier ofEmptyObjectArray() { return EMPTY_OBJECT_ARRAY; } public static Supplier ofBooleanList() { return BOOLEAN_LIST; } public static Supplier ofCharList() { return CHAR_LIST; } public static Supplier ofByteList() { return BYTE_LIST; } public static Supplier ofShortList() { return SHORT_LIST; } public static Supplier ofIntList() { return INT_LIST; } public static Supplier ofLongList() { return LONG_LIST; } public static Supplier ofFloatList() { return FLOAT_LIST; } public static Supplier ofDoubleList() { return DOUBLE_LIST; } @SuppressWarnings("rawtypes") public static Supplier> ofList() { return (Supplier) LIST; } @SuppressWarnings("rawtypes") public static Supplier> ofLinkedList() { return (Supplier) LINKED_LIST; } @SuppressWarnings("rawtypes") public static Supplier> ofSet() { return (Supplier) SET; } @SuppressWarnings("rawtypes") public static Supplier> ofLinkedHashSet() { return (Supplier) LINKED_HASH_SET; } @SuppressWarnings("rawtypes") public static Supplier> ofSortedSet() { return (Supplier) TREE_SET; } @SuppressWarnings("rawtypes") public static Supplier> ofNavigableSet() { return (Supplier) TREE_SET; } @SuppressWarnings("rawtypes") public static Supplier> ofTreeSet() { return (Supplier) TREE_SET; } @SuppressWarnings("rawtypes") public static Supplier> ofQueue() { return (Supplier) QUEUE; } @SuppressWarnings("rawtypes") public static Supplier> ofDeque() { return (Supplier) DEQUE; } @SuppressWarnings("rawtypes") public static Supplier> ofArrayDeque() { return (Supplier) ARRAY_DEQUE; } @SuppressWarnings("rawtypes") public static Supplier> ofLinkedBlockingQueue() { return (Supplier) LINKED_BLOCKING_QUEUE; } @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentLinkedQueue() { return (Supplier) CONCURRENT_LINKED_QUEUE; } @SuppressWarnings("rawtypes") public static Supplier> ofPriorityQueue() { return (Supplier) PRIORITY_QUEUE; } @SuppressWarnings("rawtypes") public static Supplier> ofMap() { return (Supplier) MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofLinkedHashMap() { return (Supplier) LINKED_HASH_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofIdentityHashMap() { return (Supplier) IDENTITY_HASH_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofSortedMap() { return (Supplier) TREE_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofNavigableMap() { return (Supplier) TREE_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofTreeMap() { return (Supplier) TREE_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentMap() { return (Supplier) CONCURRENT_HASH_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofConcurrentHashMap() { return (Supplier) CONCURRENT_HASH_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofBiMap() { return (Supplier) BI_MAP; } @SuppressWarnings("rawtypes") public static Supplier> ofMultiset() { return (Supplier) MULTISET; } @SuppressWarnings("rawtypes") public static Supplier> ofLongMultiset() { return (Supplier) LONG_MULTISET; } @SuppressWarnings("rawtypes") public static Supplier> ofListMultimap() { return (Supplier) LIST_MULTIMAP; } @SuppressWarnings("rawtypes") public static Supplier> ofSetMultimap() { return (Supplier) SET_MULTIMAP; } public static Supplier ofStringBuilder() { return STRING_BUILDER; } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static Supplier> ofImmutableList() { throw new UnsupportedOperationException(); } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static Supplier> ofImmutableSet() { throw new UnsupportedOperationException(); } /** * Won't work. * * @return * @throws UnsupportedOperationException * */ @Deprecated public static Supplier> ofImmutableMap() { throw new UnsupportedOperationException(); } @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; } }; } } public static abstract class Predicates { protected Predicates() { // for extention. } 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); } }; } public static Predicate distinct() { return new Predicate() { private final Set set = new HashSet(); @Override public boolean test(T value) { return set.add(value); } }; } public static Predicate distinctBy(final Function mapper) { return new Predicate() { private final Set set = new HashSet(); @Override public boolean test(T value) { return set.add(mapper.apply(value)); } }; } 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; } }; } 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; } }; } /** * Remove the continuous repeat elements. * Returns a stateful predicate which should not be used in parallel stream. * * @return */ 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; } }; } /** * {@code true/false} are repeatedly returned after each specified duration. * * @param periodInMillis * @param cancellationFlag the underline scheduled {@code Task} will be cancelled if {@code cancellationFlag} is set to true. * @return */ public static Predicate invertedByDuration(final long periodInMillis, final MutableBoolean cancellationFlag) { final MutableBoolean switcher = MutableBoolean.of(true); final TimerTask task = new TimerTask() { @Override public void run() { if (cancellationFlag.isTrue()) { this.cancel(); } switcher.invert(); } }; timer.schedule(task, periodInMillis, periodInMillis); return new Predicate() { @Override public boolean test(T t) { return switcher.value(); } }; } /** * {@code true/false} are repeatedly returned after each specified duration. * * @param periodInMillis * @param cancellationFlag the underline scheduled {@code Task} will be cancelled if {@code cancellationFlag} is set to true. * @param update called at the beginning of each duration. * @return */ public static Predicate invertedByDuration(final long periodInMillis, final MutableBoolean cancellationFlag, final Runnable update) { final MutableBoolean switcher = MutableBoolean.of(true); final TimerTask task = new TimerTask() { @Override public void run() { if (cancellationFlag.isTrue()) { this.cancel(); } switcher.invert(); update.run(); } }; timer.schedule(task, periodInMillis, periodInMillis); return new Predicate() { @Override public boolean test(T t) { return switcher.value(); } }; } /** * {@code true/false} are repeatedly returned after each specified duration. * * @param delayInMillis * @param periodInMillis * @param cancellationFlag the underline scheduled {@code Task} will be cancelled if {@code cancellationFlag} is set to true. * @return */ public static Predicate invertedByDuration(final long delayInMillis, final long periodInMillis, final MutableBoolean cancellationFlag) { final MutableBoolean switcher = MutableBoolean.of(true); final TimerTask task = new TimerTask() { @Override public void run() { if (cancellationFlag.isTrue()) { this.cancel(); } switcher.invert(); } }; timer.schedule(task, delayInMillis, periodInMillis); return new Predicate() { @Override public boolean test(T t) { return switcher.value(); } }; } /** * {@code true/false} are repeatedly returned after each specified duration. * * @param delayInMillis * @param periodInMillis * @param cancellationFlag the underline scheduled {@code Task} will be cancelled if {@code cancellationFlag} is set to true. * @param update called at the beginning of each duration. * @return */ public static Predicate invertedByDuration(final long delayInMillis, final long periodInMillis, final MutableBoolean cancellationFlag, final Runnable update) { final MutableBoolean switcher = MutableBoolean.of(true); final TimerTask task = new TimerTask() { @Override public void run() { if (cancellationFlag.isTrue()) { this.cancel(); } switcher.invert(); update.run(); } }; timer.schedule(task, delayInMillis, periodInMillis); return new Predicate() { @Override public boolean test(T t) { return switcher.value(); } }; } } public static abstract class BiPredicates { @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_TRUE = new BiPredicate() { @Override public boolean test(Object t, Object u) { return true; } }; @SuppressWarnings("rawtypes") private static final BiPredicate ALWAYS_FALSE = new BiPredicate() { @Override public boolean test(Object t, Object u) { return false; } }; @SuppressWarnings("rawtypes") private static final BiPredicate EQUAL = new BiPredicate() { @Override public boolean test(Object t, Object u) { return N.equals(t, u); } }; @SuppressWarnings("rawtypes") private static final BiPredicate NOT_EQUAL = new BiPredicate() { @Override public boolean test(Object t, Object u) { return !N.equals(t, u); } }; @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_THAN = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) > 0; } }; @SuppressWarnings("rawtypes") private static final BiPredicate GREATER_EQUAL = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) >= 0; } }; @SuppressWarnings("rawtypes") private static final BiPredicate LESS_THAN = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) < 0; } }; @SuppressWarnings("rawtypes") private static final BiPredicate LESS_EQUAL = new BiPredicate() { @Override public boolean test(Comparable t, Comparable u) { return N.compare(t, u) <= 0; } }; protected BiPredicates() { // for extention. } public static BiPredicate alwaysTrue() { return ALWAYS_TRUE; } public static BiPredicate alwaysFalse() { return ALWAYS_FALSE; } public static BiPredicate indexed(final IndexedBiPredicate predicate) { N.checkArgNotNull(predicate); return new BiPredicate() { private final MutableInt idx = new MutableInt(0); @Override public boolean test(U u, T t) { return predicate.test(u, idx.getAndIncrement(), t); } }; } } public static abstract class TriPredicates { @SuppressWarnings("rawtypes") private static final TriPredicate ALWAYS_TRUE = new TriPredicate() { @Override public boolean test(Object a, Object b, Object c) { return true; } }; @SuppressWarnings("rawtypes") private static final TriPredicate ALWAYS_FALSE = new TriPredicate() { @Override public boolean test(Object a, Object b, Object c) { return false; } }; protected TriPredicates() { // for extention. } public static TriPredicate alwaysTrue() { return ALWAYS_TRUE; } public static TriPredicate alwaysFalse() { return ALWAYS_FALSE; } } public static abstract class Consumers { protected Consumers() { // for extention. } /** * Returns a Consumer which calls the specified func. * * @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); } }; } 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); } }; } } public static abstract class BiConsumers { @SuppressWarnings("rawtypes") private static final BiConsumer DO_NOTHING = new BiConsumer() { @Override public void accept(Object t, Object u) { // do nothing. } }; private static final BiConsumer, Object> ADD = new BiConsumer, Object>() { @Override public void accept(Collection t, Object u) { t.add(u); } }; private static final BiConsumer, Collection> ADD_ALL = new BiConsumer, Collection>() { @Override public void accept(Collection t, Collection u) { t.addAll(u); } }; @SuppressWarnings("rawtypes") private static final BiConsumer ADD_ALL_2 = new BiConsumer() { @Override public void accept(PrimitiveList t, PrimitiveList u) { t.addAll(u); } }; private static final BiConsumer, Object> REMOVE = new BiConsumer, Object>() { @Override public void accept(Collection t, Object u) { t.remove(u); } }; private static final BiConsumer, Collection> REMOVE_ALL = new BiConsumer, Collection>() { @Override public void accept(Collection t, Collection u) { t.removeAll(u); } }; @SuppressWarnings("rawtypes") private static final BiConsumer REMOVE_ALL_2 = new BiConsumer() { @Override public void accept(PrimitiveList t, PrimitiveList u) { t.removeAll(u); } }; 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()); } }; private static final BiConsumer, Map> PUT_ALL = new BiConsumer, Map>() { @Override public void accept(Map t, Map u) { t.putAll(u); } }; private static final BiConsumer, Object> REMOVE_BY_KEY = new BiConsumer, Object>() { @Override public void accept(Map t, Object u) { t.remove(u); } }; private static final BiConsumer MERGE = new BiConsumer() { @Override public void accept(Joiner t, Joiner u) { t.merge(u); } }; private static final BiConsumer APPEND = new BiConsumer() { @Override public void accept(StringBuilder t, Object u) { t.append(u); } }; protected BiConsumers() { // for extention. } public static BiConsumer doNothing() { return DO_NOTHING; } public static > BiConsumer ofAdd() { return (BiConsumer) ADD; } public static > BiConsumer ofAddAll() { return (BiConsumer) ADD_ALL; } @SuppressWarnings("rawtypes") public static BiConsumer ofAddAlll() { return (BiConsumer) ADD_ALL_2; } public static > BiConsumer ofRemove() { return (BiConsumer) REMOVE; } public static > BiConsumer ofRemoveAll() { return (BiConsumer) REMOVE_ALL; } @SuppressWarnings("rawtypes") public static BiConsumer ofRemoveAlll() { return (BiConsumer) REMOVE_ALL_2; } public static , E extends Map.Entry> BiConsumer ofPut() { return (BiConsumer) PUT; } public static > BiConsumer ofPutAll() { return (BiConsumer) PUT_ALL; } public static > BiConsumer ofRemoveByKey() { return (BiConsumer) REMOVE_BY_KEY; } public static BiConsumer ofMerge() { return MERGE; } public static BiConsumer ofAppend() { return (BiConsumer) APPEND; } /** * Returns a BiConsumer which calls the specified func. * * @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); } }; } 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); } }; } } public static abstract class TriConsumers { protected TriConsumers() { // for extention. } /** * Returns a TriConsumer which calls the specified func. * * @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); } }; } } public static abstract class Functions { protected Functions() { // for extention. } /** * @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; } }; } 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); } }; } /** * * @return * @deprecated replaced by PairFn#toList(); */ @Deprecated public static Function, List> pairToList() { return Pairs.toList(); } /** * * @return * @deprecated replaced by PairFn#toSet(); */ @Deprecated public static Function, Set> pairToSet() { return Pairs.toSet(); } /** * * @return * @deprecated replaced by TripleFn#toList(); */ @Deprecated public static Function, List> tripleToList() { return Triples.toList(); } /** * * @deprecated replaced by TripleFn#toSet(); */ @Deprecated public static Function, Set> tripleToSet() { return Triples.toSet(); } } public static abstract class BiFunctions { private static final BiFunction RETURN_FIRST = new BiFunction() { @Override public Object apply(Object t, Object u) { return t; } }; private static final BiFunction RETURN_SECOND = new BiFunction() { @Override public Object apply(Object t, Object u) { return u; } }; private static final BiFunction, Object, Collection> ADD = new BiFunction, Object, Collection>() { @Override public Collection apply(Collection t, Object u) { t.add(u); return t; } }; 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; } }; @SuppressWarnings("rawtypes") private static final BiFunction ADD_ALL_2 = new BiFunction() { @Override public PrimitiveList apply(PrimitiveList t, PrimitiveList u) { t.addAll(u); return t; } }; private static final BiFunction, Object, Collection> REMOVE = new BiFunction, Object, Collection>() { @Override public Collection apply(Collection t, Object u) { t.remove(u); return t; } }; 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; } }; @SuppressWarnings("rawtypes") private static final BiFunction REMOVE_ALL_2 = new BiFunction() { @Override public PrimitiveList apply(PrimitiveList t, PrimitiveList u) { t.removeAll(u); return t; } }; 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; } }; 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; } }; 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; } }; private static final BiFunction MERGE = new BiFunction() { @Override public Joiner apply(Joiner t, Joiner u) { return t.merge(u); } }; private static final BiFunction APPEND = new BiFunction() { @Override public StringBuilder apply(StringBuilder t, Object u) { return t.append(u); } }; protected BiFunctions() { // for extention. } public static BiFunction returnFirst() { return (BiFunction) RETURN_FIRST; } public static BiFunction returnSecond() { return (BiFunction) RETURN_SECOND; } public static > BiFunction ofAdd() { return (BiFunction) ADD; } public static > BiFunction ofAddAll() { return (BiFunction) ADD_ALL; } @SuppressWarnings("rawtypes") public static BiFunction ofAddAlll() { return (BiFunction) ADD_ALL_2; } public static > BiFunction ofRemove() { return (BiFunction) REMOVE; } public static > BiFunction ofRemoveAll() { return (BiFunction) REMOVE_ALL; } @SuppressWarnings("rawtypes") public static BiFunction ofRemoveAlll() { return (BiFunction) REMOVE_ALL_2; } public static , E extends Map.Entry> BiFunction ofPut() { return (BiFunction) PUT; } public static > BiFunction ofPutAll() { return (BiFunction) PUT_ALL; } public static , U> BiFunction ofRemoveByKey() { return (BiFunction) REMOVE_BY_KEY; } public static BiFunction ofMerge() { return MERGE; } public static BiFunction ofAppend() { return (BiFunction) APPEND; } /** * @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; } }; } 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); } }; } } public static abstract class TriFunctions { protected TriFunctions() { // for extention. } /** * @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; } }; } } public static abstract class BinaryOperators { @SuppressWarnings("rawtypes") private static final BinaryOperator THROWING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { throw new IllegalStateException(String.format("Duplicate key (attempted merging values %s and %s)", t, u)); } }; @SuppressWarnings("rawtypes") private static final BinaryOperator IGNORING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { return t; } }; @SuppressWarnings("rawtypes") private static final BinaryOperator REPLACING_MERGER = new BinaryOperator() { @Override public Object apply(Object t, Object u) { return u; } }; private static final BinaryOperator> ADD_ALL_TO_FIRST = new BinaryOperator>() { @Override public Collection apply(Collection t, Collection u) { t.addAll(u); return t; } }; 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; } } }; private static final BinaryOperator> REMOVE_ALL_FROM_FIRST = new BinaryOperator>() { @Override public Collection apply(Collection t, Collection u) { t.removeAll(u); return t; } }; private static final BinaryOperator> PUT_ALL_TO_FIRST = new BinaryOperator>() { @Override public Map apply(Map t, Map u) { t.putAll(u); return t; } }; 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; } } }; private static final BinaryOperator MERGE_TO_FIRST = new BinaryOperator() { @Override public Joiner apply(Joiner t, Joiner u) { return t.merge(u); } }; 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); } } }; private static final BinaryOperator APPEND_TO_FIRST = new BinaryOperator() { @Override public StringBuilder apply(StringBuilder t, StringBuilder u) { return t.append(u); } }; 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); } } }; private static final BinaryOperator CONCAT = new BinaryOperator() { @Override public String apply(String t, String u) { return t + u; } }; private static final BinaryOperator ADD_INTEGER = new BinaryOperator() { @Override public Integer apply(Integer t, Integer u) { return t.intValue() + u.intValue(); } }; private static final BinaryOperator ADD_LONG = new BinaryOperator() { @Override public Long apply(Long t, Long u) { return t.longValue() + u.longValue(); } }; private static final BinaryOperator ADD_DOUBLE = new BinaryOperator() { @Override public Double apply(Double t, Double u) { return t.doubleValue() + u.doubleValue(); } }; private static final BinaryOperator ADD_BIG_INTEGER = new BinaryOperator() { @Override public BigInteger apply(BigInteger t, BigInteger u) { return t.add(u); } }; private static final BinaryOperator ADD_BIG_DECIMAL = new BinaryOperator() { @Override public BigDecimal apply(BigDecimal t, BigDecimal u) { return t.add(u); } }; @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; } }; @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; } }; protected BinaryOperators() { // for extention. } /** * * @return * @deprecated replaced by {@code #ofAddAllToFirst()} */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAll() { return (BinaryOperator) ADD_ALL_TO_FIRST; } @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAllToFirst() { return (BinaryOperator) ADD_ALL_TO_FIRST; } @SuppressWarnings("unchecked") public static > BinaryOperator ofAddAllToBigger() { return (BinaryOperator) ADD_ALL_TO_BIGGER; } /** * * @return * @deprecated replaced by {@code #ofRemoveAllFromFirst()}. */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofRemoveAll() { return (BinaryOperator) REMOVE_ALL_FROM_FIRST; } @SuppressWarnings("unchecked") public static > BinaryOperator ofRemoveAllFromFirst() { return (BinaryOperator) REMOVE_ALL_FROM_FIRST; } /** * * @return * @deprecated replaced by {@code #ofPutAllToFirst()} */ @Deprecated @SuppressWarnings("unchecked") public static > BinaryOperator ofPutAll() { return (BinaryOperator) PUT_ALL_TO_FIRST; } @SuppressWarnings("unchecked") public static > BinaryOperator ofPutAllToFirst() { return (BinaryOperator) PUT_ALL_TO_FIRST; } @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; } public static BinaryOperator ofMergeToFirst() { return MERGE_TO_FIRST; } public static BinaryOperator ofMergeToBigger() { return MERGE_TO_BIGGER; } /** * * @return * @deprecated replaced by {@code #ofAppendToFirst()} */ @Deprecated public static BinaryOperator ofAppend() { return APPEND_TO_FIRST; } public static BinaryOperator ofAppendToFirst() { return APPEND_TO_FIRST; } public static BinaryOperator ofAppendToBigger() { return APPEND_TO_BIGGER; } public static BinaryOperator ofConcat() { return CONCAT; } public static BinaryOperator ofAddInt() { return ADD_INTEGER; } public static BinaryOperator ofAddLong() { return ADD_LONG; } public static BinaryOperator ofAddDouble() { return ADD_DOUBLE; } public static BinaryOperator ofAddBigInteger() { return ADD_BIG_INTEGER; } public static BinaryOperator ofAddBigDecimal() { return ADD_BIG_DECIMAL; } @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator min() { return (BinaryOperator) MIN; } @SuppressWarnings({ "unchecked", "rawtypes" }) public static > BinaryOperator max() { return (BinaryOperator) MAX; } public static BinaryOperator minBy(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; } }; } public static BinaryOperator maxBy(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; } }; } } public static abstract class UnaryOperators { @SuppressWarnings("rawtypes") private static final UnaryOperator IDENTITY = new UnaryOperator() { @Override public Object apply(Object t) { return t; } }; protected UnaryOperators() { // for extention. } public static UnaryOperator identity() { return IDENTITY; } } public static abstract class Entries { protected Entries() { // for extention. } 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()); } }; } 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()); } }; } 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()); } }; } public static Try.Function, T, E> ef(final Try.BiFunction f) { N.checkArgNotNull(f, "BiFunction"); return new Try.Function, T, E>() { @Override public T apply(Entry e) throws E { return f.apply(e.getKey(), e.getValue()); } }; } public static Try.Predicate, E> ep(final Try.BiPredicate p) { N.checkArgNotNull(p, "BiPredicate"); return new Try.Predicate, E>() { @Override public boolean test(Entry e) throws E { return p.test(e.getKey(), e.getValue()); } }; } public static Try.Consumer, E> ec(final Try.BiConsumer c) { N.checkArgNotNull(c, "BiConsumer"); return new Try.Consumer, E>() { @Override public void accept(Entry e) throws E { c.accept(e.getKey(), e.getValue()); } }; } public static Function, T> ff(final Try.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); } } }; } public static Predicate> pp(final Try.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); } } }; } public static Consumer> cc(final Try.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); } } }; } } public static abstract class Pairs { @SuppressWarnings("rawtypes") private static final Function PAIR_TO_LIST = new Function() { @Override public List apply(Pair t) { return N.asList(t.left, t.right); } }; @SuppressWarnings("rawtypes") private static final Function PAIR_TO_SET = new Function() { @Override public Set apply(Pair t) { return N.asSet(t.left, t.right); } }; protected Pairs() { // for extention. } @SuppressWarnings("rawtypes") public static Function, List> toList() { return (Function) PAIR_TO_LIST; } @SuppressWarnings("rawtypes") public static Function, Set> toSet() { return (Function) PAIR_TO_SET; } } public static abstract class Triples { @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); } }; @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); } }; protected Triples() { // for extention. } @SuppressWarnings("rawtypes") public static Function, List> toList() { return (Function) TRIPLE_TO_LIST; } @SuppressWarnings("rawtypes") public static Function, Set> toSet() { return (Function) TRIPLE_TO_SET; } } public static final class Disposables { @SuppressWarnings("rawtypes") private static final Function CLONE = new Function() { @Override public Object[] apply(DisposableArray t) { return t.clone(); } }; @SuppressWarnings("rawtypes") private static final Function TO_STRING = new Function() { @Override public String apply(DisposableArray t) { return t.toString(); } }; private Disposables() { // singleton. } @SuppressWarnings("rawtypes") public static > Function cloneArray() { return (Function) CLONE; } @SuppressWarnings("rawtypes") public static Function toStr() { return (Function) TO_STRING; } @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 abstract class FnC { private static final CharPredicate POSITIVE = new CharPredicate() { @Override public boolean test(char t) { return t > 0; } }; private static final CharPredicate NOT_NEGATIVE = new CharPredicate() { @Override public boolean test(char t) { return t >= 0; } }; private static final CharBiPredicate EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t == u; } }; private static final CharBiPredicate NOT_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t != u; } }; private static final CharBiPredicate GREATER_THAN = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t > u; } }; private static final CharBiPredicate GREATER_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t >= u; } }; private static final CharBiPredicate LESS_THAN = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t < u; } }; private static final CharBiPredicate LESS_EQUAL = new CharBiPredicate() { @Override public boolean test(char t, char u) { return t <= u; } }; protected FnC() { // for extention. } public static CharPredicate positve() { return POSITIVE; } public static CharPredicate notNegative() { return NOT_NEGATIVE; } public static CharBiPredicate equal() { return EQUAL; } public static CharBiPredicate notEqual() { return NOT_EQUAL; } public static CharBiPredicate greaterThan() { return GREATER_THAN; } public static CharBiPredicate greaterEqual() { return GREATER_EQUAL; } public static CharBiPredicate lessThan() { return LESS_THAN; } public static CharBiPredicate lessEqual() { return LESS_EQUAL; } public static CharBiFunction alternate() { return new CharBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(char t, char u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code BytePredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnB { private static final BytePredicate POSITIVE = new BytePredicate() { @Override public boolean test(byte t) { return t > 0; } }; private static final BytePredicate NOT_NEGATIVE = new BytePredicate() { @Override public boolean test(byte t) { return t >= 0; } }; private static final ByteBiPredicate EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t == u; } }; private static final ByteBiPredicate NOT_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t != u; } }; private static final ByteBiPredicate GREATER_THAN = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t > u; } }; private static final ByteBiPredicate GREATER_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t >= u; } }; private static final ByteBiPredicate LESS_THAN = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t < u; } }; private static final ByteBiPredicate LESS_EQUAL = new ByteBiPredicate() { @Override public boolean test(byte t, byte u) { return t <= u; } }; protected FnB() { // for extention. } public static BytePredicate positve() { return POSITIVE; } public static BytePredicate notNegative() { return NOT_NEGATIVE; } public static ByteBiPredicate equal() { return EQUAL; } public static ByteBiPredicate notEqual() { return NOT_EQUAL; } public static ByteBiPredicate greaterThan() { return GREATER_THAN; } public static ByteBiPredicate greaterEqual() { return GREATER_EQUAL; } public static ByteBiPredicate lessThan() { return LESS_THAN; } public static ByteBiPredicate lessEqual() { return LESS_EQUAL; } public static ByteBiFunction alternate() { return new ByteBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(byte t, byte u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code ShortPredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnS { private static final ShortPredicate POSITIVE = new ShortPredicate() { @Override public boolean test(short t) { return t > 0; } }; private static final ShortPredicate NOT_NEGATIVE = new ShortPredicate() { @Override public boolean test(short t) { return t >= 0; } }; private static final ShortBiPredicate EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t == u; } }; private static final ShortBiPredicate NOT_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t != u; } }; private static final ShortBiPredicate GREATER_THAN = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t > u; } }; private static final ShortBiPredicate GREATER_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t >= u; } }; private static final ShortBiPredicate LESS_THAN = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t < u; } }; private static final ShortBiPredicate LESS_EQUAL = new ShortBiPredicate() { @Override public boolean test(short t, short u) { return t <= u; } }; protected FnS() { // for extention. } public static ShortPredicate positve() { return POSITIVE; } public static ShortPredicate notNegative() { return NOT_NEGATIVE; } public static ShortBiPredicate equal() { return EQUAL; } public static ShortBiPredicate notEqual() { return NOT_EQUAL; } public static ShortBiPredicate greaterThan() { return GREATER_THAN; } public static ShortBiPredicate greaterEqual() { return GREATER_EQUAL; } public static ShortBiPredicate lessThan() { return LESS_THAN; } public static ShortBiPredicate lessEqual() { return LESS_EQUAL; } public static ShortBiFunction alternate() { return new ShortBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(short t, short u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code IntPredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnI { private static final IntPredicate POSITIVE = new IntPredicate() { @Override public boolean test(int t) { return t > 0; } }; private static final IntPredicate NOT_NEGATIVE = new IntPredicate() { @Override public boolean test(int t) { return t >= 0; } }; private static final IntBiPredicate EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t == u; } }; private static final IntBiPredicate NOT_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t != u; } }; private static final IntBiPredicate GREATER_THAN = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t > u; } }; private static final IntBiPredicate GREATER_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t >= u; } }; private static final IntBiPredicate LESS_THAN = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t < u; } }; private static final IntBiPredicate LESS_EQUAL = new IntBiPredicate() { @Override public boolean test(int t, int u) { return t <= u; } }; protected FnI() { // for extention. } public static IntPredicate positve() { return POSITIVE; } public static IntPredicate notNegative() { return NOT_NEGATIVE; } public static IntBiPredicate equal() { return EQUAL; } public static IntBiPredicate notEqual() { return NOT_EQUAL; } public static IntBiPredicate greaterThan() { return GREATER_THAN; } public static IntBiPredicate greaterEqual() { return GREATER_EQUAL; } public static IntBiPredicate lessThan() { return LESS_THAN; } public static IntBiPredicate lessEqual() { return LESS_EQUAL; } public static IntBiFunction alternate() { return new IntBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(int t, int u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code LongPredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnL { private static final LongPredicate POSITIVE = new LongPredicate() { @Override public boolean test(long t) { return t > 0; } }; private static final LongPredicate NOT_NEGATIVE = new LongPredicate() { @Override public boolean test(long t) { return t >= 0; } }; private static final LongBiPredicate EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t == u; } }; private static final LongBiPredicate NOT_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t != u; } }; private static final LongBiPredicate GREATER_THAN = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t > u; } }; private static final LongBiPredicate GREATER_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t >= u; } }; private static final LongBiPredicate LESS_THAN = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t < u; } }; private static final LongBiPredicate LESS_EQUAL = new LongBiPredicate() { @Override public boolean test(long t, long u) { return t <= u; } }; protected FnL() { // for extention. } public static LongPredicate positve() { return POSITIVE; } public static LongPredicate notNegative() { return NOT_NEGATIVE; } public static LongBiPredicate equal() { return EQUAL; } public static LongBiPredicate notEqual() { return NOT_EQUAL; } public static LongBiPredicate greaterThan() { return GREATER_THAN; } public static LongBiPredicate greaterEqual() { return GREATER_EQUAL; } public static LongBiPredicate lessThan() { return LESS_THAN; } public static LongBiPredicate lessEqual() { return LESS_EQUAL; } public static LongBiFunction alternate() { return new LongBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(long t, long u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code FloatPredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnF { private static final FloatPredicate POSITIVE = new FloatPredicate() { @Override public boolean test(float t) { return t > 0; } }; private static final FloatPredicate NOT_NEGATIVE = new FloatPredicate() { @Override public boolean test(float t) { return t >= 0; } }; private static final FloatBiPredicate EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t == u; } }; private static final FloatBiPredicate NOT_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t != u; } }; private static final FloatBiPredicate GREATER_THAN = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t > u; } }; private static final FloatBiPredicate GREATER_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t >= u; } }; private static final FloatBiPredicate LESS_THAN = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t < u; } }; private static final FloatBiPredicate LESS_EQUAL = new FloatBiPredicate() { @Override public boolean test(float t, float u) { return t <= u; } }; protected FnF() { // for extention. } public static FloatPredicate positve() { return POSITIVE; } public static FloatPredicate notNegative() { return NOT_NEGATIVE; } public static FloatBiPredicate equal() { return EQUAL; } public static FloatBiPredicate notEqual() { return NOT_EQUAL; } public static FloatBiPredicate greaterThan() { return GREATER_THAN; } public static FloatBiPredicate greaterEqual() { return GREATER_EQUAL; } public static FloatBiPredicate lessThan() { return LESS_THAN; } public static FloatBiPredicate lessEqual() { return LESS_EQUAL; } public static FloatBiFunction alternate() { return new FloatBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(float t, float u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for {@code DoublePredicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FnD { private static final DoublePredicate POSITIVE = new DoublePredicate() { @Override public boolean test(double t) { return t > 0; } }; private static final DoublePredicate NOT_NEGATIVE = new DoublePredicate() { @Override public boolean test(double t) { return t >= 0; } }; private static final DoubleBiPredicate EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.equals(t, u); } }; private static final DoubleBiPredicate NOT_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) != 0; } }; private static final DoubleBiPredicate GREATER_THAN = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) > 0; } }; private static final DoubleBiPredicate GREATER_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) >= 0; } }; private static final DoubleBiPredicate LESS_THAN = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) < 0; } }; private static final DoubleBiPredicate LESS_EQUAL = new DoubleBiPredicate() { @Override public boolean test(double t, double u) { return N.compare(t, u) <= 0; } }; protected FnD() { // for extention. } public static DoublePredicate positve() { return POSITIVE; } public static DoublePredicate notNegative() { return NOT_NEGATIVE; } public static DoubleBiPredicate equal() { return EQUAL; } public static DoubleBiPredicate notEqual() { return NOT_EQUAL; } public static DoubleBiPredicate greaterThan() { return GREATER_THAN; } public static DoubleBiPredicate greaterEqual() { return GREATER_EQUAL; } public static DoubleBiPredicate lessThan() { return LESS_THAN; } public static DoubleBiPredicate lessEqual() { return LESS_EQUAL; } public static DoubleBiFunction alternate() { return new DoubleBiFunction() { private final MutableBoolean flag = MutableBoolean.of(true); @Override public Nth apply(double t, double u) { return flag.getAndInvert() ? Nth.FIRST : Nth.SECOND; } }; } } /** * Utility class for primitive {@code Predicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class Fnn { protected Fnn() { // for extention } public static ToByteFunction unboxB() { return ToByteFunction.UNBOX; } public static ToCharFunction unboxC() { return ToCharFunction.UNBOX; } public static ToShortFunction unboxS() { return ToShortFunction.UNBOX; } public static ToIntFunction unboxI() { return ToIntFunction.UNBOX; } public static ToLongFunction unboxL() { return ToLongFunction.UNBOX; } public static ToFloatFunction unboxF() { return ToFloatFunction.UNBOX; } public static ToDoubleFunction unboxD() { return ToDoubleFunction.UNBOX; } public static CharPredicate cp(final CharPredicate p) { N.checkArgNotNull(p); return p; } public static BytePredicate bp(final BytePredicate p) { N.checkArgNotNull(p); return p; } public static ShortPredicate sp(final ShortPredicate p) { N.checkArgNotNull(p); return p; } public static IntPredicate ip(final IntPredicate p) { N.checkArgNotNull(p); return p; } public static LongPredicate lp(final LongPredicate p) { N.checkArgNotNull(p); return p; } public static FloatPredicate fp(final FloatPredicate p) { N.checkArgNotNull(p); return p; } public static DoublePredicate dp(final DoublePredicate p) { N.checkArgNotNull(p); return p; } public static CharFunction cf(final CharFunction f) { N.checkArgNotNull(f); return f; } public static ByteFunction bf(final ByteFunction f) { N.checkArgNotNull(f); return f; } public static ShortFunction sf(final ShortFunction f) { N.checkArgNotNull(f); return f; } public static IntFunction iF(final IntFunction f) { N.checkArgNotNull(f); return f; } public static LongFunction lf(final LongFunction f) { N.checkArgNotNull(f); return f; } public static FloatFunction ff(final FloatFunction f) { N.checkArgNotNull(f); return f; } public static DoubleFunction df(final DoubleFunction f) { N.checkArgNotNull(f); return f; } public static CharConsumer cc(final CharConsumer c) { N.checkArgNotNull(c); return c; } public static ByteConsumer bc(final ByteConsumer c) { N.checkArgNotNull(c); return c; } public static ShortConsumer sc(final ShortConsumer c) { N.checkArgNotNull(c); return c; } public static IntConsumer ic(final IntConsumer c) { N.checkArgNotNull(c); return c; } public static LongConsumer lc(final LongConsumer c) { N.checkArgNotNull(c); return c; } public static FloatConsumer fc(final FloatConsumer c) { N.checkArgNotNull(c); return c; } public static DoubleConsumer dc(final DoubleConsumer c) { N.checkArgNotNull(c); return c; } } /** * Utility class for exceptional {@code Predicate/Function/Consumer}. * * @author haiyangl * */ public static abstract class FN { protected FN() { // for extention } public static Try.Function identity() { return Fn.IDENTITY; } public static Try.Runnable emptyAction() { return (Try.Runnable) Fn.EMPTY_ACTION; } public static Try.Consumer doNothing() { return Fn.DO_NOTHING; } public static Try.Consumer sleep(final long millis) { return new Try.Consumer() { @Override public void accept(T t) { N.sleep(millis); } }; } public static Try.Consumer sleepUninterruptibly(final long millis) { return new Try.Consumer() { @Override public void accept(T t) throws E { N.sleepUninterruptibly(millis); } }; } // public static Try.Consumer close() { // return (Try.Consumer) Fn.CLOSE; // } public static Try.Consumer closeQuietly() { return (Try.Consumer) Fn.CLOSE_QUIETLY; } public static Try.Consumer println() { return Fn.PRINTLN; } public static Try.BiConsumer println(final String separator) { return cc(Fn. println(separator)); } @Beta public static Try.Predicate pp(final Predicate predicate) { N.checkArgNotNull(predicate); return (Try.Predicate) predicate; } @Beta public static Try.Predicate pp(final A a, final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return new Try.Predicate() { @Override public boolean test(T t) { return biPredicate.test(a, t); } }; } @Beta public static Try.Predicate pp(final A a, final B b, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Try.Predicate() { @Override public boolean test(T t) { return triPredicate.test(a, b, t); } }; } @Beta public static Try.BiPredicate pp(final BiPredicate biPredicate) { N.checkArgNotNull(biPredicate); return (Try.BiPredicate) biPredicate; } @Beta public static Try.BiPredicate pp(final A a, final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return new Try.BiPredicate() { @Override public boolean test(T t, U u) { return triPredicate.test(a, t, u); } }; } @Beta public static Try.TriPredicate pp(final TriPredicate triPredicate) { N.checkArgNotNull(triPredicate); return (Try.TriPredicate) triPredicate; } @Beta public static Try.Consumer cc(final Consumer consumer) { N.checkArgNotNull(consumer); return (Try.Consumer) consumer; } @Beta public static Try.Consumer cc(final A a, final BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return new Try.Consumer() { @Override public void accept(T t) { biConsumer.accept(a, t); } }; } @Beta public static Try.Consumer cc(final A a, final B b, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Try.Consumer() { @Override public void accept(T t) { triConsumer.accept(a, b, t); } }; } @Beta public static Try.BiConsumer cc(final BiConsumer biConsumer) { N.checkArgNotNull(biConsumer); return (Try.BiConsumer) biConsumer; } @Beta public static Try.BiConsumer cc(final A a, final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return new Try.BiConsumer() { @Override public void accept(T t, U u) { triConsumer.accept(a, t, u); } }; } @Beta public static Try.TriConsumer cc(final TriConsumer triConsumer) { N.checkArgNotNull(triConsumer); return (Try.TriConsumer) triConsumer; } @Beta public static Try.Function ff(final Function function) { N.checkArgNotNull(function); return (Try.Function) function; } @Beta public static Try.Function ff(final A a, final BiFunction biFunction) { N.checkArgNotNull(biFunction); return new Try.Function() { @Override public R apply(T t) { return biFunction.apply(a, t); } }; } @Beta public static Try.Function ff(final A a, final B b, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Try.Function() { @Override public R apply(T t) { try { return triFunction.apply(a, b, t); } catch (Exception e) { throw N.toRuntimeException(e); } } }; } @Beta public static Try.BiFunction ff(final BiFunction biFunction) { N.checkArgNotNull(biFunction); return (Try.BiFunction) biFunction; } @Beta public static Try.BiFunction ff(final A a, final TriFunction triFunction) { N.checkArgNotNull(triFunction); return new Try.BiFunction() { @Override public R apply(T t, U u) { return triFunction.apply(a, t, u); } }; } @Beta public static Try.TriFunction ff(final TriFunction triFunction) { N.checkArgNotNull(triFunction); return (Try.TriFunction) triFunction; } public static Try.BinaryOperator throwingMerger() { return BinaryOperators.THROWING_MERGER; } public static Try.BinaryOperator ignoringMerger() { return BinaryOperators.IGNORING_MERGER; } public static Try.BinaryOperator replacingMerger() { return BinaryOperators.REPLACING_MERGER; } public static Try.Callable callable(final Try.Callable callable) { N.checkArgNotNull(callable); return callable; } public static Try.Runnable runnable(final Try.Runnable runnable) { N.checkArgNotNull(runnable); return runnable; } public static Try.Callable toCallable(final Try.Runnable runnable) { N.checkArgNotNull(runnable); return new Try.Callable() { @Override public Void call() throws E { runnable.run(); return null; } }; } public static Try.Runnable toRunnable(final Try.Callable callable) { N.checkArgNotNull(callable); return new Try.Runnable() { @Override public void run() throws E { callable.call(); } }; } } }