[*] Migrate to jspecify annotations
This commit is contained in:
@@ -3,17 +3,18 @@ package de.siphalor.tweed5.utils.api.collection;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@EqualsAndHashCode
|
||||
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class ClassToInstanceMap<T> implements Iterable<T> {
|
||||
public class ClassToInstanceMap<T extends @NonNull Object> implements Iterable<T> {
|
||||
private final Map<Class<? extends T>, T> delegate;
|
||||
|
||||
public static <T> ClassToInstanceMap<T> backedBy(Map<Class<? extends T>, T> delegate) {
|
||||
public static <T extends @NonNull Object> ClassToInstanceMap<T> backedBy(Map<Class<? extends T>, T> delegate) {
|
||||
return new ClassToInstanceMap<>(delegate);
|
||||
}
|
||||
|
||||
@@ -41,11 +42,11 @@ public class ClassToInstanceMap<T> implements Iterable<T> {
|
||||
return (V) delegate.get(key);
|
||||
}
|
||||
|
||||
public <V extends T> V put(@NotNull V value) {
|
||||
public <V extends T> @Nullable V put(V value) {
|
||||
return (V) delegate.put((Class<? extends T>) value.getClass(), value);
|
||||
}
|
||||
|
||||
public <V extends T> V remove(Class<V> key) {
|
||||
public <V extends T> @Nullable V remove(Class<V> key) {
|
||||
return (V) delegate.remove(key);
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ public class ClassToInstanceMap<T> implements Iterable<T> {
|
||||
public Set<T> values() {
|
||||
return new AbstractSet<T>() {
|
||||
@Override
|
||||
public @NotNull Iterator<T> iterator() {
|
||||
public Iterator<T> iterator() {
|
||||
Iterator<Map.Entry<Class<? extends T>, T>> entryIterator = delegate.entrySet().iterator();
|
||||
return new Iterator<T>() {
|
||||
@Override
|
||||
@@ -88,7 +89,7 @@ public class ClassToInstanceMap<T> implements Iterable<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Iterator<T> iterator() {
|
||||
public Iterator<T> iterator() {
|
||||
return values().iterator();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package de.siphalor.tweed5.utils.api.collection;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
@@ -33,7 +33,7 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(@NotNull Object o) {
|
||||
public boolean contains(Object o) {
|
||||
return delegate.getOrDefault(o.getClass(), Collections.emptyList()).contains(o);
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Iterator<T> iterator() {
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private final Iterator<Map.Entry<Class<? extends T>, Collection<T>>> classIterator = delegate.entrySet().iterator();
|
||||
private Iterator<? extends T> listIterator;
|
||||
private @Nullable Iterator<? extends T> listIterator;
|
||||
private boolean keptElement;
|
||||
private boolean keptAnyElementInList;
|
||||
|
||||
@@ -82,14 +82,12 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Object @NotNull [] toArray() {
|
||||
public Object[] toArray() {
|
||||
return delegate.values().stream().flatMap(Collection::stream).toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public <S> S @NotNull [] toArray(@NotNull S @NotNull [] array) {
|
||||
public <S> S[] toArray(S[] array) {
|
||||
Class<?> clazz = array.getClass().getComponentType();
|
||||
return delegate.values().stream()
|
||||
.flatMap(Collection::stream)
|
||||
@@ -97,12 +95,12 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(@NotNull T value) {
|
||||
public boolean add(T value) {
|
||||
return delegate.computeIfAbsent(((Class<T>) value.getClass()), clazz -> collectionSupplier.get()).add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(@NotNull Object value) {
|
||||
public boolean remove(Object value) {
|
||||
Collection<T> values = delegate.get(value.getClass());
|
||||
if (values == null) {
|
||||
return false;
|
||||
@@ -116,19 +114,17 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public <U extends T> Collection<U> getAll(Class<U> clazz) {
|
||||
return (Collection<U>) Collections.unmodifiableCollection(delegate.getOrDefault(clazz, Collections.emptyList()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Collection<T> removeAll(Class<? extends T> clazz) {
|
||||
Collection<T> removed = delegate.remove(clazz);
|
||||
return removed == null ? Collections.emptyList() : Collections.unmodifiableCollection(removed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> values) {
|
||||
public boolean containsAll(Collection<?> values) {
|
||||
for (Object value : values) {
|
||||
if (!contains(value)) {
|
||||
return false;
|
||||
@@ -138,7 +134,7 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends T> values) {
|
||||
public boolean addAll(Collection<? extends T> values) {
|
||||
boolean changed = false;
|
||||
for (T value : values) {
|
||||
changed = add(value) || changed;
|
||||
@@ -147,7 +143,7 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> values) {
|
||||
public boolean removeAll(Collection<?> values) {
|
||||
boolean changed = false;
|
||||
for (Object value : values) {
|
||||
changed = remove(value) || changed;
|
||||
@@ -156,7 +152,7 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> values) {
|
||||
public boolean retainAll(Collection<?> values) {
|
||||
Map<Class<?>, ? extends List<?>> valuesByClass = values.stream()
|
||||
.collect(Collectors.groupingBy(Object::getClass));
|
||||
delegate.putAll((Map<Class<? extends T>, List<T>>)(Object) valuesByClass);
|
||||
@@ -178,37 +174,37 @@ public class ClassToInstancesMultimap<T> implements Collection<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Iterator<T> iterator() {
|
||||
public Iterator<T> iterator() {
|
||||
return delegate.values().stream().flatMap(Collection::stream).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(@NotNull T value) {
|
||||
public boolean add(T value) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(@NotNull Object value) {
|
||||
public boolean remove(Object value) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<T> removeAll(Class<? extends T> clazz) {
|
||||
public Collection<T> removeAll(Class<? extends T> clazz) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends T> values) {
|
||||
public boolean addAll(Collection<? extends T> values) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> values) {
|
||||
public boolean removeAll(Collection<?> values) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> values) {
|
||||
public boolean retainAll(Collection<?> values) {
|
||||
throw createUnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,22 +2,24 @@ package de.siphalor.tweed5.utils.api.collection;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@AllArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class InheritanceMap<T> {
|
||||
public class InheritanceMap<T extends @NonNull Object> {
|
||||
private static final InheritanceMap<Object> EMPTY = unmodifiable(new InheritanceMap<>(Object.class));
|
||||
|
||||
private final Class<T> baseClass;
|
||||
private final Map<T, Collection<Class<? extends T>>> instanceToClasses;
|
||||
private final Map<Class<? extends T>, Collection<T>> classToInstances;
|
||||
|
||||
public static <T> InheritanceMap<T> empty() {
|
||||
public static <T extends @NonNull Object> InheritanceMap<T> empty() {
|
||||
return (InheritanceMap<T>) EMPTY;
|
||||
}
|
||||
public static <T> InheritanceMap<T> unmodifiable(InheritanceMap<T> map) {
|
||||
public static <T extends @NonNull Object> InheritanceMap<T> unmodifiable(InheritanceMap<T> map) {
|
||||
return new Unmodifiable<>(map);
|
||||
}
|
||||
|
||||
@@ -49,7 +51,7 @@ public class InheritanceMap<T> {
|
||||
return (Collection<V>) classToInstances.getOrDefault(clazz, Collections.emptyList());
|
||||
}
|
||||
|
||||
public <V extends T> V getSingleInstance(Class<V> clazz) throws NonUniqueResultException {
|
||||
public <V extends T> @Nullable V getSingleInstance(Class<V> clazz) throws NonUniqueResultException {
|
||||
Collection<T> instances = classToInstances.getOrDefault(clazz, Collections.emptyList());
|
||||
if (instances.isEmpty()) {
|
||||
return null;
|
||||
@@ -87,7 +89,7 @@ public class InheritanceMap<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public <V extends T> V removeInstance(V instance) {
|
||||
public <V extends T> @Nullable V removeInstance(V instance) {
|
||||
if (!instanceToClasses.containsKey(instance)) {
|
||||
return null;
|
||||
}
|
||||
@@ -153,7 +155,7 @@ public class InheritanceMap<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class Unmodifiable<T> extends InheritanceMap<T> {
|
||||
private static class Unmodifiable<T extends @NonNull Object> extends InheritanceMap<T> {
|
||||
public Unmodifiable(InheritanceMap<T> delegate) {
|
||||
super(delegate.baseClass, delegate.instanceToClasses, delegate.classToInstances);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
@NullMarked
|
||||
package de.siphalor.tweed5.utils.api.collection;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
Reference in New Issue
Block a user