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

ru.progrm_jarvis.javacommons.range.CharRange 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.CharPredicate;

import java.util.*;

/**
 * {@link Range} specialization for {@code char}.
 */
@FunctionalInterface
public interface CharRange extends CharPredicate {

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

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

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

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

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy