
ru.progrm_jarvis.javacommons.range.ShortRange 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.ShortPredicate;
import java.util.*;
/**
* {@link Range} specialization for {@code short}.
*/
@FunctionalInterface
public interface ShortRange extends ShortPredicate {
/* ************************************************* Factories ************************************************* */
/**
* Creates a range [-∞ ; +∞].
*
* @return created range
*/
static @NotNull ShortRange any() {
return value -> true;
}
/**
* Creates a range ∅.
*
* @return created range
*/
static @NotNull ShortRange none() {
return value -> false;
}
/**
* Creates a range {{@code value}}.
*
* @param value the only value contained by the range
* @return created range
*/
static @NotNull ShortRange only(final short 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 ShortRange only(final short @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 ShortRange only(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange onlyCopy(final short @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 ShortRange onlyCopy(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange onlyCopyOrdered(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange except(final short 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 ShortRange except(final short @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 ShortRange except(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange exceptCopy(final short @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 ShortRange exceptCopy(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange exceptCopyOrdered(final @Ref @NonNull Collection<@NotNull Short> 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 ShortRange greater(final short lowerBound) {
return value -> lowerBound < value;
}
/**
* Creates a range [{@code lowerBound}; +∞).
*
* @param lowerBound lower inclusive bound of the range
* @return created range
*/
static @NotNull ShortRange greaterOrEqual(final short lowerBound) {
return value -> lowerBound <= value;
}
/**
* Creates a range (∞; {@code upperBound}).
*
* @param upperBound upper inclusive bound of the range
* @return created range
*/
static @NotNull ShortRange less(final short upperBound) {
return value -> upperBound > value;
}
/**
* Creates a range (∞; {@code upperBound}].
*
* @param upperBound upper exclusive bound of the range
* @return created range
*/
static @NotNull ShortRange lessOrEqual(final short 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 ShortRange between(final short lowerBound, final short 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 ShortRange betweenOrEqual(final short lowerBound, final short 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 ShortRange fromExclusiveTo(final short lowerBound, final short 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 ShortRange fromToExclusive(final short lowerBound, final short 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 ShortRange anyOf(final @Ref @NotNull ShortRange @NonNull ... ranges) {
return value -> {
for (val range : ranges) if (range.testAsShort(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 ShortRange anyOf(final @Ref @NonNull Iterable<@NotNull ShortRange> ranges) {
return value -> {
for (val range : ranges) if (range.testAsShort(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 ShortRange anyOfCopy(final @Ref @NotNull ShortRange @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 ShortRange anyOfCopy(final @Ref @NonNull Iterable<@NotNull ShortRange> 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 ShortRange anyOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ShortRange> 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 ShortRange allOf(final @Ref @NotNull ShortRange @NonNull ... ranges) {
return value -> {
for (val range : ranges) if (!range.testAsShort(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 ShortRange allOf(final @Ref @NonNull Iterable<@NotNull ShortRange> ranges) {
return value -> {
for (val range : ranges) if (!range.testAsShort(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 ShortRange allOfCopy(final @Ref @NotNull ShortRange @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 ShortRange allOfCopy(final @Ref @NonNull Iterable<@NotNull ShortRange> 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 ShortRange allOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ShortRange> 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 ShortRange noneOf(final @Ref @NotNull ShortRange @NonNull ... ranges) {
return value -> {
for (val range : ranges) if (range.testAsShort(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 ShortRange noneOf(final @Ref @NonNull Iterable<@NotNull ShortRange> ranges) {
return value -> {
for (val range : ranges) if (range.testAsShort(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 ShortRange noneOfCopy(final @Ref @NotNull ShortRange @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 ShortRange noneOfCopy(final @Ref @NonNull Iterable<@NotNull ShortRange> 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 ShortRange noneOfCopyOrdered(final @Ref @NonNull Iterable<@NotNull ShortRange> ranges) {
return noneOf(Iterables.toList(ranges));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy