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

ru.progrm_jarvis.javacommons.range.LongRange Maven / Gradle / Ivy

package ru.progrm_jarvis.javacommons.range;

import lombok.NonNull;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import ru.progrm_jarvis.javacommons.collection.Iterables;
import ru.progrm_jarvis.javacommons.ownership.annotation.Own;
import ru.progrm_jarvis.javacommons.ownership.annotation.Ref;
import ru.progrm_jarvis.javacommons.util.function.LongPredicate;

import java.util.*;

/**
 * {@link Range} specialization for {@code long}.
 */
@FunctionalInterface
public interface LongRange extends LongPredicate {

    /* ************************************************* Factories ************************************************* */

    /**
     * Creates a range [-∞ ; +∞].
     *
     * @return created range
     */
    static @NotNull LongRange any() {
        return value -> true;
    }

    /**
     * Creates a range .
     *
     * @return created range
     */
    static @NotNull LongRange none() {
        return value -> false;
    }

    /**
     * Creates a range {{@code value}}.
     *
     * @param value the only value contained by the range
     * @return created range
     */
    static @NotNull LongRange only(final long value) {
        return tested -> tested == value;
    }

    /**
     * Creates a range {x: x ∈ {@code values}}.
     *
     * @param values the only values contained by the range
     * @return created range
     *
     * @apiNote this takes ownership over {@code values}
     */
    static @NotNull LongRange only(final long @Own @NonNull ... values) {
        Arrays.sort(values);

        return value -> Arrays.binarySearch(values, value) >= 0;
    }

    /**
     * Creates a range {x: x ∈ {@code values}}.
     *
     * @param values the only values contained by the range
     * @return created range
     *
     * @apiNote this takes ownership over {@code values}
     */
    static @NotNull LongRange only(final @Ref @NonNull Collection<@NotNull Long> values) {
        return values::contains;
    }

    /**
     * Creates a range {x: x ∈ {@code values}}.
     *
     * @param values the only values contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} not preserving order
     */
    static @NotNull LongRange onlyCopy(final long @Ref @NonNull ... values) {
        return only(Arrays.copyOf(values, values.length));
    }

    /**
     * Creates a range {x: x ∈ {@code values}}.
     *
     * @param values the only values contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} not preserving order
     */
    static @NotNull LongRange onlyCopy(final @Ref @NonNull Collection<@NotNull Long> values) {
        return only(new HashSet<>(values));
    }

    /**
     * Creates a range {x: x ∈ {@code values}}.
     *
     * @param values the only values contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} preserving order
     */
    static @NotNull LongRange onlyCopyOrdered(final @Ref @NonNull Collection<@NotNull Long> values) {
        return only(new ArrayList<>(values));
    }

    /**
     * Creates a range ¬{{@code value}}.
     *
     * @param value the only value not contained by the range
     * @return created range
     */
    static @NotNull LongRange except(final long value) {
        return tested -> tested != value;
    }

    /**
     * Creates a range {x: x ∉ {@code values}}.
     *
     * @param values the only values not contained by the range
     * @return created range
     *
     * @apiNote this takes ownership over {@code values}
     */
    static @NotNull LongRange except(final long @Own @NonNull ... values) {
        Arrays.sort(values);

        return value -> Arrays.binarySearch(values, value) < 0;
    }

    /**
     * Creates a range {x: x ∉ {@code values}}.
     *
     * @param values the only values not contained by the range
     * @return created range
     *
     * @apiNote this takes ownership over {@code values}
     */
    static @NotNull LongRange except(final @Ref @NonNull Collection<@NotNull Long> values) {
        return value -> !values.contains(value);
    }

    /**
     * Creates a range {x: x ∉ {@code values}}.
     *
     * @param values the only values not contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} not preserving order
     */
    static @NotNull LongRange exceptCopy(final long @Own @NonNull ... values) {
        return except(Arrays.copyOf(values, values.length));
    }

    /**
     * Creates a range {x: x ∉ {@code values}}.
     *
     * @param values the only values not contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} not preserving order
     */
    static @NotNull LongRange exceptCopy(final @Ref @NonNull Collection<@NotNull Long> values) {
        return except(new HashSet<>(values));
    }

    /**
     * Creates a range {x: x ∉ {@code values}}.
     *
     * @param values the only values not contained by the range
     * @return created range
     *
     * @apiNote this copies {@code values} preserving order
     */
    static @NotNull LongRange exceptCopyOrdered(final @Ref @NonNull Collection<@NotNull Long> values) {
        return except(new ArrayList<>(values));
    }

    /* ************************************************* Intervals ************************************************* */

    /**
     * Creates a range ({@code lowerBound}; +∞).
     *
     * @param lowerBound lower exclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange greater(final long lowerBound) {
        return value -> lowerBound < value;
    }

    /**
     * Creates a range [{@code lowerBound}; +∞).
     *
     * @param lowerBound lower inclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange greaterOrEqual(final long lowerBound) {
        return value -> lowerBound <= value;
    }

    /**
     * Creates a range (∞; {@code upperBound}).
     *
     * @param upperBound upper inclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange less(final long upperBound) {
        return value -> upperBound > value;
    }

    /**
     * Creates a range (∞; {@code upperBound}].
     *
     * @param upperBound upper exclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange lessOrEqual(final long upperBound) {
        return value -> upperBound >= value;
    }

    /**
     * Creates a range ({@code lowerBound}; {@code upperBound}).
     *
     * @param lowerBound lower exclusive bound of the range
     * @param upperBound upper exclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange between(final long lowerBound, final long upperBound) {
        return value -> lowerBound < value && upperBound > value;
    }

    /**
     * Creates a range [{@code lowerBound}; {@code upperBound}].
     *
     * @param lowerBound lower inclusive bound of the range
     * @param upperBound upper inclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange betweenOrEqual(final long lowerBound, final long upperBound) {
        return value -> lowerBound <= value && upperBound >= value;
    }

    /**
     * Creates a range ({@code lowerBound}; {@code upperBound}].
     *
     * @param lowerBound lower exclusive bound of the range
     * @param upperBound upper inclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange fromExclusiveTo(final long lowerBound, final long upperBound) {
        return value -> lowerBound < value && upperBound >= value;
    }

    /**
     * Creates a range [{@code lowerBound}; {@code upperBound}).
     *
     * @param lowerBound lower inclusive bound of the range
     * @param upperBound upper exclusive bound of the range
     * @return created range
     */
    static @NotNull LongRange fromToExclusive(final long lowerBound, final long upperBound) {
        return value -> lowerBound <= value && upperBound > value;
    }

    /* ************************************************** Joiners ************************************************** */

    /**
     * Creates a range {x: ∃ range ∈ ranges: x ∈ range}.
     *
     * @param ranges matched disjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange anyOf(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsLong(value)) return true;

            return false;
        };
    }

    /**
     * Creates a range {x: ∃ range ∈ ranges: x ∈ range}.
     *
     * @param ranges matched disjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange anyOf(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsLong(value)) return true;

            return false;
        };
    }

    /**
     * Creates a range {x: ∃ range ∈ ranges: x ∈ range}.
     *
     * @param ranges matched disjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange anyOfCopy(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return anyOf(Arrays.copyOf(ranges, ranges.length));
    }

    /**
     * Creates a range {x: ∃ range ∈ ranges: x ∈ range}.
     *
     * @param ranges matched disjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} not preserving order
     */
    static @NotNull LongRange anyOfCopy(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return anyOf(Iterables.toSet(ranges));
    }

    /**
     * Creates a range {x: ∃ range ∈ ranges: x ∈ range}.
     *
     * @param ranges matched disjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange anyOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return anyOf(Iterables.toList(ranges));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∈ range}.
     *
     * @param ranges matched conjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange allOf(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (!range.testAsLong(value)) return false;

            return true;
        };
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∈ range}.
     *
     * @param ranges matched conjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange allOf(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return value -> {
            for (val range : ranges) if (!range.testAsLong(value)) return false;

            return true;
        };
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∈ range}.
     *
     * @param ranges matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange allOfCopy(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return allOf(Arrays.copyOf(ranges, ranges.length));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∈ range}.
     *
     * @param ranges matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} not preserving order
     */
    static @NotNull LongRange allOfCopy(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return allOf(Iterables.toSet(ranges));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∈ range}.
     *
     * @param ranges matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange allOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return allOf(Iterables.toList(ranges));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∉ range}.
     *
     * @param ranges not matched conjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange noneOf(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsLong(value)) return false;

            return true;
        };
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∉ range}.
     *
     * @param ranges not matched conjunctive ranges
     * @return created range
     *
     * @apiNote this takes ownership over {@code ranges}
     */
    static @NotNull LongRange noneOf(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsLong(value)) return false;

            return true;
        };
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∉ range}.
     *
     * @param ranges not matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange noneOfCopy(final @Ref @NotNull LongRange @NonNull ... ranges) {
        return noneOf(Arrays.copyOf(ranges, ranges.length));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∉ range}.
     *
     * @param ranges not matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} not preserving order
     */
    static @NotNull LongRange noneOfCopy(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return noneOf(Iterables.toSet(ranges));
    }

    /**
     * Creates a range {x: ∀ range ∈ ranges, x ∉ range}.
     *
     * @param ranges not matched conjunctive ranges
     * @return created range
     *
     * @apiNote this copies {@code ranges} preserving order
     */
    static @NotNull LongRange noneOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull LongRange> ranges) {
        return noneOf(Iterables.toList(ranges));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy