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
Go to download
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2017, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.util;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.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.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableArray;
import com.landawn.abacus.util.Tuple.Tuple1;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.Tuple.Tuple4;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.ByteBiPredicate;
import com.landawn.abacus.util.function.ByteConsumer;
import com.landawn.abacus.util.function.ByteFunction;
import com.landawn.abacus.util.function.BytePredicate;
import com.landawn.abacus.util.function.CharBiPredicate;
import com.landawn.abacus.util.function.CharConsumer;
import com.landawn.abacus.util.function.CharFunction;
import com.landawn.abacus.util.function.CharPredicate;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.DoubleBiPredicate;
import com.landawn.abacus.util.function.DoubleConsumer;
import com.landawn.abacus.util.function.DoubleFunction;
import com.landawn.abacus.util.function.DoublePredicate;
import com.landawn.abacus.util.function.FloatBiPredicate;
import com.landawn.abacus.util.function.FloatConsumer;
import com.landawn.abacus.util.function.FloatFunction;
import com.landawn.abacus.util.function.FloatPredicate;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IndexedBiConsumer;
import com.landawn.abacus.util.function.IndexedBiFunction;
import com.landawn.abacus.util.function.IndexedBiPredicate;
import com.landawn.abacus.util.function.IndexedConsumer;
import com.landawn.abacus.util.function.IndexedFunction;
import com.landawn.abacus.util.function.IndexedPredicate;
import com.landawn.abacus.util.function.IntBiPredicate;
import com.landawn.abacus.util.function.IntConsumer;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.IntPredicate;
import com.landawn.abacus.util.function.LongBiPredicate;
import com.landawn.abacus.util.function.LongConsumer;
import com.landawn.abacus.util.function.LongFunction;
import com.landawn.abacus.util.function.LongPredicate;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.QuadFunction;
import com.landawn.abacus.util.function.ShortBiPredicate;
import com.landawn.abacus.util.function.ShortConsumer;
import com.landawn.abacus.util.function.ShortFunction;
import com.landawn.abacus.util.function.ShortPredicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.TriConsumer;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
import com.landawn.abacus.util.function.UnaryOperator;
// TODO: Auto-generated Javadoc
/**
* 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 {
/** The Constant NONE. */
private static final Object NONE = new Object();
/** The Constant timer. */
private static final Timer timer = new Timer();
/** The Constant FACTORY_OF_MAP. */
@SuppressWarnings("rawtypes")
public static final IntFunction> FACTORY_OF_MAP = (IntFunction) Factory.MAP_FACTORY;
/** The Constant FACTORY_OF_LINKED_HASH_MAP. */
@SuppressWarnings("rawtypes")
public static final IntFunction> FACTORY_OF_LINKED_HASH_MAP = (IntFunction) Factory.LINKED_HASH_MAP_FACTORY;
/** The Constant SUPPLIER_OF_MAP. */
@SuppressWarnings("rawtypes")
public static final Supplier> SUPPLIER_OF_MAP = (Supplier) Suppliers.MAP;
/** The Constant SUPPLIER_OF_LINKED_HASH_MAP. */
@SuppressWarnings("rawtypes")
public static final Supplier> SUPPLIER_OF_LINKED_HASH_MAP = (Supplier) Suppliers.LINKED_HASH_MAP;
/** The Constant EMPTY_ACTION. */
private static final com.landawn.abacus.util.function.Runnable EMPTY_ACTION = new com.landawn.abacus.util.function.Runnable() {
@Override
public void run() {
}
};
/** The Constant DO_NOTHING. */
@SuppressWarnings("rawtypes")
private static final Consumer DO_NOTHING = new Consumer() {
@Override
public void accept(Object value) {
// do nothing.
}
};
/** The Constant CLOSE. */
private static final Consumer CLOSE = new Consumer() {
@Override
public void accept(AutoCloseable value) {
IOUtil.close(value);
}
};
/** The Constant CLOSE_QUIETLY. */
private static final Consumer CLOSE_QUIETLY = new Consumer() {
@Override
public void accept(AutoCloseable value) {
IOUtil.closeQuietly(value);
}
};
/** The Constant PRINTLN_EQUAL. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_EQUAL = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), "=", N.toString(value)));
}
};
/** The Constant PRINTLN_HYPHEN. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_HYPHEN = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), "-", N.toString(value)));
}
};
/** The Constant PRINTLN_UNDERSCORE. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_UNDERSCORE = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), "_", N.toString(value)));
}
};
/** The Constant PRINTLN_COLON. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_COLON = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), ":", N.toString(value)));
}
};
/** The Constant PRINTLN_COLON_SPACE. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_COLON_SPACE = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), ": ", N.toString(value)));
}
};
/** The Constant PRINTLN_COMMA. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_COMMA = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), ",", N.toString(value)));
}
};
/** The Constant PRINTLN_COMMA_SPACE. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_COMMA_SPACE = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), ", ", N.toString(value)));
}
};
/** The Constant PRINTLN_EMPTY. */
@SuppressWarnings("rawtypes")
private static final BiConsumer PRINTLN_EMPTY = new BiConsumer() {
@Override
public void accept(Object key, Object value) {
N.println(StringUtil.concat(N.toString(key), N.toString(value)));
}
};
/** The Constant PRINTLN. */
@SuppressWarnings("rawtypes")
private static final Consumer PRINTLN = new Consumer() {
@Override
public void accept(Object value) {
N.println(value);
}
};
/** The Constant TO_STRING. */
@SuppressWarnings("rawtypes")
private static final Function TO_STRING = new Function() {
@Override
public Object apply(Object t) {
return N.toString(t);
}
};
/** The Constant TO_CAMEL_CASE. */
private static final Function TO_CAMEL_CASE = new Function() {
@Override
public String apply(String t) {
return StringUtil.toCamelCase(t);
}
};
/** The Constant TO_LOWER_CASE. */
private static final Function TO_LOWER_CASE = new Function() {
@Override
public String apply(String t) {
return StringUtil.toLowerCase(t);
}
};
/** The Constant TO_LOWER_CASE_WITH_UNDERSCORE. */
private static final Function TO_LOWER_CASE_WITH_UNDERSCORE = new Function() {
@Override
public String apply(String t) {
return StringUtil.toLowerCaseWithUnderscore(t);
}
};
/** The Constant TO_UPPER_CASE. */
private static final Function TO_UPPER_CASE = new Function() {
@Override
public String apply(String t) {
return StringUtil.toUpperCase(t);
}
};
/** The Constant TO_UPPER_CASE_WITH_UNDERSCORE. */
private static final Function TO_UPPER_CASE_WITH_UNDERSCORE = new Function() {
@Override
public String apply(String t) {
return StringUtil.toUpperCaseWithUnderscore(t);
}
};
/** The Constant COMPARE. */
@SuppressWarnings("rawtypes")
private static final BiFunction COMPARE = new BiFunction() {
@Override
public Integer apply(Comparable a, Comparable b) {
return N.compare(a, b);
}
};
/** The Constant IDENTITY. */
@SuppressWarnings("rawtypes")
private static final Function IDENTITY = new Function() {
@Override
public Object apply(Object t) {
return t;
}
};
/** The Constant TRIM. */
private static final Function TRIM = new Function() {
@Override
public String apply(String t) {
return t == null ? null : t.trim();
}
};
/** The Constant TRIM_TO_EMPTY. */
private static final Function TRIM_TO_EMPTY = new Function() {
@Override
public String apply(String t) {
return t == null ? "" : t.trim();
}
};
/** The Constant TRIM_TO_NULL. */
private static final Function TRIM_TO_NULL = new Function() {
@Override
public String apply(String t) {
if (t == null || (t = t.trim()).length() == 0) {
return null;
}
return t;
}
};
/** The Constant NULL_TO_EMPTY. */
private static final Function NULL_TO_EMPTY = new Function() {
@Override
public String apply(String t) {
return t == null ? N.EMPTY_STRING : t;
}
};
/** The Constant NULL_TO_EMPTY_L. */
@SuppressWarnings("rawtypes")
private static final Function NULL_TO_EMPTY_LIST = new Function() {
@Override
public List apply(List t) {
return t == null ? N.emptyList() : t;
}
};
/** The Constant NULL_TO_EMPTY_S. */
@SuppressWarnings("rawtypes")
private static final Function NULL_TO_EMPTY_SET = new Function() {
@Override
public Set apply(Set t) {
return t == null ? N.emptySet() : t;
}
};
/** The Constant NULL_TO_EMPTY_M. */
@SuppressWarnings("rawtypes")
private static final Function NULL_TO_EMPTY_MAP = new Function() {
@Override
public Map apply(Map t) {
return t == null ? N.emptyMap() : t;
}
};
/** The Constant LENGTH. */
private static final Function LENGTH = new Function() {
@Override
public Integer apply(CharSequence t) {
return t == null ? 0 : t.length();
}
};
/** The Constant LEN. */
private static final Function LEN = new Function() {
@Override
public Integer apply(Object[] t) {
return t == null ? 0 : t.length;
}
};
/** The Constant SIZE. */
@SuppressWarnings("rawtypes")
private static final Function SIZE = new Function() {
@Override
public Integer apply(Collection t) {
return t == null ? 0 : t.size();
}
};
/** The Constant SIZE_M. */
@SuppressWarnings("rawtypes")
private static final Function SIZE_MAP = new Function() {
@Override
public Integer apply(Map t) {
return t == null ? 0 : t.size();
}
};
/** The Constant KEY. */
private static final Function, Object> KEY = new Function, Object>() {
@Override
public Object apply(Map.Entry t) {
return t.getKey();
}
};
/** The Constant VALUE. */
private static final Function, Object> VALUE = new Function, Object>() {
@Override
public Object apply(Map.Entry t) {
return t.getValue();
}
};
/** The Constant INVERSE. */
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());
}
};
/** The Constant ENTRY. */
private static final BiFunction> ENTRY = new BiFunction>() {
@Override
public Map.Entry apply(Object key, Object value) {
return new SimpleImmutableEntry<>(key, value);
}
};
/** The Constant PAIR. */
private static final BiFunction> PAIR = new BiFunction>() {
@Override
public Pair apply(Object key, Object value) {
return Pair.of(key, value);
}
};
/** The Constant TRIPLE. */
private static final TriFunction> TRIPLE = new TriFunction>() {
@Override
public Triple apply(Object a, Object b, Object c) {
return Triple.of(a, b, c);
}
};
/** The Constant TUPLE_1. */
private static final Function> TUPLE_1 = new Function>() {
@Override
public Tuple1 apply(Object t) {
return Tuple.of(t);
}
};
/** The Constant TUPLE_2. */
private static final BiFunction> TUPLE_2 = new BiFunction>() {
@Override
public Tuple2 apply(Object t, Object u) {
return Tuple.of(t, u);
}
};
/** The Constant TUPLE_3. */
private static final TriFunction> TUPLE_3 = new TriFunction>() {
@Override
public Tuple3 apply(Object a, Object b, Object c) {
return Tuple.of(a, b, c);
}
};
/** The Constant TUPLE_4. */
private static final QuadFunction> TUPLE_4 = new QuadFunction>() {
@Override
public Tuple4 apply(Object a, Object b, Object c, Object d) {
return Tuple.of(a, b, c, d);
}
};
/** The Constant ALWAYS_TRUE. */
@SuppressWarnings("rawtypes")
private static final Predicate ALWAYS_TRUE = new Predicate() {
@Override
public boolean test(Object value) {
return true;
}
};
/** The Constant ALWAYS_FALSE. */
@SuppressWarnings("rawtypes")
private static final Predicate ALWAYS_FALSE = new Predicate() {
@Override
public boolean test(Object value) {
return false;
}
};
/** The Constant IS_NULL. */
@SuppressWarnings("rawtypes")
private static final Predicate IS_NULL = new Predicate() {
@Override
public boolean test(Object value) {
return value == null;
}
};
/** The Constant IS_NULL_OR_EMPTY. */
private static final Predicate IS_NULL_OR_EMPTY = new Predicate() {
@Override
public boolean test(CharSequence value) {
return value == null || value.length() == 0;
}
};
/** The Constant IS_NULL_OR_EMPTY_OR_BLANK. */
private static final Predicate IS_NULL_OR_EMPTY_OR_BLANK = new Predicate() {
@Override
public boolean test(CharSequence value) {
return N.isNullOrEmptyOrBlank(value);
}
};
/** The Constant NOT_NULL. */
@SuppressWarnings("rawtypes")
private static final Predicate NOT_NULL = new Predicate() {
@Override
public boolean test(Object value) {
return value != null;
}
};
/** The Constant NOT_NULL_OR_EMPTY. */
private static final Predicate NOT_NULL_OR_EMPTY = new Predicate() {
@Override
public boolean test(CharSequence value) {
return value != null && value.length() > 0;
}
};
/** The Constant NOT_NULL_OR_EMPTY_OR_BLANK. */
private static final Predicate NOT_NULL_OR_EMPTY_OR_BLANK = new Predicate() {
@Override
public boolean test(CharSequence value) {
return N.notNullOrEmptyOrBlank(value);
}
};
/** The Constant IS_FILE. */
private static final Predicate IS_FILE = new Predicate() {
@Override
public boolean test(File value) {
return value.isFile();
}
};
/** The Constant IS_DIRECTORY. */
private static final Predicate IS_DIRECTORY = new Predicate() {
@Override
public boolean test(File value) {
return value.isDirectory();
}
};
/**
* Instantiates a new fn.
*/
protected Fn() {
super();
// for extention.
}
/**
*
* @param
* @param supplier
* @return
*/
public static T get(final Supplier supplier) {
return supplier.get();
}
/**
* Returns a {@code Supplier} which returns a single instance created by calling the specified {@code supplier.get()}.
*
* @param
* @param supplier
* @return
*/
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;
}
};
}
/**
*
* @param
* @param
* @param func
* @return
*/
public static Function memoize(final Function super T, ? extends R> func) {
return new Function() {
private volatile R resultForNull = (R) NONE;
private volatile Map resultMap = null;
@Override
public R apply(T t) {
R result = null;
if (t == null) {
result = resultForNull;
if (result == NONE) {
synchronized (this) {
if (resultForNull == NONE) {
resultForNull = func.apply(t);
}
result = resultForNull;
}
}
} else {
synchronized (this) {
if (resultMap == null) {
resultMap = new HashMap<>();
}
result = resultMap.get(t);
if (result == null && resultMap.containsKey(t) == false) {
result = func.apply(t);
resultMap.put(t, result);
}
}
}
return result;
}
};
}
/**
* Only for temporary use in sequential stream/single thread, not for parallel stream/multiple threads.
* The returned Collection will clean up before it's returned every time when {@code get} is called.
* Don't save the returned Collection object or use it to save objects.
*
* @param
* @param
* @param supplier
* @return
* @see {@code Stream.split/sliding};
* @deprecated
*/
@Deprecated
@Beta
@SequentialOnly
public static > Supplier extends C> reuse(final Supplier extends C> supplier) {
return new Supplier() {
private C c;
@Override
public C get() {
if (c == null) {
c = supplier.get();
} else if (c.size() > 0) {
c.clear();
}
return c;
}
};
}
/**
* Only for temporary use in sequential stream/single thread, not for parallel stream/multiple threads.
* The returned Collection will clean up before it's returned every time when {@code get} is called.
* Don't save the returned Collection object or use it to save objects.
*
* @param
* @param
* @param supplier
* @return
* @see {@code Stream.split/sliding};
* @deprecated
*/
@Deprecated
@Beta
@SequentialOnly
public static > IntFunction extends C> reuse(final IntFunction extends C> supplier) {
return new IntFunction() {
private C c;
@Override
public C apply(int size) {
if (c == null) {
c = supplier.apply(size);
} else if (c.size() > 0) {
c.clear();
}
return c;
}
};
}
/**
*
* @param closeable
* @return
*/
public static com.landawn.abacus.util.function.Runnable close(final AutoCloseable closeable) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.close(closeable);
}
};
}
/**
*
* @param a
* @return
*/
@SafeVarargs
public static com.landawn.abacus.util.function.Runnable closeAll(final AutoCloseable... a) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.closeAll(a);
}
};
}
/**
*
* @param c
* @return
*/
public static com.landawn.abacus.util.function.Runnable closeAll(final Collection extends AutoCloseable> c) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.closeAll(c);
}
};
}
/**
*
* @param closeable
* @return
*/
public static com.landawn.abacus.util.function.Runnable closeQuietly(final AutoCloseable closeable) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.closeQuietly(closeable);
}
};
}
/**
* Close all quietly.
*
* @param a
* @return
*/
@SafeVarargs
public static com.landawn.abacus.util.function.Runnable closeAllQuietly(final AutoCloseable... a) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.closeAllQuietly(a);
}
};
}
/**
* Close all quietly.
*
* @param c
* @return
*/
public static com.landawn.abacus.util.function.Runnable closeAllQuietly(final Collection extends AutoCloseable> c) {
return new com.landawn.abacus.util.function.Runnable() {
private volatile boolean isClosed = false;
@Override
public void run() {
if (isClosed) {
return;
}
isClosed = true;
IOUtil.closeAllQuietly(c);
}
};
}
/**
*
* @return
*/
public static com.landawn.abacus.util.function.Runnable emptyAction() {
return EMPTY_ACTION;
}
/**
*
* @param
* @return
*/
public static Consumer doNothing() {
return DO_NOTHING;
}
/**
*
* @param
* @param errorMessage
* @return
*/
public static Consumer throwRuntimeException(final String errorMessage) {
return new Consumer() {
@Override
public void accept(T t) {
throw new RuntimeException(errorMessage);
}
};
}
/**
*
* @param
* @param excpetionSupplier
* @return
*/
public static Consumer throwException(final Supplier extends RuntimeException> excpetionSupplier) {
return new Consumer() {
@Override
public void accept(T t) {
throw excpetionSupplier.get();
}
};
}
/**
*
* @param
* @return
*/
public static Consumer close() {
return (Consumer) CLOSE;
}
/**
*
* @param
* @return
*/
public static Consumer closeQuietly() {
return (Consumer) CLOSE_QUIETLY;
}
/**
*
* @param
* @param millis
* @return
*/
public static Consumer sleep(final long millis) {
return new Consumer() {
@Override
public void accept(T t) {
N.sleep(millis);
}
};
}
/**
*
* @param
* @param millis
* @return
*/
public static Consumer sleepUninterruptibly(final long millis) {
return new Consumer() {
@Override
public void accept(T t) {
N.sleepUninterruptibly(millis);
}
};
}
/**
*
* @param
* @return
*/
public static Consumer println() {
return PRINTLN;
}
/**
*
* @param
* @param
* @param separator
* @return
*/
public static BiConsumer println(final String separator) {
N.checkArgNotNull(separator);
switch (separator) {
case "=":
return PRINTLN_EQUAL;
case ":":
return PRINTLN_COLON;
case ": ":
return PRINTLN_COLON_SPACE;
case "-":
return PRINTLN_HYPHEN;
case "_":
return PRINTLN_UNDERSCORE;
case ",":
return PRINTLN_COMMA;
case ", ":
return PRINTLN_COMMA_SPACE;
case "":
return PRINTLN_EMPTY;
default:
return new BiConsumer() {
@Override
public void accept(T t, U u) {
N.println(t + separator + u);
}
};
}
}
/**
*
* @param
* @return
*/
public static Function toStr() {
return TO_STRING;
}
/**
* To camel case.
*
* @return
*/
public static Function toCamelCase() {
return TO_CAMEL_CASE;
}
/**
* To lower case.
*
* @return
*/
public static Function toLowerCase() {
return TO_LOWER_CASE;
}
/**
* To lower case with underscore.
*
* @return
*/
public static Function toLowerCaseWithUnderscore() {
return TO_LOWER_CASE_WITH_UNDERSCORE;
}
/**
* To upper case.
*
* @return
*/
public static Function toUpperCase() {
return TO_UPPER_CASE;
}
/**
* To upper case with underscore.
*
* @return
*/
public static Function toUpperCaseWithUnderscore() {
return TO_UPPER_CASE_WITH_UNDERSCORE;
}
/**
*
* @param
* @return
*/
public static Function identity() {
return IDENTITY;
}
/**
*
* @param the key type
* @param
* @param keyMapper
* @return
*/
public static Function> keyed(final Function super T, K> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Function>() {
@Override
public Keyed apply(T t) {
return Keyed.of(keyMapper.apply(t), t);
}
};
}
/** The Constant VAL. */
private static final Function, Object> VAL = new Function, Object>() {
@Override
public Object apply(Keyed, Object> t) {
return t.val();
}
};
/** The Constant KK. */
private static final Function, Object>, Object> KK = new Function, Object>, Object>() {
@Override
public Object apply(Map.Entry, Object> t) {
return t.getKey().val();
}
};
/**
*
* @param the key type
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, T> val() {
return (Function) VAL;
}
/**
*
* @param
* @param the key type
* @param the value type
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, V>, T> kk() {
return (Function) KK;
}
/** The Constant WRAP. */
private static final Function> WRAP = new Function>() {
@Override
public Wrapper apply(Object t) {
return Wrapper.of(t);
}
};
/**
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Function> wrap() {
return (Function) WRAP;
}
/**
*
* @param
* @param hashFunction
* @param equalsFunction
* @return
*/
public static Function> wrap(final ToIntFunction super T> hashFunction, final BiPredicate super T, ? super T> equalsFunction) {
N.checkArgNotNull(hashFunction);
N.checkArgNotNull(equalsFunction);
return new Function>() {
@Override
public Wrapper apply(T t) {
return Wrapper.of(t, hashFunction, equalsFunction);
}
};
}
/** The Constant UNWRAP. */
private static final Function, Object> UNWRAP = new Function, Object>() {
@Override
public Object apply(Wrapper t) {
return t.value();
}
};
/**
*
* @param the key type
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, T> unwrap() {
return (Function) UNWRAP;
}
/**
*
* @param the key type
* @param the value type
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, K> key() {
return (Function) KEY;
}
/**
*
* @param the key type
* @param the value type
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, V> value() {
return (Function) VALUE;
}
/**
*
* @param the key type
* @param the value type
* @return
*/
@SuppressWarnings("rawtypes")
public static Function, Entry> inverse() {
return (Function) INVERSE;
}
/**
*
* @param the key type
* @param the value type
* @return
*/
@SuppressWarnings("rawtypes")
public static BiFunction> entry() {
return (BiFunction) ENTRY;
}
/**
*
* @param the key type
* @param
* @param key
* @return
*/
public static Function> entry(final K key) {
return new Function>() {
@Override
public Entry apply(T t) {
return new SimpleImmutableEntry<>(key, t);
}
};
}
/**
*
* @param the key type
* @param
* @param keyMapper
* @return
*/
public static Function> entry(final Function super T, K> keyMapper) {
N.checkArgNotNull(keyMapper);
return new Function>() {
@Override
public Entry apply(T t) {
return new SimpleImmutableEntry<>(keyMapper.apply(t), t);
}
};
}
/**
*
* @param
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static BiFunction> pair() {
return (BiFunction) PAIR;
}
/**
*
* @param
* @param
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static TriFunction> triple() {
return (TriFunction) TRIPLE;
}
/**
*
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static Function> tuple1() {
return (Function) TUPLE_1;
}
/**
*
* @param
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static BiFunction> tuple2() {
return (BiFunction) TUPLE_2;
}
/**
*
* @param
* @param
* @param
* @return
*/
@SuppressWarnings("rawtypes")
public static TriFunction > tuple3() {
return (TriFunction) TUPLE_3;
}
/**
*
* @param
* @param
* @param
* @param
* @return
*/
@SuppressWarnings({ "rawtypes" })
public static QuadFunction > tuple4() {
return (QuadFunction) TUPLE_4;
}
/**
*
* @return
*/
public static Function trim() {
return TRIM;
}
/**
* Trim to empty.
*
* @return
*/
public static Function trimToEmpty() {
return TRIM_TO_EMPTY;
}
/**
* Trim to null.
*
* @return
*/
public static Function trimToNull() {
return TRIM_TO_NULL;
}
/**
*
* @return
*/
public static Function nullToEmpty() {
return NULL_TO_EMPTY;
}
/**
*
* @param
* @return
* @deprecated replaced by {@code nullToEmptyList}
*/
@Deprecated
@SuppressWarnings("rawtypes")
public static