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

ru.progrm_jarvis.javacommons.range.ByteRange 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.BytePredicate;

import java.util.*;

/**
 * {@link Range} specialization for {@code byte}.
 */
@FunctionalInterface
public interface ByteRange extends BytePredicate {

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

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

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

    /**
     * Creates a range {{@code value}}.
     *
     * @param value the only value contained by the range
     * @return created range
     */
    static @NotNull ByteRange only(final byte 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 ByteRange only(final byte @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 ByteRange only(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange onlyCopy(final byte @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 ByteRange onlyCopy(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange onlyCopyOrdered(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange except(final byte 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 ByteRange except(final byte @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 ByteRange except(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange exceptCopy(final byte @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 ByteRange exceptCopy(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange exceptCopyOrdered(final @Ref @NonNull Collection<@NotNull Byte> 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 ByteRange greater(final byte lowerBound) {
        return value -> lowerBound < value;
    }

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

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

    /**
     * Creates a range (∞; {@code upperBound}].
     *
     * @param upperBound upper exclusive bound of the range
     * @return created range
     */
    static @NotNull ByteRange lessOrEqual(final byte 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 ByteRange between(final byte lowerBound, final byte 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 ByteRange betweenOrEqual(final byte lowerBound, final byte 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 ByteRange fromExclusiveTo(final byte lowerBound, final byte 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 ByteRange fromToExclusive(final byte lowerBound, final byte 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 ByteRange anyOf(final @Ref @NotNull ByteRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsByte(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 ByteRange anyOf(final @Ref @NonNull Iterable<@NotNull ByteRange> ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsByte(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 ByteRange anyOfCopy(final @Ref @NotNull ByteRange @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 ByteRange anyOfCopy(final @Ref @NonNull Iterable<@NotNull ByteRange> 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 ByteRange anyOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ByteRange> 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 ByteRange allOf(final @Ref @NotNull ByteRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (!range.testAsByte(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 ByteRange allOf(final @Ref @NonNull Iterable<@NotNull ByteRange> ranges) {
        return value -> {
            for (val range : ranges) if (!range.testAsByte(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 ByteRange allOfCopy(final @Ref @NotNull ByteRange @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 ByteRange allOfCopy(final @Ref @NonNull Iterable<@NotNull ByteRange> 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 ByteRange allOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ByteRange> 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 ByteRange noneOf(final @Ref @NotNull ByteRange @NonNull ... ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsByte(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 ByteRange noneOf(final @Ref @NonNull Iterable<@NotNull ByteRange> ranges) {
        return value -> {
            for (val range : ranges) if (range.testAsByte(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 ByteRange noneOfCopy(final @Ref @NotNull ByteRange @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 ByteRange noneOfCopy(final @Ref @NonNull Iterable<@NotNull ByteRange> 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 ByteRange noneOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ByteRange> ranges) {
        return noneOf(Iterables.toList(ranges));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy