From a581502a55310854ff7a81582d738f793f52c970 Mon Sep 17 00:00:00 2001 From: Cyrus Chan Date: Sun, 16 Jan 2022 16:29:27 +0800 Subject: [PATCH] Add interface for range --- .../collections/SemVerMultipleRange.java | 16 +- .../collections/SemVerRangeCollection.java | 151 ++++++++++++++++++ 2 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 src/main/java/xyz/rk0cc/josev/collections/SemVerRangeCollection.java diff --git a/src/main/java/xyz/rk0cc/josev/collections/SemVerMultipleRange.java b/src/main/java/xyz/rk0cc/josev/collections/SemVerMultipleRange.java index 93abc9d..42d9931 100644 --- a/src/main/java/xyz/rk0cc/josev/collections/SemVerMultipleRange.java +++ b/src/main/java/xyz/rk0cc/josev/collections/SemVerMultipleRange.java @@ -18,7 +18,7 @@ * @since 3.0.0 */ public abstract class SemVerMultipleRange - implements Set, SemVerDetermineInRange, Serializable { + implements Set, SemVerRangeCollection, SemVerDetermineInRange, Serializable { /** * Range item. */ @@ -199,13 +199,10 @@ public final Stream parallelStream() { } /** - * Get corresponded index of {@link Set} of {@link R}. - * - * @param index Index of {@link Set}. - * - * @return A {@link R range} object in the list. + * {@inheritDoc} */ @Nonnull + @Override public final R elementAt(@Nonnegative int index) { Optional optR = ranges.stream().skip(index).findFirst(); if (optR.isEmpty()) throw new IndexOutOfBoundsException(index); @@ -213,13 +210,10 @@ public final R elementAt(@Nonnegative int index) { } /** - * Get a {@link List} of {@link R} which matching the condition. - * - * @param condition Condition of searching. - * - * @return A {@link List} that matched the condition. + * {@inheritDoc} */ @Nonnull + @Override public final List where(@Nonnull Predicate condition) { return ranges.stream().filter(condition).toList(); } diff --git a/src/main/java/xyz/rk0cc/josev/collections/SemVerRangeCollection.java b/src/main/java/xyz/rk0cc/josev/collections/SemVerRangeCollection.java new file mode 100644 index 0000000..07a2997 --- /dev/null +++ b/src/main/java/xyz/rk0cc/josev/collections/SemVerRangeCollection.java @@ -0,0 +1,151 @@ +package xyz.rk0cc.josev.collections; + +import xyz.rk0cc.josev.SemVerRange; + +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import java.util.*; +import java.util.function.Predicate; + +/** + * Enhanced {@link Collection} interface for handling {@link SemVerRange}. + * + * @param Applied {@link SemVerRange}. + * + * @since 3.1.0 + */ +public interface SemVerRangeCollection extends Collection { + /** + * Get corresponded index of {@link R}'s {@link Collection}. + * + * @param index Index of {@link Collection}. + * + * @return A {@link R range} object in the list. + * + * @throws IndexOutOfBoundsException If index number is out of range. + */ + @Nonnull + R elementAt(@Nonnegative int index); + + /** + * Get a {@link List} of {@link R} which matching the condition. + * + * @param condition Condition of searching. + * + * @return A {@link List} that matched the condition. + */ + @Nonnull + default List where(@Nonnull Predicate condition) { + return stream().filter(condition).toList(); + } + + /** + * Parse multiple range to {@link List} based {@link SemVerRangeCollection}. + * + * @param ranges Applied ranges. + * @param Type uses for multiple range. + * + * @return {@link ArrayList} based {@link SemVerRangeCollection}. + */ + @SafeVarargs + @Nonnull + static SemVerRangeCollection buildList(R... ranges) { + return new GenericSemVerList<>(Arrays.asList(ranges)); + } + + /** + * Parse multiple range to {@link List} based {@link SemVerRangeCollection}. + * + * @param ranges Applied ranges under a {@link Collection}. + * @param Type uses for multiple range. + * + * @return {@link ArrayList} based {@link SemVerRangeCollection}. + */ + @Nonnull + static SemVerRangeCollection buildList(Collection ranges) { + return new GenericSemVerList<>(ranges); + } + + /** + * Parse multiple range to {@link Set} based {@link SemVerRangeCollection}. + * + * @param ranges Applied ranges. + * @param Type uses for multiple range. + * + * @return {@link LinkedHashSet} based {@link SemVerRangeCollection}. + */ + @SafeVarargs + @Nonnull + static SemVerRangeCollection buildSet(R... ranges) { + return new GenericSemVerSet<>(Arrays.asList(ranges)); + } + + /** + * Parse multiple range to {@link Set} based {@link SemVerRangeCollection}. + * + * @param ranges Applied ranges under a {@link Collection}. + * @param Type uses for multiple range. + * + * @return {@link LinkedHashSet} based {@link SemVerRangeCollection}. + */ + @Nonnull + static SemVerRangeCollection buildSet(Collection ranges) { + return new GenericSemVerSet<>(ranges); + } +} + +/** + * Generic list based for parsing from {@link SemVerRangeCollection#buildList(SemVerRange[])}. + * + * @param Applied range. + * + * @since 3.1.0 + */ +final class GenericSemVerList extends ArrayList implements SemVerRangeCollection { + /** + * Create new generic list. + * + * @param origin Origin collection. + */ + GenericSemVerList(@Nonnull Collection origin) { + super(origin); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public R elementAt(@Nonnegative int index) { + return get(index); + } +} + +/** + * Generic set based for parsing from {@link SemVerRangeCollection#buildSet(SemVerRange[])}. + * + * @param Applied range. + * + * @since 3.1.0 + */ +final class GenericSemVerSet extends LinkedHashSet implements SemVerRangeCollection { + /** + * Create new generic set. + * + * @param origin Origin collection. + */ + GenericSemVerSet(@Nonnull Collection origin) { + super(origin); + } + + /** + * {@inheritDoc} + */ + @Nonnull + @Override + public R elementAt(int index) { + Optional o = stream().skip(index).findFirst(); + if (o.isEmpty()) throw new IndexOutOfBoundsException(index); + return o.get(); + } +} \ No newline at end of file