cyclops.companion.vavr.VavrMonoids Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-vavr Show documentation
Show all versions of cyclops-vavr Show documentation
Converters and Comprehenders for Javaslang
The newest version!
package cyclops.companion.vavr;
import cyclops.function.Monoid;
import io.vavr.collection.*;
import io.vavr.collection.HashSet;
import io.vavr.collection.LinkedHashSet;
import io.vavr.collection.List;
import io.vavr.collection.Queue;
import io.vavr.collection.Set;
import io.vavr.collection.TreeSet;
import io.vavr.collection.Vector;
import io.vavr.concurrent.Future;
import io.vavr.concurrent.Promise;
import io.vavr.control.Either;
import io.vavr.control.Option;
import io.vavr.control.Try;
import java.util.*;
/**
*
* A static class with a large number of Monoids or Combiners.
*
* A semigroup is an Object that can be used to combine objects of the same type.
*
* @author johnmcclean
*/
public interface VavrMonoids {
/**
* To manage javac type inference first assign the Monoid
*
* {@code
*
* Monoid> listX = VavrMonoid.linearSeqConcat();
* Monoid> streamX = VavrMonoid.linearSeqConcat();
*
*
*
* }
*
* @return A Monoid that can combine any vavr Set type
*/
static > Monoid linearSeqConcat(C identity) {
return Monoid.of(identity,VavrSemigroups.linearSeqConcat());
}
static > Monoid indexedSeqConcat(C identity) {
return Monoid.of(identity,VavrSemigroups.indexedSeqConcat());
}
static > Monoid setConcat(C identity) {
return Monoid.of(identity,VavrSemigroups.setConcat());
}
static Monoid> listConcat() {
return VavrMonoids.linearSeqConcat(List.empty());
}
static Monoid> vectorConcat() {
return VavrMonoids.indexedSeqConcat(Vector.empty());
}
static Monoid charSeqConcat() {
return VavrMonoids.indexedSeqConcat(CharSeq.empty());
}
static Monoid> arrayConcat() {
return VavrMonoids.indexedSeqConcat(Array.empty());
}
static Monoid> streamConcat() {
return VavrMonoids.linearSeqConcat(Stream.empty());
}
static Monoid> queueConcat() {
return VavrMonoids.linearSeqConcat(Queue.empty());
}
static Monoid> linkedHashSetConcat() {
return VavrMonoids.setConcat(LinkedHashSet.empty());
}
static Monoid> hashSetConcat() {
return VavrMonoids.setConcat(HashSet.empty());
}
static > Monoid> treeSetConcat() {
return VavrMonoids.>setConcat(TreeSet.empty(Comparator.naturalOrder()));
}
static Monoid> treeSetConcat(Comparator super T> c) {
return VavrMonoids.>setConcat(TreeSet.empty(c));
}
/**
* @return Combination of two Collection, first non-empty is returned
*/
static > Monoid firstNonEmptySeq(C identity) {
return Monoid.of(identity,VavrSemigroups.firstNonEmptySeq());
}
static > Monoid firstNonEmptySet(C identity) {
return Monoid.of(identity,VavrSemigroups.firstNonEmptySet());
}
static Monoid> firstNonEmptyList() {
return VavrMonoids.firstNonEmptySeq(List.empty());
}
static Monoid> firstNonEmptyVector() {
return VavrMonoids.firstNonEmptySeq(Vector.empty());
}
static Monoid firstNonEmptyCharSeq() {
return VavrMonoids.firstNonEmptySeq(CharSeq.empty());
}
static Monoid> firstNonEmptyArray() {
return VavrMonoids.firstNonEmptySeq(Array.empty());
}
static Monoid> firstNonEmptyStream() {
return VavrMonoids.firstNonEmptySeq(Stream.empty());
}
static Monoid> firstNonEmptyQueue() {
return VavrMonoids.firstNonEmptySeq(Queue.empty());
}
static Monoid> firstNonEmptylinkedHashSet() {
return VavrMonoids.firstNonEmptySet(LinkedHashSet.empty());
}
static Monoid> firstNonEmptyHashSet() {
return VavrMonoids.firstNonEmptySet(HashSet.empty());
}
static > Monoid> firstNonEmptyTreeSet() {
return VavrMonoids.>firstNonEmptySet(TreeSet.empty());
}
/**
* @return Combination of two Collection, last non-empty is returned
*/
static > Monoid lastNonEmptySeq(C identity) {
return Monoid.of(identity,VavrSemigroups.lastNonEmptySeq());
}
/**
* @return Combination of two Collection, last non-empty is returned
*/
static > Monoid lastNonEmptySet(C identity) {
return Monoid.of(identity,VavrSemigroups.lastNonEmptySet());
}
static Monoid> lastNonEmptyList() {
return VavrMonoids.lastNonEmptySeq(List.empty());
}
static Monoid> lastNonEmptyVector() {
return VavrMonoids.lastNonEmptySeq(Vector.empty());
}
static Monoid lastNonEmptyCharSeq() {
return VavrMonoids.lastNonEmptySeq(CharSeq.empty());
}
static Monoid> lastNonEmptyArray() {
return VavrMonoids.lastNonEmptySeq(Array.empty());
}
static Monoid> lastNonEmptyStream() {
return VavrMonoids.lastNonEmptySeq(Stream.empty());
}
static Monoid> lastNonEmptyQueue() {
return VavrMonoids.lastNonEmptySeq(Queue.empty());
}
static Monoid> lastNonEmptylinkedHashSet() {
return VavrMonoids.lastNonEmptySet(LinkedHashSet.empty());
}
static Monoid> lastNonEmptyHashSet() {
return VavrMonoids.lastNonEmptySet(HashSet.empty());
}
static > Monoid> lastNonEmptyTreeSet() {
return VavrMonoids.>lastNonEmptySet(TreeSet.empty());
}
/**
* @return Combine two Future's by taking the first result
*/
static Monoid> firstCompleteFuture() {
return Monoid.of(Promise.make().future(),VavrSemigroups.firstCompleteFuture());
}
/**
* @return Combine two Future's by taking the first successful
*/
static Monoid> firstSuccessfulFuture() {
return Monoid.of(Promise.make().future(),VavrSemigroups.firstSuccessfulFuture());
}
/**
* @return Combine two Either's by taking the first primary
*/
static Monoid> firstRightEither() {
return Monoid.of(Either.left(null),VavrSemigroups.firstRightEither());
}
/**
* @return Combine two Either's by taking the first secondary
*/
static Monoid> firstLeftEither() {
return Monoid.of(Either.right(null),VavrSemigroups.firstLeftEither());
}
/**
* @return Combine two Either's by taking the last primary
*/
static Monoid> lastRightEither() {
return Monoid.of(Either.left(null),VavrSemigroups.lastRightEither());
}
/**
* @return Combine two Either's by taking the last secondary
*/
static Monoid> lastLeftEither() {
return Monoid.of(Either.right(null),VavrSemigroups.lastLeftEither());
}
/**
* @return Combine two Try's by taking the first primary
*/
static Monoid> firstTrySuccess() {
return Monoid.of(Try.failure(new NoSuchElementException()),VavrSemigroups.firstTrySuccess());
}
/**
* @return Combine two Try's by taking the first secondary
*/
static Monoid> firstTryFailure() {
return Monoid.of(Try.success(null),VavrSemigroups.firstTryFailure());
}
/**
* @return Combine two Tryr's by taking the last primary
*/
static Monoid> lastTrySuccess() {
return Monoid.of(Try.failure(new NoSuchElementException()),VavrSemigroups.lastTrySuccess());
}
/**
* @return Combine two Try's by taking the last secondary
*/
static Monoid>lastTryFailure() {
return Monoid.of(Try.success(null),VavrSemigroups.lastTryFailure());
}
/**
* @return Combine two Options by taking the first present
*/
static Monoid