added first deps implementation
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package p.studio.utilities.structures;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ReadOnlyCollection<T> implements Iterable<T> {
|
||||
protected final Collection<T> collection;
|
||||
|
||||
protected ReadOnlyCollection(final Collection<T> collection) {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyCollection<T> empty() {
|
||||
return ReadOnlyCollection.wrap(List.of());
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyCollection<T> wrap(final Collection<T> collection) {
|
||||
return new ReadOnlyCollection<>(collection);
|
||||
}
|
||||
|
||||
public static <T> boolean isEmpty(final ReadOnlyCollection<T> c) {
|
||||
if (c == null) return true;
|
||||
return c.isEmpty();
|
||||
}
|
||||
|
||||
public static <T> boolean isNotEmpty(final ReadOnlyCollection<T> c) {
|
||||
return !isEmpty(c);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return collection.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return collection.isEmpty();
|
||||
}
|
||||
|
||||
public boolean contains(final T o) {
|
||||
return collection.contains(o);
|
||||
}
|
||||
|
||||
public boolean containsAll(final ReadOnlyCollection<T> c) {
|
||||
return c.stream().allMatch(collection::contains);
|
||||
}
|
||||
|
||||
public boolean containsAny(final ReadOnlyCollection<T> c) {
|
||||
return c.stream().anyMatch(collection::contains);
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
return collection.iterator();
|
||||
}
|
||||
|
||||
public Stream<T> stream() {
|
||||
return collection.stream();
|
||||
}
|
||||
|
||||
public <R> Stream<R> map(final Function<T, R> mapper) {
|
||||
return stream().map(mapper);
|
||||
}
|
||||
|
||||
public T[] toArray(final Supplier<T[]> factory) {
|
||||
return collection.toArray(factory.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (o == null) return false;
|
||||
if (o instanceof ReadOnlyCollection<?> c) {
|
||||
return c.collection.equals(collection);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return collection.hashCode();
|
||||
}
|
||||
|
||||
public Spliterator<T> spliterator() {
|
||||
return collection.spliterator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package p.studio.utilities.structures;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ReadOnlyList<T> extends ReadOnlyCollection<T> {
|
||||
private ReadOnlyList(final List<T> list) {
|
||||
super(list);
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyList<T> empty() {
|
||||
return ReadOnlyList.wrap(List.of());
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyList<T> wrap(final List<T> list) {
|
||||
return new ReadOnlyList<>(list);
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyList<T> wrap(final ReadOnlyCollection<T> c) {
|
||||
if (ReadOnlyCollection.isEmpty(c)) return ReadOnlyList.empty();
|
||||
return ReadOnlyList.wrap(new ArrayList<>(c.collection));
|
||||
}
|
||||
|
||||
public static <T> ReadOnlyCollection<T> wrap(final Set<T> set) {
|
||||
return new ReadOnlyCollection<>(List.copyOf(set));
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> ReadOnlyList<T> from(final T... values) {
|
||||
if (values == null) return ReadOnlyList.empty();
|
||||
if (values.length == 0) return ReadOnlyList.empty();
|
||||
return ReadOnlyList.wrap(Stream.of(values).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (o == null) return false;
|
||||
if (o instanceof ReadOnlyList<?> c) {
|
||||
return c.collection.equals(collection);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public T get(final int index) {
|
||||
return ((List<T>)collection).get(index);
|
||||
}
|
||||
|
||||
public int indexOf(final T o) {
|
||||
return ((List<T>)collection).indexOf(o);
|
||||
}
|
||||
|
||||
public int lastIndexOf(final T o) {
|
||||
return ((List<T>)collection).lastIndexOf(o);
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator() {
|
||||
return ((List<T>)collection).listIterator();
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator(final int index) {
|
||||
return ((List<T>)collection).listIterator(index);
|
||||
}
|
||||
|
||||
public ReadOnlyList<T> subList(final int fromIndex, final int toIndex) {
|
||||
return ReadOnlyList.wrap(((List<T>)collection).subList(fromIndex, toIndex));
|
||||
}
|
||||
|
||||
public T getFirst() {
|
||||
return ((List<T>)collection).getFirst();
|
||||
}
|
||||
|
||||
public T getLast() {
|
||||
return ((List<T>)collection).getLast();
|
||||
}
|
||||
|
||||
public ReadOnlyList<T> sort(final Comparator<? super T> c) {
|
||||
Stream<T> s = collection.stream();
|
||||
if (Objects.isNull(c)) {
|
||||
s = s.sorted();
|
||||
} else {
|
||||
s = s.sorted(c);
|
||||
}
|
||||
return ReadOnlyList.wrap(s.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package p.studio.utilities.structures;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ReadOnlySet<T> extends ReadOnlyCollection<T> {
|
||||
protected ReadOnlySet(final Set<T> set) {
|
||||
super(set);
|
||||
}
|
||||
|
||||
public static <T> ReadOnlySet<T> empty() {
|
||||
return ReadOnlySet.wrap(Set.of());
|
||||
}
|
||||
|
||||
public static <T> ReadOnlySet<T> wrap(final Set<T> set) {
|
||||
return new ReadOnlySet<>(set);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> ReadOnlySet<T> from(final T... values) {
|
||||
if (values == null) return ReadOnlySet.empty();
|
||||
if (values.length == 0) return ReadOnlySet.empty();
|
||||
return ReadOnlySet.wrap(Stream.of(values).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (o == null) return false;
|
||||
if (o instanceof ReadOnlySet<?> c) {
|
||||
return c.collection.equals(collection);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user