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.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.list.longlist;
import static functionalj.lens.Access.theLong;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Random;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.function.LongBinaryOperator;
import java.util.function.LongConsumer;
import java.util.function.LongFunction;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
import java.util.function.Supplier;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import functionalj.function.Func;
import functionalj.function.LongComparator;
import functionalj.function.aggregator.LongAggregation;
import functionalj.function.aggregator.LongAggregationToBoolean;
import functionalj.function.aggregator.LongAggregationToDouble;
import functionalj.function.aggregator.LongAggregationToInt;
import functionalj.function.aggregator.LongAggregationToLong;
import functionalj.list.AsFuncList;
import functionalj.list.FuncList;
import functionalj.list.FuncList.Mode;
import functionalj.list.ImmutableFuncList;
import functionalj.list.doublelist.AsDoubleFuncList;
import functionalj.list.doublelist.DoubleFuncList;
import functionalj.list.intlist.AsIntFuncList;
import functionalj.list.intlist.IntFuncList;
import functionalj.result.NoMoreResultException;
import functionalj.result.Result;
import functionalj.stream.StreamPlus;
import functionalj.stream.SupplierBackedIterator;
import functionalj.stream.doublestream.DoubleStreamPlus;
import functionalj.stream.intstream.IntStreamPlus;
import functionalj.stream.longstream.GrowOnlyLongArray;
import functionalj.stream.longstream.LongIterable;
import functionalj.stream.longstream.LongIteratorPlus;
import functionalj.stream.longstream.LongStreamPlus;
import functionalj.stream.longstream.LongStreamPlusHelper;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Sequential;
import functionalj.stream.markers.Terminal;
import functionalj.tuple.IntLongTuple;
import functionalj.tuple.LongLongTuple;
import lombok.val;
import nullablej.nullable.Nullable;
// TODO - Use this for byte, short and char
public interface LongFuncList
extends
AsLongFuncList,
LongIterable,
LongPredicate,
LongFuncListWithCombine,
LongFuncListWithFilter,
LongFuncListWithFlatMap,
LongFuncListWithLimit,
LongFuncListWithMap,
LongFuncListWithMapFirst,
LongFuncListWithMapGroup,
LongFuncListWithMapMulti,
LongFuncListWithMapThen,
LongFuncListWithMapToMap,
LongFuncListWithMapToTuple,
LongFuncListWithMapWithIndex,
LongFuncListWithModify,
LongFuncListWithPeek,
LongFuncListWithPipe,
LongFuncListWithSegment,
LongFuncListWithSort,
LongFuncListWithSplit {
/** Throw a no more element exception. This is used for generator. */
public static long noMoreElement() throws NoMoreResultException {
SupplierBackedIterator.noMoreElement();
return Long.MIN_VALUE;
}
/** Returns an empty LongFuncList. */
public static ImmutableLongFuncList empty() {
return ImmutableLongFuncList.empty();
}
/** Returns an empty functional list. */
public static ImmutableLongFuncList emptyList() {
return ImmutableLongFuncList.empty();
}
/** Returns an empty functional list. */
public static ImmutableLongFuncList emptyLongList() {
return ImmutableLongFuncList.empty();
}
/** Create a FuncList from the given longs. */
public static ImmutableLongFuncList of(long... data) {
return ImmutableLongFuncList.of(data);
}
/** Create a FuncList from the given longs. */
public static ImmutableLongFuncList AllOf(long... data) {
return ImmutableLongFuncList.of(data);
}
/** Create a FuncList from the given longs. */
public static ImmutableLongFuncList longs(long... data) {
return ImmutableLongFuncList.of(data);
}
/** Create a FuncList from the given longs. */
public static ImmutableLongFuncList longList(long... data) {
return ImmutableLongFuncList.of(data);
}
/** Create a FuncList from the given longs. */
@SafeVarargs
public static ImmutableLongFuncList ListOf(long... data) {
return ImmutableLongFuncList.of(data);
}
/** Create a FuncList from the given longs. */
@SafeVarargs
public static ImmutableLongFuncList listOf(long... data) {
return ImmutableLongFuncList.of(data);
}
//-- From --
/** Create a FuncList from the given longs. */
public static ImmutableLongFuncList from(long[] datas) {
return new ImmutableLongFuncList(datas, datas.length);
}
/** Create a FuncList from the given collection. */
public static ImmutableLongFuncList from(Collection data, long valueForNull) {
LongStream longStream
= StreamPlus.from(data.stream())
.fillNull((Long)valueForNull)
.mapToLong(theLong);
Mode mode = (data instanceof FuncList) ? ((FuncList)data).mode() : Mode.lazy;
return ImmutableLongFuncList.from(mode, longStream);
}
/** Create a FuncList from the given FuncList. */
public static LongFuncList from(Mode mode, AsLongFuncList asFuncList) {
val funcList = asFuncList.asLongFuncList();
return funcList.toMode(mode);
}
/** Create a FuncList from the given stream. */
public static LongFuncList from(LongStream stream) {
return new StreamBackedLongFuncList(stream);
}
/**
* Create a FuncList from the given supplier of stream.
*
* The provided stream should produce the same sequence of values.
**/
public static LongFuncList from(Supplier supplier) {
return new LongFuncListDerived(() -> LongStreamPlus.from(supplier.get()));
}
// == Create ==
/** Returns the infinite streams of zeroes. */
public static LongFuncList zeroes() {
return zeroes(Integer.MAX_VALUE);
}
/** Returns a list that contains zeroes. */
public static LongFuncList zeroes(int count) {
return LongFuncList.from(() -> LongStreamPlus.zeroes(count));
}
/** Returns the list of ones. */
public static LongFuncList ones() {
return ones(Integer.MAX_VALUE);
}
/** Returns a list that contains ones. */
public static LongFuncList ones(int count) {
return LongFuncList.from(() -> LongStreamPlus.ones(count));
}
/** Create a list that is the repeat of the given array of data. */
public static LongFuncList repeat(long... data) {
return LongFuncList.from(() -> LongStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given array of data. */
public static LongFuncList cycle(long... data) {
return LongFuncList.from(() -> LongStreamPlus.cycle(data));
}
/** Create a list that is the repeat of the given list of data. */
public static LongFuncList repeat(LongFuncList data) {
return LongFuncList.from(() -> LongStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given list of data. */
public static LongFuncList cycle(LongFuncList data) {
return LongFuncList.from(() -> LongStreamPlus.cycle(data));
}
/** Create a list that for a loop with the number of time given - the value is the index of the loop. */
public static LongFuncList loop() {
return LongFuncList.from(() -> LongStreamPlus.loop());
}
/** Create a list that for a loop with the number of time given - the value is the index of the loop. */
public static LongFuncList loop(long times) {
return LongFuncList.from(() -> LongStreamPlus.loop(times));
}
public static LongFuncList loopBy(long step) {
return LongFuncList.from(() -> LongStreamPlus.loopBy(step));
}
public static LongFuncList loopBy(long step, int times) {
return LongFuncList.from(() -> LongStreamPlus.loopBy(step, times));
}
public static LongFuncList infinite() {
return LongFuncList.from(() -> LongStreamPlus.infinite());
}
public static LongFuncList infiniteInt() {
return LongFuncList.from(() -> LongStreamPlus.infiniteInt());
}
public static LongFuncList naturalNumbers() {
return LongFuncList.from(() -> LongStreamPlus.naturalNumbers());
}
public static LongFuncList naturalNumbers(int count) {
return LongFuncList.from(() -> LongStreamPlus.naturalNumbers(count));
}
/** Returns the infinite streams of wholes numbers -- 0, 1, 2, 3, .... */
public static LongFuncList wholeNumbers() {
return LongFuncList.from(() -> LongStreamPlus.wholeNumbers());
}
/** Returns the infinite streams of wholes numbers -- 0, 1, 2, 3, .... */
public static LongFuncList wholeNumbers(int count) {
return LongFuncList.from(() -> LongStreamPlus.wholeNumbers(count));
}
/** Create a FuncList that for a loop with the number of time given - the value is the index of the loop. */
public static LongFuncList range(int startInclusive, int endExclusive) {
return LongFuncList.from(() -> LongStreamPlus.range(startInclusive, endExclusive));
}
//-- Concat + Combine --
/** Concatenate all the given streams. */
public static LongFuncList concat(LongFuncList... lists) {
return combine(lists);
}
/**
* Concatenate all the given lists.
*
* This method is the alias of {@link FuncList#concat(FuncList...)}
* but allowing static import without colliding with {@link String#concat(String)}.
**/
public static LongFuncList combine(LongFuncList... lists) {
ImmutableFuncList listOfList = FuncList.listOf(lists);
return listOfList.flatMapToLong(Func.itself());
}
// TODO - Rethink ... as this will generate un-repeatable stream.
// we may want to do cache here.
/**
* Create a FuncList from the supplier of suppliers.
* The supplier will be repeatedly asked for value until NoMoreResultException is thrown.
**/
public static LongFuncList generate(Supplier suppliers) {
return LongFuncList.from(() -> {
val generator = suppliers.get();
return LongStreamPlus.generate(generator);
});
}
/**
* Create a list from the supplier of suppliers.
* The supplier will be repeatedly asked for value until NoMoreResultException is thrown.
**/
public static LongFuncList generateWith(Supplier suppliers) {
return generate(suppliers);
}
/**
* Create a list by apply the compounder to the seed over and over.
*
* For example: let say seed = 1 and f(x) = x*2.
* The result stream will be:
* 1 <- seed,
* 2 <- (1*2),
* 4 <- ((1*2)*2),
* 8 <- (((1*2)*2)*2),
* 16 <- ((((1*2)*2)*2)*2)
* ...
*
* Note: this is an alias of compound()
**/
public static LongFuncList iterate(
long seed,
LongUnaryOperator compounder) {
return LongFuncList.from(() -> LongStreamPlus.iterate(seed, compounder));
}
public static LongFuncList iterate(
long seed,
LongAggregationToLong aggregation) {
return LongFuncList.from(() -> LongStreamPlus.iterate(seed, aggregation));
}
/**
* Create a list by apply the compounder to the seed over and over.
*
* For example: let say seed = 1 and f(x) = x*2.
* The result stream will be:
* 1 <- seed,
* 2 <- (1*2),
* 4 <- ((1*2)*2),
* 8 <- (((1*2)*2)*2),
* 16 <- ((((1*2)*2)*2)*2)
* ...
*
* Note: this is an alias of iterate()
**/
public static LongFuncList compound(
long seed,
LongUnaryOperator compounder) {
return LongFuncList.from(() -> LongStreamPlus.compound(seed, compounder));
}
public static LongFuncList compound(
long seed,
LongAggregationToLong compounder) {
return LongFuncList.from(() -> LongStreamPlus.compound(seed, compounder));
}
/**
* Create a list by apply the compounder to the seeds over and over.
*
* For example: let say seed1 = 1, seed2 = 1 and f(a,b) = a+b.
* The result stream will be:
* 1 <- seed1,
* 1 <- seed2,
* 2 <- (1+1),
* 3 <- (1+2),
* 5 <- (2+3),
* 8 <- (5+8)
* ...
*
* Note: this is an alias of compound()
**/
public static LongFuncList iterate(
long seed1,
long seed2,
LongBinaryOperator compounder) {
return LongFuncList.from(() -> LongStreamPlus.iterate(seed1, seed2, compounder));
}
/**
* Create a list by apply the compounder to the seeds over and over.
*
* For example: let say seed1 = 1, seed2 = 1 and f(a,b) = a+b.
* The result stream will be:
* 1 <- seed1,
* 1 <- seed2,
* 2 <- (1+1),
* 3 <- (1+2),
* 5 <- (2+3),
* 8 <- (5+8)
* ...
*
* Note: this is an alias of iterate()
**/
public static LongFuncList compound(
long seed1,
long seed2,
LongBinaryOperator compounder) {
return iterate(seed1, seed2, compounder);
}
// == Zip ==
/**
* Create a FuncList by combining elements together into a FuncList of tuples.
* Only elements with pair will be combined. If this is not desirable, use FuncList1.zip(FuncList2).
*
* For example:
* list1 = [A, B, C, D, E]
* list2 = [1, 2, 3, 4]
*
* The result stream = [(A,1), (B,2), (C,3), (D,4)].
**/
public static FuncList zipOf(
AsLongFuncList list1,
AsLongFuncList list2) {
return FuncList.from(() -> {
return LongStreamPlus.zipOf(
list1.longStream(),
list2.longStream());
});
}
public static FuncList zipOf(
AsLongFuncList list1, long defaultValue1,
AsLongFuncList list2, long defaultValue2) {
return FuncList.from(() -> {
return LongStreamPlus.zipOf(
list1.longStream(), defaultValue1,
list2.longStream(), defaultValue2);
});
}
/** Zip integers from two LongFuncLists and combine it into another object. */
public static LongFuncList zipOf(
AsLongFuncList list1,
AsLongFuncList list2,
LongBinaryOperator merger) {
return LongFuncList.from(() -> {
return LongStreamPlus.zipOf(
list1.longStream(),
list2.longStream(),
merger);
});
}
/**
* Zip integers from an int stream and another object stream and combine it into another object.
* The result stream has the size of the shortest stream.
*/
public static LongFuncList zipOf(
AsLongFuncList list1, long defaultValue1,
AsLongFuncList list2, long defaultValue2,
LongBinaryOperator merger) {
return LongFuncList.from(() -> {
return LongStreamPlus.zipOf(
list1.longStream(), defaultValue1,
list2.longStream(), defaultValue2,
merger);
});
}
// -- Builder --
/** Create a new FuncList. */
public static LongFuncListBuilder newListBuilder() {
return new LongFuncListBuilder();
}
/** Create a new list builder. */
public static LongFuncListBuilder newBuilder() {
return new LongFuncListBuilder();
}
/** Create a new FuncList. */
public static LongFuncListBuilder newIntListBuilder() {
return new LongFuncListBuilder();
}
// == Core ==
/** Return the stream of data behind this LongFuncList. */
public LongStreamPlus longStream();
/** Return the stream of data behind this LongFuncList. */
public default LongStreamPlus longStreamPlus() {
return longStream();
}
/** Return the this as a FuncList. */
@Override
public default LongFuncList asLongFuncList() {
return this;
}
//-- Derive --
/** Create a FuncList from the given FuncList. */
@SuppressWarnings({"rawtypes", "unchecked"})
public static