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.annimon.stream.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.annimon.stream;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
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.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import com.annimon.stream.function.BiConsumer;
import com.annimon.stream.function.BiFunction;
import com.annimon.stream.function.BiPredicate;
import com.annimon.stream.function.BinaryOperator;
import com.annimon.stream.function.Consumer;
import com.annimon.stream.function.Function;
import com.annimon.stream.function.IntFunction;
import com.annimon.stream.function.Predicate;
import com.annimon.stream.function.Supplier;
import com.annimon.stream.function.ToDoubleFunction;
import com.annimon.stream.function.ToIntFunction;
import com.annimon.stream.function.ToLongFunction;
/**
* Factory utility class for functional interfaces.
*
* @author haiyang li
*
* @since 0.8.3
*/
@SuppressWarnings("unchecked")
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 Runnable EMPTY_ACTION = new Runnable() {
@Override
public void run() {
}
};
@SuppressWarnings("rawtypes")
private static final Consumer DO_NOTHING = new Consumer() {
@Override
public void accept(Object value) {
// do nothing.
}
};
@SuppressWarnings("rawtypes")
private static final Consumer PRINTLN = new Consumer() {
@Override
public void accept(Object value) {
System.out.println(value);
}
};
@SuppressWarnings("rawtypes")
private static final Function IDENTITY = new Function() {
@Override
public Object apply(Object t) {
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 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;
}
};
@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;
}
};
@SuppressWarnings("rawtypes")
private static final Predicate NOT_NULL = new Predicate() {
@Override
public boolean test(Object value) {
return value != null;
}
};
@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 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 Runnable emptyAction() {
return EMPTY_ACTION;
}
public static Consumer doNothing() {
return DO_NOTHING;
}
public static Consumer println() {
return PRINTLN;
}
public static BiConsumer println(final String separator) {
return new BiConsumer() {
@Override
public void accept(T t, U u) {
System.out.println(t + separator + u);
}
};
}
public static Function identity() {
return IDENTITY;
}
@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;
}
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 notNull() {
return NOT_NULL;
}
public static Predicate equal(final Object target) {
return new Predicate() {
@Override
public boolean test(T value) {
return value == target || (value != null && value.equals(target));
}
};
}
public static Predicate notEqual(final Object target) {
return new Predicate() {
@Override
public boolean test(T value) {
return !(value == target || (value != null && value.equals(target)));
}
};
}
@SuppressWarnings("rawtypes")
public static Predicate greaterThan(final T target) {
return new Predicate() {
@Override
public boolean test(T value) {
return Objects.compare(value, target) > 0;
}
};
}
@SuppressWarnings("rawtypes")
public static Predicate greaterEqual(final T target) {
return new Predicate() {
@Override
public boolean test(T value) {
return Objects.compare(value, target) >= 0;
}
};
}
@SuppressWarnings("rawtypes")
public static Predicate lessThan(final T target) {
return new Predicate() {
@Override
public boolean test(T value) {
return Objects.compare(value, target) < 0;
}
};
}
@SuppressWarnings("rawtypes")
public static Predicate lessEqual(final T target) {
return new Predicate() {
@Override
public boolean test(T value) {
return Objects.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 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, R>() {
@Override
public R apply(Entry entry) {
return func.apply(entry.getValue());
}
};
}
public static Function, Map.Entry> mapKey(final Function super K, KK> func) {
return new Function, Map.Entry>() {
@Override
public Map.Entry apply(Entry entry) {
return new AbstractMap.SimpleImmutableEntry<>(func.apply(entry.getKey()), entry.getValue());
}
};
}
public static Function, Map.Entry> mapValue(final Function super V, VV> func) {
return new Function, Map.Entry>() {
@Override
public Map.Entry apply(Entry entry) {
return new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), func.apply(entry.getValue()));
}
};
}
public static BinaryOperator throwingMerger() {
return THROWING_MERGER;
}
public static BinaryOperator ignoringMerger() {
return IGNORING_MERGER;
}
public static BinaryOperator replacingMerger() {
return REPLACING_MERGER;
}
/**
*
* @param predicate
* @param limit
* @return
*/
public static Predicate limited(final Predicate predicate, final int limit) {
Objects.requireNonNull(predicate);
return new Predicate() {
private final AtomicInteger counter = new AtomicInteger(limit);
@Override
public boolean test(T t) {
return predicate.test(t) && counter.decrementAndGet() >= 0;
}
};
}
/**
*
* @param predicate
* @param limit
* @return
*/
public static BiPredicate limited(final BiPredicate predicate, final int limit) {
Objects.requireNonNull(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.decrementAndGet() >= 0;
}
};
}
public static Collector> toList() {
return Collectors.toList();
}
public static Collector> toSet() {
return Collectors.toSet();
}
public static Collector counting() {
return Collectors.counting();
}
public static Collector summingInt(final ToIntFunction super T> mapper) {
return Collectors.summingInt(mapper);
}
public static Collector summingLong(final ToLongFunction super T> mapper) {
return Collectors.summingLong(mapper);
}
public static Collector summingDouble(final ToDoubleFunction super T> mapper) {
return Collectors.summingDouble(mapper);
}
public static Collector averagingInt(final ToIntFunction super T> mapper) {
return Collectors.averagingInt(mapper);
}
public static Collector averagingLong(final ToLongFunction super T> mapper) {
return Collectors.averagingLong(mapper);
}
public static Collector averagingDouble(final ToDoubleFunction super T> mapper) {
return Collectors.averagingDouble(mapper);
}
public static final 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];
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super List> LIST_FACTORY = new IntFunction() {
@Override
public List apply(int len) {
return new ArrayList<>(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super LinkedList> LINKED_LIST_FACTORY = new IntFunction() {
@Override
public LinkedList apply(int len) {
return new LinkedList<>();
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super Set> SET_FACTORY = new IntFunction() {
@Override
public Set apply(int len) {
return new HashSet<>(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super LinkedHashSet> LINKED_HASH_SET_FACTORY = new IntFunction() {
@Override
public LinkedHashSet apply(int len) {
return new LinkedHashSet<>(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super TreeSet> TREE_SET_FACTORY = new IntFunction() {
@Override
public TreeSet apply(int len) {
return new TreeSet<>();
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super Map> MAP_FACTORY = new IntFunction() {
@Override
public Map apply(int len) {
return new HashMap<>(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super LinkedHashMap> LINKED_HASH_MAP_FACTORY = new IntFunction() {
@Override
public LinkedHashMap apply(int len) {
return new LinkedHashMap<>(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super TreeMap> TREE_MAP_FACTORY = new IntFunction() {
@Override
public TreeMap apply(int len) {
return new TreeMap<>();
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super ConcurrentHashMap> CONCURRENT_HASH_MAP_FACTORY = new IntFunction() {
@Override
public ConcurrentHashMap apply(int len) {
return new ConcurrentHashMap(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super Queue> QUEUE_FACTORY = new IntFunction() {
@Override
public Queue apply(int len) {
return new LinkedList();
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super ArrayDeque> ARRAY_DEQUE_FACTORY = new IntFunction() {
@Override
public ArrayDeque apply(int len) {
return new ArrayDeque(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super LinkedBlockingQueue> LINKED_BLOCKING_QUEUE_FACTORY = new IntFunction() {
@Override
public LinkedBlockingQueue apply(int len) {
return new LinkedBlockingQueue(len);
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super ConcurrentLinkedQueue> CONCURRENT_LINKED_QUEUE_FACTORY = new IntFunction() {
@Override
public ConcurrentLinkedQueue apply(int len) {
return new ConcurrentLinkedQueue();
}
};
@SuppressWarnings("rawtypes")
private static final IntFunction super PriorityQueue> PRIORITY_QUEUE_FACTORY = new IntFunction() {
@Override
public PriorityQueue apply(int len) {
return new PriorityQueue(len);
}
};
private Factory() {
// singleton.
}
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;
}
@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> ofTreeSet() {
return (IntFunction) TREE_SET_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> ofTreeMap() {
return (IntFunction) TREE_MAP_FACTORY;
}
@SuppressWarnings("rawtypes")
public static IntFunction> ofConcurrentHashMap() {
return (IntFunction) CONCURRENT_HASH_MAP_FACTORY;
}
@SuppressWarnings("rawtypes")
public static IntFunction> ofQueue() {
return (IntFunction) QUEUE_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;
}
}
public static final class Suppliers {
@SuppressWarnings("rawtypes")
private static final Supplier super List> LIST = new Supplier() {
@Override
public List get() {
return new ArrayList();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super LinkedList> LINKED_LIST = new Supplier() {
@Override
public LinkedList get() {
return new LinkedList();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super Set> SET = new Supplier() {
@Override
public Set get() {
return new HashSet();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super LinkedHashSet> LINKED_HASH_SET = new Supplier() {
@Override
public LinkedHashSet get() {
return new LinkedHashSet();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super TreeSet> TREE_SET = new Supplier() {
@Override
public TreeSet get() {
return new TreeSet();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super Map> MAP = new Supplier() {
@Override
public Map get() {
return new HashMap();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super LinkedHashMap> LINKED_HASH_MAP = new Supplier() {
@Override
public LinkedHashMap get() {
return new LinkedHashMap();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super TreeMap> TREE_MAP = new Supplier() {
@Override
public TreeMap get() {
return new TreeMap();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super ConcurrentHashMap> CONCURRENT_HASH_MAP = new Supplier() {
@Override
public ConcurrentHashMap get() {
return new ConcurrentHashMap();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super Queue> QUEUE = new Supplier() {
@Override
public Queue get() {
return new LinkedList();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super ArrayDeque> ARRAY_DEQUE = new Supplier() {
@Override
public ArrayDeque get() {
return new ArrayDeque();
}
};
@SuppressWarnings("rawtypes")
private static final Supplier super LinkedBlockingQueue> LINKED_BLOCKING_QUEUE = new Supplier