Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.Fn Maven / Gradle / Ivy
/*
* 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.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.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.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
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.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
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.Consumer;
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.IntFunction;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.QuadFunction;
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.Collectors;
import com.landawn.abacus.util.stream.Stream;
/**
* Factory utility class for functional interfaces.
*
*
*
*
* Map map = N.asMap("a", 1, "b", 2, "c", 3);
* // Instead of
* Stream.of(map).filter(e -> e.getKey().equals("a") || e.getKey().equals("b")).toMap(e -> e.getKey(), e -> e.getValue());
* // Using Fn
* Stream.of(map).filter(Fn.testByKey(k -> k.equals("a") || k.equals("b"))).collect(Collectors.toMap());
*
*
*
*
*
* @author haiyang li
*
*/
public final class Fn {
@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(N.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(N.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(N.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(N.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(N.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(N.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(N.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(N.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 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, 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 BiFunction> ENTRY = new BiFunction>() {
// @Override
// public Map.Entry apply(Object key, Object value) {
// return new AbstractMap.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);
}
};
private static final ToIntFunction NUM_TO_INT_FUNC = new ToIntFunction() {
@Override
public int applyAsInt(Number value) {
return value == null ? 0 : value.intValue();
}
};
private static final ToLongFunction NUM_TO_LONG_FUNC = new ToLongFunction() {
@Override
public long applyAsLong(Number value) {
return value == null ? 0 : value.longValue();
}
};
private static final ToDoubleFunction NUM_TO_DOUBLE_FUNC = new ToDoubleFunction() {
@Override
public double applyAsDouble(Number value) {
return value == null ? 0d : value.doubleValue();
}
};
private static final Object NULL = new Object();
private Fn() {
// Singleton.
}
public static T get(final Supplier supplier) {
return supplier.get();
}
public static Comparator naturalOrder() {
return Comparators.naturalOrder();
}
public static Comparator reversedOrder() {
return Comparators.reversedOrder();
}
public static Comparator reversedOrder(final Comparator cmp) {
return Comparators.reversedOrder(cmp);
}
@SuppressWarnings("rawtypes")
public static Comparator comparingBy(final Function super T, ? extends U> keyExtractor) {
return Comparators.comparingBy(keyExtractor);
}
@SuppressWarnings("rawtypes")
public static Comparator reversedComparingBy(final Function super T, ? extends U> keyExtractor) {
return Comparators.reversedComparingBy(keyExtractor);
}
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);
}
};
}
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);
}
};
}
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 println() {
return PRINTLN;
}
public static BiConsumer println(final String separator) {
N.requireNonNull(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 identity() {
return IDENTITY;
}
public static Function> keyed(final Function super T, K> keyExtractor) {
return new Function>() {
@Override
public Keyed apply(T t) {
return Keyed.of(keyExtractor.apply(t), t);
}
};
}
public static Function> wrap() {
return new Function>() {
@Override
public Wrapper apply(T t) {
return Wrapper.of(t);
}
};
}
public static Function> wrap(final ToIntFunction super T> hashFunction, final BiPredicate super T, ? super T> equalsFunction) {
return new Function>() {
@Override
public Wrapper apply(T t) {
return Wrapper.of(t, hashFunction, equalsFunction);
}
};
}
@SuppressWarnings("rawtypes")
public static Function, K> key() {
return (Function) KEY;
}
@SuppressWarnings("rawtypes")
public static Function, V> value() {
return (Function) VALUE;
}
@SuppressWarnings("rawtypes")
public static BiFunction> entry() {
// return (BiFunction) ENTRY;
return (BiFunction) PAIR;
}
@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 cast(final Class 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;
}
};
}
public static Predicate in(final Collection> c) {
return new Predicate() {
@Override
public boolean test(T value) {
return c.contains(value);
}
};
}
public static Predicate notIn(final Collection> c) {
return new Predicate() {
@Override
public boolean test(T value) {
return !c.contains(value);
}
};
}
public static Predicate instanceOf(final Class> clazz) {
return new Predicate() {
@Override
public boolean test(T value) {
return clazz.isInstance(value);
}
};
}
@SuppressWarnings("rawtypes")
public static Predicate subtypeOf(final Class> clazz) {
return new Predicate() {
@Override
public boolean test(Class value) {
return clazz.isAssignableFrom(value);
}
};
}
public static Predicate startsWith(final String prefix) {
return new Predicate() {
@Override
public boolean test(String value) {
return value.startsWith(prefix);
}
};
}
public static Predicate endsWith(final String suffix) {
return new Predicate() {
@Override
public boolean test(String value) {
return value.endsWith(suffix);
}
};
}
public static Predicate matches(final Pattern 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 BooleanSupplier and(final BooleanSupplier first, final BooleanSupplier 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) {
return new BooleanSupplier() {
@Override
public boolean getAsBoolean() {
return first.getAsBoolean() && second.getAsBoolean() && third.getAsBoolean();
}
};
}
public static Predicate and(final Predicate super T> first, final Predicate super T> second) {
return new Predicate() {
@Override
public boolean test(T t) {
return first.test(t) && second.test(t);
}
};
}
public static Predicate and(final Predicate super T> first, final Predicate super T> second, final Predicate super T> 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) {
return new Predicate() {
@Override
public boolean test(T t) {
for (Predicate super T> p : c) {
if (p.test(t) == false) {
return false;
}
}
return true;
}
};
}
public static BiPredicate and(final BiPredicate super T, ? super U> first, final BiPredicate super T, ? super U> 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 super T, ? super U> first, final BiPredicate super T, ? super U> second,
final BiPredicate super T, ? super U> 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) {
return new BiPredicate() {
@Override
public boolean test(T t, U u) {
for (BiPredicate super T, ? super U> p : c) {
if (p.test(t, u) == false) {
return false;
}
}
return true;
}
};
}
public static BooleanSupplier or(final BooleanSupplier first, final BooleanSupplier 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) {
return new BooleanSupplier() {
@Override
public boolean getAsBoolean() {
return first.getAsBoolean() || second.getAsBoolean() || third.getAsBoolean();
}
};
}
public static Predicate or(final Predicate super T> first, final Predicate super T> second) {
return new Predicate() {
@Override
public boolean test(T t) {
return first.test(t) || second.test(t);
}
};
}
public static Predicate or(final Predicate super T> first, final Predicate super T> second, final Predicate super T> 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) {
return new Predicate() {
@Override
public boolean test(T t) {
for (Predicate super T> p : c) {
if (p.test(t)) {
return true;
}
}
return false;
}
};
}
public static BiPredicate or(final BiPredicate super T, ? super U> first, final BiPredicate super T, ? super U> 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 super T, ? super U> first, final BiPredicate super T, ? super U> second,
final BiPredicate super T, ? super U> 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) {
return new BiPredicate() {
@Override
public boolean test(T t, U u) {
for (BiPredicate super T, ? super U> p : c) {
if (p.test(t, u)) {
return true;
}
}
return false;
}
};
}
public static Predicate> testByKey(final Predicate super K> predicate) {
return new Predicate>() {
@Override
public boolean test(Entry entry) {
return predicate.test(entry.getKey());
}
};
}
public static Predicate> testByValue(final Predicate super V> predicate) {
return new Predicate>() {
@Override
public boolean test(Entry entry) {
return predicate.test(entry.getValue());
}
};
}
public static Consumer> acceptByKey(final Consumer super K> consumer) {
return new Consumer>() {
@Override
public void accept(Entry entry) {
consumer.accept(entry.getKey());
}
};
}
public static Consumer> acceptByValue(final Consumer super V> consumer) {
return new Consumer>() {
@Override
public void accept(Entry entry) {
consumer.accept(entry.getValue());
}
};
}
public static Function, R> applyByKey(final Function super K, R> func) {
return new Function, R>() {
@Override
public R apply(Entry entry) {
return func.apply(entry.getKey());
}
};
}
public static Function, R> applyByValue(final Function super V, R> func) {
return new Function