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

ru.progrm_jarvis.javacommons.range.FloatRange 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.FloatPredicate;

import java.util.*;

/**
 * {@link Range} specialization for {@code float}.
 */
@FunctionalInterface
public interface FloatRange extends FloatPredicate {

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

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

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

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

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy