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.intlist;
import static functionalj.lens.Access.theInteger;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Random;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
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.IntComparator;
import functionalj.function.aggregator.IntAggregation;
import functionalj.function.aggregator.IntAggregationToBoolean;
import functionalj.function.aggregator.IntAggregationToDouble;
import functionalj.function.aggregator.IntAggregationToInt;
import functionalj.function.aggregator.IntAggregationToLong;
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.longlist.AsLongFuncList;
import functionalj.list.longlist.LongFuncList;
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.GrowOnlyIntArray;
import functionalj.stream.intstream.IntIterable;
import functionalj.stream.intstream.IntIteratorPlus;
import functionalj.stream.intstream.IntStreamPlus;
import functionalj.stream.intstream.IntStreamPlusHelper;
import functionalj.stream.longstream.LongStreamPlus;
import functionalj.stream.markers.Eager;
import functionalj.stream.markers.Sequential;
import functionalj.stream.markers.Terminal;
import functionalj.tuple.IntIntTuple;
import lombok.val;
import nullablej.nullable.Nullable;
// TODO - Use this for byte, short and char
public interface IntFuncList
extends
AsIntFuncList,
IntIterable,
IntPredicate,
IntFuncListWithCombine,
IntFuncListWithFilter,
IntFuncListWithFlatMap,
IntFuncListWithLimit,
IntFuncListWithMap,
IntFuncListWithMapFirst,
IntFuncListWithMapGroup,
IntFuncListWithMapMulti,
IntFuncListWithMapThen,
IntFuncListWithMapToMap,
IntFuncListWithMapToTuple,
IntFuncListWithMapWithIndex,
IntFuncListWithModify,
IntFuncListWithPeek,
IntFuncListWithPipe,
IntFuncListWithSegment,
IntFuncListWithSort,
IntFuncListWithSplit {
/** Throw a no more element exception. This is used for generator. */
public static int noMoreElement() throws NoMoreResultException {
SupplierBackedIterator.noMoreElement();
return Integer.MIN_VALUE;
}
/** Returns an empty IntFuncList. */
public static ImmutableIntFuncList empty() {
return ImmutableIntFuncList.empty();
}
/** Returns an empty functional list. */
public static ImmutableIntFuncList emptyList() {
return ImmutableIntFuncList.empty();
}
/** Returns an empty functional list. */
public static ImmutableIntFuncList emptyIntList() {
return ImmutableIntFuncList.empty();
}
/** Create a FuncList from the given ints. */
public static ImmutableIntFuncList of(int... data) {
return ImmutableIntFuncList.of(data);
}
/** Create a FuncList from the given ints. */
public static ImmutableIntFuncList AllOf(int... data) {
return ImmutableIntFuncList.of(data);
}
/** Create a FuncList from the given ints. */
public static ImmutableIntFuncList ints(int... data) {
return ImmutableIntFuncList.of(data);
}
/** Create a FuncList from the given ints. */
public static ImmutableIntFuncList intList(int... data) {
return ImmutableIntFuncList.of(data);
}
/** Create a FuncList from the given ints. */
@SafeVarargs
public static ImmutableIntFuncList ListOf(int... data) {
return ImmutableIntFuncList.of(data);
}
/** Create a FuncList from the given ints. */
@SafeVarargs
public static ImmutableIntFuncList listOf(int... data) {
return ImmutableIntFuncList.of(data);
}
//-- From --
/** Create a FuncList from the given ints. */
public static ImmutableIntFuncList from(int[] datas) {
return new ImmutableIntFuncList(datas, datas.length);
}
/** Create a FuncList from the given collection. */
public static IntFuncList from(Collection data, int valueForNull) {
IntStream intStream
= StreamPlus.from(data.stream())
.fillNull((Integer)valueForNull)
.mapToInt(theInteger);
Mode mode = (data instanceof FuncList) ? ((FuncList)data).mode() : Mode.lazy;
return ImmutableIntFuncList.from(mode, intStream);
}
/** Create a FuncList from the given FuncList. */
public static IntFuncList from(Mode mode, AsIntFuncList asFuncList) {
val funcList = asFuncList.asIntFuncList();
return funcList.toMode(mode);
}
/** Create a FuncList from the given stream. */
public static IntFuncList from(IntStream stream) {
return new StreamBackedIntFuncList(stream);
}
/**
* Create a FuncList from the given supplier of stream.
*
* The provided stream should produce the same sequence of values.
**/
public static IntFuncList from(Supplier supplier) {
return new IntFuncListDerived(() -> IntStreamPlus.from(supplier.get()));
}
// == Create ==
/** Returns the infinite streams of zeroes. */
public static IntFuncList zeroes() {
return zeroes(Integer.MAX_VALUE);
}
/** Returns a list that contains zeroes. */
public static IntFuncList zeroes(int count) {
return IntFuncList.from(() -> IntStreamPlus.zeroes(count));
}
/** Returns the list of ones. */
public static IntFuncList ones() {
return ones(Integer.MAX_VALUE);
}
/** Returns a list that contains ones. */
public static IntFuncList ones(int count) {
return IntFuncList.from(() -> IntStreamPlus.ones(count));
}
/** Create a list that is the repeat of the given array of data. */
public static IntFuncList repeat(int... data) {
return IntFuncList.from(() -> IntStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given array of data. */
public static IntFuncList cycle(int... data) {
return IntFuncList.from(() -> IntStreamPlus.cycle(data));
}
/** Create a list that is the repeat of the given list of data. */
public static IntFuncList repeat(IntFuncList data) {
return IntFuncList.from(() -> IntStreamPlus.repeat(data));
}
/** Create a list that is the repeat of the given list of data. */
public static IntFuncList cycle(IntFuncList data) {
return IntFuncList.from(() -> IntStreamPlus.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 IntFuncList loop() {
return IntFuncList.from(() -> IntStreamPlus.loop());
}
/** Create a list that for a loop with the number of time given - the value is the index of the loop. */
public static IntFuncList loop(int times) {
return IntFuncList.from(() -> IntStreamPlus.loop(times));
}
public static IntFuncList loopBy(int step) {
return IntFuncList.from(() -> IntStreamPlus.loopBy(step));
}
public static IntFuncList loopBy(int step, int times) {
return IntFuncList.from(() -> IntStreamPlus.loopBy(step, times));
}
public static IntFuncList infinite() {
return IntFuncList.from(() -> IntStreamPlus.infinite());
}
public static IntFuncList infiniteInt() {
return IntFuncList.from(() -> IntStreamPlus.infiniteInt());
}
public static IntFuncList naturalNumbers() {
return IntFuncList.from(() -> IntStreamPlus.naturalNumbers());
}
public static IntFuncList naturalNumbers(int count) {
return IntFuncList.from(() -> IntStreamPlus.naturalNumbers(count));
}
public static IntFuncList wholeNumbers() {
return IntFuncList.from(() -> IntStreamPlus.wholeNumbers());
}
/** Returns the infinite streams of wholes numbers -- 0, 1, 2, 3, .... */
public static IntFuncList wholeNumbers(int count) {
return IntFuncList.from(() -> IntStreamPlus.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 IntFuncList range(int startInclusive, int endExclusive) {
return IntFuncList.from(() -> IntStreamPlus.range(startInclusive, endExclusive));
}
//-- Concat + Combine --
/** Concatenate all the given streams. */
public static IntFuncList concat(IntFuncList... 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 IntFuncList combine(IntFuncList... lists) {
ImmutableFuncList listOfList = FuncList.listOf(lists);
return listOfList.flatMapToInt(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 IntFuncList generate(Supplier suppliers) {
return IntFuncList.from(() -> {
val generator = suppliers.get();
return IntStreamPlus.generate(generator);
});
}
/**
* Create a list from the supplier of suppliers.
* The supplier will be repeatedly asked for value until NoMoreResultException is thrown.
**/
public static IntFuncList 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 IntFuncList iterate(
int seed,
IntUnaryOperator compounder) {
return IntFuncList.from(() -> IntStreamPlus.iterate(seed, compounder));
}
public static IntFuncList iterate(
int seed,
IntAggregationToInt aggregation) {
return IntFuncList.from(() -> IntStreamPlus.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 IntFuncList compound(
int seed,
IntUnaryOperator compounder) {
return IntFuncList.from(() -> IntStreamPlus.compound(seed, compounder));
}
public static IntFuncList compound(
int seed,
IntAggregationToInt aggregation) {
return IntFuncList.from(() -> IntStreamPlus.compound(seed, aggregation));
}
/**
* 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 IntFuncList iterate(
int seed1,
int seed2,
IntBinaryOperator compounder) {
return IntFuncList.from(() -> IntStreamPlus.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 IntFuncList compound(
int seed1,
int seed2,
IntBinaryOperator 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(
AsIntFuncList list1,
AsIntFuncList list2) {
return FuncList.from(() -> {
return IntStreamPlus.zipOf(
list1.intStream(),
list2.intStream());
});
}
/**
* Create a FuncList by combining elements together using the merger function and collected into the result stream.
* 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]
* merger = a + "+" + b
*
* The result stream = ["A+1", "B+2", "C+3", "D+4"].
**/
public static FuncList zipOf(
AsIntFuncList list1, int defaultValue1,
AsIntFuncList list2, int defaultValue2) {
return FuncList.from(() -> {
return IntStreamPlus.zipOf(
list1.intStream(), defaultValue1,
list2.intStream(), defaultValue2);
});
}
/** Zip integers from two IntFuncLists and combine it into another object. */
public static IntFuncList zipOf(
AsIntFuncList list1,
AsIntFuncList list2,
IntBinaryOperator merger) {
return IntFuncList.from(() -> {
return IntStreamPlus.zipOf(
list1.intStream(),
list2.intStream(),
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 IntFuncList zipOf(
AsIntFuncList list1, int defaultValue1,
AsIntFuncList list2, int defaultValue2,
IntBinaryOperator merger) {
return IntFuncList.from(() -> {
return IntStreamPlus.zipOf(
list1.intStream(), defaultValue1,
list2.intStream(), defaultValue2,
merger);
});
}
// -- Builder --
/** Create a new FuncList. */
public static IntFuncListBuilder newListBuilder() {
return new IntFuncListBuilder();
}
/** Create a new list builder. */
public static IntFuncListBuilder newBuilder() {
return new IntFuncListBuilder();
}
/** Create a new FuncList. */
public static IntFuncListBuilder newIntListBuilder() {
return new IntFuncListBuilder();
}
// == Core ==
/** Return the stream of data behind this IntFuncList. */
public IntStreamPlus intStream();
/** Return the stream of data behind this IntFuncList. */
public default IntStreamPlus intStreamPlus() {
return intStream();
}
/** Return the this as a FuncList. */
@Override
public default IntFuncList asIntFuncList() {
return this;
}
//-- Derive --
/** Create a FuncList from the given FuncList. */
@SuppressWarnings({"rawtypes", "unchecked"})
public static