All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.marksman.collectionviews.ImmutableSets Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package dev.marksman.collectionviews;

import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.functions.builtin.fn2.Take;

import java.util.HashSet;
import java.util.Objects;

import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.functions.builtin.fn2.ToCollection.toCollection;
import static dev.marksman.collectionviews.Validation.validateCopyFrom;

final class ImmutableSets {

    private ImmutableSets() {

    }

    static  ImmutableSet copyFrom(A[] source) {
        Objects.requireNonNull(source);
        return copyFrom(source.length, source);
    }

    static  ImmutableSet copyFrom(Iterable source) {
        Objects.requireNonNull(source);
        if (source instanceof ImmutableSet) {
            return (ImmutableSet) source;
        } else if (!source.iterator().hasNext()) {
            return Sets.empty();
        } else {
            return new ImmutableWrappedSet<>(toCollection(HashSet::new, source));
        }
    }

    static  ImmutableSet copyFrom(int maxCount, A[] source) {
        validateCopyFrom(maxCount, source);
        return copyFrom(maxCount, Vector.wrap(source));
    }

    static  ImmutableSet copyFrom(int maxCount, Iterable source) {
        validateCopyFrom(maxCount, source);
        if (maxCount == 0) {
            return Sets.empty();
        } else if (source instanceof ImmutableSet) {
            return copyFrom(maxCount, (ImmutableSet) source);
        } else {
            return copyFrom(Take.take(maxCount, source));
        }
    }

    private static  ImmutableSet copyFrom(int maxCount, ImmutableSet source) {
        if (source.size() <= maxCount) {
            return source;
        } else {
            return copyFrom(Take.take(maxCount, source));
        }
    }

    static  ImmutableSet ensureImmutable(Set set) {
        if (set instanceof ImmutableSet) {
            return (ImmutableSet) set;
        } else if (set.isEmpty()) {
            return Sets.empty();
        } else {
            HashSet copied = toCollection(HashSet::new, set);
            return new ImmutableWrappedSet<>(copied);
        }
    }

    static  ImmutableNonEmptySet ensureImmutable(NonEmptySet set) {
        if (set instanceof ImmutableNonEmptySet) {
            return (ImmutableNonEmptySet) set;
        } else {
            HashSet copied = toCollection(HashSet::new, set);
            return new ImmutableWrappedSet<>(copied);
        }
    }

    static  Maybe> maybeNonEmptyConvert(ImmutableSet source) {
        Objects.requireNonNull(source);
        if (source instanceof ImmutableNonEmptySet) {
            return just((ImmutableNonEmptySet) source);
        } else if (!source.isEmpty()) {
            return just(new ImmutableNonEmptySetAdapter<>(source));
        } else {
            return nothing();
        }
    }

    static  Maybe> maybeNonEmptyCopyFrom(A[] source) {
        Objects.requireNonNull(source);
        return maybeNonEmptyCopyFrom(source.length, source);
    }

    @SuppressWarnings("unchecked")
    static  Maybe> maybeNonEmptyCopyFrom(Iterable source) {
        Objects.requireNonNull(source);
        if (!source.iterator().hasNext()) {
            return nothing();
        } else {
            return (Maybe>) copyFrom(source).toNonEmpty();
        }
    }

    @SuppressWarnings("unchecked")
    static  Maybe> maybeNonEmptyCopyFrom(int maxCount, A[] source) {
        validateCopyFrom(maxCount, source);
        if (source.length == 0 || maxCount == 0) {
            return nothing();
        } else {
            return (Maybe>) copyFrom(maxCount, source).toNonEmpty();
        }
    }

    @SuppressWarnings("unchecked")
    static  Maybe> maybeNonEmptyCopyFrom(int maxCount, Iterable source) {
        validateCopyFrom(maxCount, source);
        if (maxCount == 0 || !source.iterator().hasNext()) {
            return nothing();
        } else {
            return (Maybe>) copyFrom(maxCount, source).toNonEmpty();
        }
    }

    static  ImmutableNonEmptySet nonEmptyConvertOrThrow(ImmutableSet source) {
        return getNonEmptyOrThrow(maybeNonEmptyConvert(source));
    }

    static  ImmutableNonEmptySet nonEmptyCopyFromOrThrow(A[] source) {
        return getNonEmptyOrThrow(maybeNonEmptyCopyFrom(source));
    }

    static  ImmutableNonEmptySet nonEmptyCopyFromOrThrow(Iterable source) {
        return getNonEmptyOrThrow(maybeNonEmptyCopyFrom(source));
    }

    static  ImmutableNonEmptySet nonEmptyCopyFromOrThrow(int maxCount, A[] source) {
        return getNonEmptyOrThrow(maybeNonEmptyCopyFrom(maxCount, source));
    }

    static  ImmutableNonEmptySet nonEmptyCopyFromOrThrow(int maxCount, Iterable source) {
        return getNonEmptyOrThrow(maybeNonEmptyCopyFrom(maxCount, source));
    }

    private static  ImmutableNonEmptySet getNonEmptyOrThrow(Maybe> maybeResult) {
        return maybeResult.orElseThrow(Sets.nonEmptyError());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy