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.stream.EntryStream 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.stream;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.Comparators;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.LongMultiset;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nullable;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Tuple;
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.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
/**
*
* @since 0.9
*
* @author Haiyang Li
*/
public final class EntryStream implements AutoCloseable {
private static final Function, Stream>> mapper_func = new Function, Stream>>() {
@Override
public Stream> apply(Map t) {
return Stream.of(t);
}
};
@SuppressWarnings("rawtypes")
private static final EntryStream EMPTY = new EntryStream(Stream. empty());
private final Stream> s;
EntryStream(final Stream extends Map.Entry> s) {
this.s = (Stream>) s;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
static Function, Stream>> mapFunc() {
return (Function) mapper_func;
}
public static EntryStream empty() {
return EMPTY;
}
public static EntryStream of(K k1, V v1) {
return of(Stream.of(Tuple.of(k1, v1)));
}
public static EntryStream of(K k1, V v1, K k2, V v2) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2)));
}
public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2), Tuple.of(k3, v3)));
}
public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2), Tuple.of(k3, v3), Tuple.of(k4, v4)));
}
public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2), Tuple.of(k3, v3), Tuple.of(k4, v4), Tuple.of(k5, v5)));
}
public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2), Tuple.of(k3, v3), Tuple.of(k4, v4), Tuple.of(k5, v5), Tuple.of(k6, v6)));
}
public static EntryStream of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
return of(Stream.of(Tuple.of(k1, v1), Tuple.of(k2, v2), Tuple.of(k3, v3), Tuple.of(k4, v4), Tuple.of(k5, v5), Tuple.of(k6, v6), Tuple.of(k7, v7)));
}
public static EntryStream of(final Stream extends Map.Entry> s) {
return new EntryStream(s);
}
public static EntryStream of(final Iterator extends Map.Entry> iterator) {
return new EntryStream(Stream.of(iterator));
}
public static EntryStream of(final Map map) {
return new EntryStream(Stream.of(map));
}
public static EntryStream of(final Collection extends Map.Entry> entries) {
return new EntryStream(Stream.of(entries));
}
// @SafeVarargs
// public static EntryStream of(final Map.Entry... entries) {
// return new EntryStream(Stream.of(entries));
// }
public static EntryStream of(final Multiset multiset) {
return multiset == null ? EntryStream. empty() : multiset.entryStream();
}
public static EntryStream of(final LongMultiset multiset) {
return multiset == null ? EntryStream. empty() : multiset.entryStream();
}
public static > EntryStream of(final Multimap mulitmap) {
return mulitmap == null ? EntryStream. empty() : mulitmap.entryStream();
}
public static EntryStream of(final Collection extends T> c, final Function super T, K> keyExtractor) {
final Function valueMapper = Fn.identity();
return Stream.of(c).mapToEntry(keyExtractor, valueMapper);
}
public static EntryStream of(final T[] a, final Function super T, K> keyExtractor) {
final Function valueMapper = Fn.identity();
return Stream.of(a).mapToEntry(keyExtractor, valueMapper);
}
@SafeVarargs
public static EntryStream concat(final Map... maps) {
final Function, Stream>> mapper = mapFunc();
return Stream.of(maps).flatMapToEntry(mapper);
}
public static EntryStream concat(final Collection extends Map> maps) {
final Function, Stream>> mapper = mapFunc();
return Stream.of(maps).flatMapToEntry(mapper);
}
public static EntryStream zip(final K[] keys, final V[] values) {
final BiFunction> zipFunction = new BiFunction>() {
@Override
public Entry apply(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
};
final Function, Map.Entry> mapper = Fn.identity();
return Stream.zip(keys, values, zipFunction).mapToEntry(mapper);
}
public static EntryStream zip(final K[] keys, final V[] values, K valueForNonKey, V valueForNonValue) {
final BiFunction> zipFunction = new BiFunction>() {
@Override
public Entry apply(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
};
final Function, Map.Entry> mapper = Fn.identity();
return Stream.zip(keys, values, valueForNonKey, valueForNonValue, zipFunction).mapToEntry(mapper);
}
public static EntryStream zip(final Collection extends K> keys, final Collection extends V> values) {
final BiFunction> zipFunction = new BiFunction>() {
@Override
public Entry apply(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
};
final Function, Map.Entry> mapper = Fn.identity();
return Stream.zip(keys, values, zipFunction).mapToEntry(mapper);
}
public static EntryStream zip(final Collection extends K> keys, final Collection extends V> values, K valueForNonKey, V valueForNonValue) {
final BiFunction> zipFunction = new BiFunction>() {
@Override
public Entry apply(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
};
final Function, Map.Entry> mapper = Fn.identity();
return Stream.zip(keys, values, valueForNonKey, valueForNonValue, zipFunction).mapToEntry(mapper);
}
public Stream keys() {
final Function, K> func = Fn.key();
return s.map(func);
}
public Stream values() {
final Function, V> func = Fn.value();
return s.map(func);
}
public Stream> entries() {
return s;
}
public EntryStream inversed() {
final Function, Map.Entry> mapper = new Function, Map.Entry>() {
@Override
public Entry apply(Entry e) {
return new AbstractMap.SimpleImmutableEntry<>(e.getValue(), e.getKey());
}
};
return map(mapper);
}
public EntryStream filter(final Predicate> predicate) {
return of(s.filter(predicate));
}
public EntryStream filter(final BiPredicate super K, ? super V> predicate) {
final Predicate> predicate2 = new Predicate>() {
@Override
public boolean test(Entry entry) {
return predicate.test(entry.getKey(), entry.getValue());
}
};
return of(s.filter(predicate2));
}
public EntryStream filterByKey(final Predicate super K> keyPredicate) {
final Predicate> predicate = Fn.testByKey(keyPredicate);
return of(s.filter(predicate));
}
public EntryStream filterByValue(final Predicate super V> valuePredicate) {
final Predicate> predicate = Fn.testByValue(valuePredicate);
return of(s.filter(predicate));
}
public EntryStream map(final Function super Map.Entry, Map.Entry> mapper) {
return of(s.map(mapper));
}
public EntryStream map(final BiFunction super K, ? super V, Map.Entry> mapper) {
final Function, Map.Entry> mapper2 = new Function, Map.Entry>() {
@Override
public Entry apply(Map.Entry entry) {
return mapper.apply(entry.getKey(), entry.getValue());
}
};
return of(s.map(mapper2));
}
public EntryStream map(final Function super K, KK> keyMapper, final Function super V, VV> valueMapper) {
final Function, Map.Entry> mapper = new Function, Map.Entry>() {
@Override
public Entry apply(Entry t) {
return new AbstractMap.SimpleImmutableEntry<>(keyMapper.apply(t.getKey()), valueMapper.apply(t.getValue()));
}
};
return map(mapper);
}
public EntryStream mapKey(final Function super K, KK> keyMapper) {
final Function, Map.Entry> mapper = Fn.mapKey(keyMapper);
return of(s.map(mapper));
}
public EntryStream mapValue(final Function super V, VV> valueMapper) {
final Function, Map.Entry> mapper = Fn.mapValue(valueMapper);
return of(s.map(mapper));
}
// public EntryStream flatMap(final Function super Map.Entry, EntryStream> mapper) {
// final Function, Stream>> mapper2 = new Function, Stream>>() {
// @Override
// public Stream> apply(Entry t) {
// return mapper.apply(t).s;
// }
// };
//
// return flatCollection(mapper2);
// }
public EntryStream flatMap(final Function super Map.Entry, Stream>> mapper) {
return of(s.flatMap(mapper));
}
// public EntryStream flatArray(final Function super Map.Entry, Map> mapper) {
// final Function, Stream>> mapper2 = new Function, Stream>>() {
// @Override
// public Stream> apply(Entry t) {
// return Stream.of(mapper.apply(t));
// }
// };
//
// return flatMap(mapper2);
// }
public EntryStream flatMapKey(final Function super K, Stream> keyMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Map.Entry e) {
return keyMapper.apply(e.getKey()).map(new Function>() {
@Override
public Map.Entry apply(KK kk) {
return new AbstractMap.SimpleImmutableEntry<>(kk, e.getValue());
}
});
}
};
return flatMap(mapper2);
}
public EntryStream flatMapValue(final Function super V, Stream> valueMapper) {
final Function, Stream>> mapper2 = new Function, Stream>>() {
@Override
public Stream> apply(final Entry e) {
return valueMapper.apply(e.getValue()).map(new Function>() {
@Override
public Map.Entry apply(VV vv) {
return new AbstractMap.SimpleImmutableEntry<>(e.getKey(), vv);
}
});
}
};
return flatMap(mapper2);
}
/**
*
* @param classifier
* @return
* @see Collectors#groupingBy(Function)
*/
public EntryStream> groupBy() {
final Function super Map.Entry, K> classifier = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
return of(s.groupBy(classifier, valueMapper));
}
/**
*
* @param classifier
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Supplier)
*/
public EntryStream> groupBy(final Supplier>> mapFactory) {
final Function super Map.Entry, K> classifier = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
return of(s.groupBy(classifier, valueMapper, mapFactory));
}
public EntryStream> groupBy(final Function super Map.Entry, ? extends KK> keyExtractor,
final Function super Map.Entry, ? extends VV> valueMapper) {
return of(s.groupBy(keyExtractor, valueMapper));
}
/**
*
* @param keyExtractor
* @param valueMapper
* @param mapFactory
* @return
* @see Collectors#toMultimap(Function, Function, Supplier)
*/
public EntryStream> groupBy(final Function super Map.Entry, ? extends KK> keyExtractor,
final Function super Map.Entry, ? extends VV> valueMapper, final Supplier>> mapFactory) {
return of(s.groupBy(keyExtractor, valueMapper, mapFactory));
}
/**
*
* @param downstream
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
public EntryStream groupBy(final Collector super Map.Entry, A, D> downstream) {
final Function super Map.Entry, K> classifier = Fn.key();
return of(s.groupBy(classifier, downstream));
}
/**
*
* @param downstream
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
public EntryStream groupBy(final Collector super Map.Entry, A, D> downstream, final Supplier> mapFactory) {
final Function super Map.Entry, K> classifier = Fn.key();
return of(s.groupBy(classifier, downstream, mapFactory));
}
/**
*
* @param classifier
* @param downstream
* @param mapFactory
* @return
* @see Collectors#groupingBy(Function, Collector, Supplier)
*/
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> classifier,
final Collector super Map.Entry, A, D> downstream) {
return of(s.groupBy(classifier, downstream));
}
/**
*
* @param classifier
* @param downstream
* @return
* @see Collectors#groupingBy(Function, Collector)
*/
public EntryStream groupBy(final Function super Map.Entry, ? extends KK> classifier,
final Collector super Map.Entry, A, D> downstream, final Supplier> mapFactory) {
return of(s.groupBy(classifier, downstream, mapFactory));
}
/**
*
* @param mergeFunction
* @return
*/
public EntryStream groupBy(final BinaryOperator mergeFunction) {
final Function super Map.Entry, K> classifier = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
return of(s.groupBy(classifier, valueMapper, mergeFunction));
}
/**
*
* @param mergeFunction
* @param mapFactory
* @return
*/
public EntryStream groupBy(final BinaryOperator mergeFunction, final Supplier> mapFactory) {
final Function super Map.Entry, K> classifier = Fn.key();
final Function super Map.Entry, V> valueMapper = Fn.value();
return of(s.groupBy(classifier, valueMapper, mergeFunction, mapFactory));
}
/**
*
* @param keyExtractor
* @param valueMapper
* @param mergeFunction
* @return
* @see Collectors#groupBy(Function, Function, BinaryOperator)
*/
public EntryStream