package speiger.src.collections.PACKAGE.utils; import java.util.Arrays; import java.util.Collection; import java.util.Objects; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.BiFunction; #endif import java.util.function.Predicate; #if PRIMITIVES import java.util.function.JAVA_PREDICATE; #endif import java.util.function.Consumer; import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif import speiger.src.collections.objects.utils.ObjectArrays; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.utils.ARRAYS; #endif import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.utils.ITrimmable; import speiger.src.collections.utils.SanityChecks; /** * A Helper class for Collections */ public class COLLECTIONS { /** * Empty Collection Reference */ public static final COLLECTION NO_GENERIC_TYPE EMPTY = new EmptyCollectionBRACES(); /** * Returns a Immutable EmptyCollection instance that is automatically casted. * @Type(T) * @return an empty collection */ public static GENERIC_KEY_BRACES COLLECTION KEY_GENERIC_TYPE empty() { #if TYPE_OBJECT return (COLLECTION)EMPTY; #else return EMPTY; #endif } /** * Returns a Immutable Collection instance based on the instance given. * @param c that should be made immutable/unmodifiable * @Type(T) * @return a unmodifiable collection wrapper. If the Collection already a unmodifiable wrapper then it just returns itself. */ public static GENERIC_KEY_BRACES COLLECTION KEY_GENERIC_TYPE unmodifiable(COLLECTION KEY_GENERIC_TYPE c) { return c instanceof UnmodifiableCollection ? c : new UnmodifiableCollectionBRACES(c); } /** * Returns a synchronized Collection instance based on the instance given. * @param c that should be synchronized * @Type(T) * @return a synchronized collection wrapper. If the Collection already a synchronized wrapper then it just returns itself. */ public static GENERIC_KEY_BRACES COLLECTION KEY_GENERIC_TYPE synchronize(COLLECTION KEY_GENERIC_TYPE c) { return c instanceof SynchronizedCollection ? c : new SynchronizedCollectionBRACES(c); } /** * Returns a synchronized Collection instance based on the instance given. * @param c that should be synchronized * @param mutex is the controller of the synchronization block. * @Type(T) * @return a synchronized collection wrapper. If the Collection already a synchronized wrapper then it just returns itself. */ public static GENERIC_KEY_BRACES COLLECTION KEY_GENERIC_TYPE synchronize(COLLECTION KEY_GENERIC_TYPE c, Object mutex) { return c instanceof SynchronizedCollection ? c : new SynchronizedCollectionBRACES(c, mutex); } protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() { return new CollectionWrapperBRACES(); } protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper(int size) { return new CollectionWrapperBRACES(size); } protected static class CollectionWrapper KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE implements ITrimmable { KEY_TYPE[] elements; int size = 0; public CollectionWrapper() { this(10); } public CollectionWrapper(int size) { if(size < 0) throw new IllegalStateException("Size has to be 0 or greater"); elements = NEW_KEY_ARRAY(size); } @Override public boolean add(KEY_TYPE o) { if(size >= elements.length) elements = Arrays.copyOf(elements, (int)Math.min((long)elements.length + (elements.length >> 1), SanityChecks.MAX_ARRAY_SIZE)); elements[size++] = o; return true; } public KEY_TYPE GET_KEY(int index) { if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); return elements[index]; } #if TYPE_OBJECT @Override public boolean remove(Object e) { for(int i = 0;i= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); size--; if(index != size) System.arraycopy(elements, index+1, elements, index, size - index); } @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return new ITERATOR KEY_GENERIC_TYPE() { int index = 0; int lastReturned = -1; @Override public boolean hasNext() { return index < size; } @Override public KEY_TYPE NEXT() { int i = index++; return elements[(lastReturned = i)]; } @Override public void remove() { if(lastReturned == -1) throw new IllegalStateException(); removeIndex(lastReturned); index = lastReturned; lastReturned = -1; } }; } @Override public int size() { return size; } @Override public void clear() { #if TYPE_OBJECT for(int i = 0;i c) { if(c != null) ARRAYS.stableSort(elements, size, c); else ARRAYS.stableSort(elements, size); } public void unstableSort(Comparator c) { if(c != null) ARRAYS.unstableSort(elements, size, c); else ARRAYS.unstableSort(elements, size); } #else public void sort(COMPARATOR c) { if(c != null) ARRAYS.stableSort(elements, size, c); else ARRAYS.stableSort(elements, size); } public void unstableSort(COMPARATOR c) { if(c != null) ARRAYS.unstableSort(elements, size, c); else ARRAYS.unstableSort(elements, size); } #endif @Override public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { Objects.requireNonNull(action); for(int i = 0;i void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { Objects.requireNonNull(action); for(int i = 0;i size() || size() == elements.length) return false; int value = Math.max(size, size()); elements = value == 0 ? EMPTY_KEY_ARRAY : Arrays.copyOf(elements, value); return true; } @Override public void clearAndTrim(int size) { if(elements.length <= size) { clear(); return; } elements = size == 0 ? EMPTY_KEY_ARRAY : NEW_KEY_ARRAY(size); this.size = size; } @Override @Primitive public Object[] toArray() { Object[] obj = new Object[size]; for(int i = 0;i E[] toArray(E[] a) { if(a == null) a = (E[])new Object[size]; else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size); #if TYPE_OBJECT System.arraycopy(elements, 0, a, 0, size); #else for(int i = 0;i size) a[size] = null; return a; } #if !TYPE_OBJECT @Override public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { if(a.length < size) a = new KEY_TYPE[size]; System.arraycopy(elements, 0, a, 0, size); if (a.length > size) a[size] = EMPTY_KEY_VALUE; return a; } #endif } /** * Synchronized Collection Wrapper for the synchronizedCollection function * @Type(T) */ public static class SynchronizedCollection KEY_GENERIC_TYPE implements COLLECTION KEY_GENERIC_TYPE { COLLECTION KEY_GENERIC_TYPE c; protected Object mutex; SynchronizedCollection(COLLECTION KEY_GENERIC_TYPE c) { this.c = c; mutex = this; } SynchronizedCollection(COLLECTION KEY_GENERIC_TYPE c, Object mutex) { this.c = c; this.mutex = mutex; } @Override public boolean add(KEY_TYPE o) { synchronized(mutex) { return c.add(o); } } @Override public boolean addAll(Collection c) { synchronized(mutex) { return this.c.addAll(c); } } @Override public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.addAll(c); } } @Override public boolean addAll(KEY_TYPE[] e, int offset, int length) { synchronized(mutex) { return c.addAll(e, offset, length); } } #if !TYPE_OBJECT @Override public boolean contains(KEY_TYPE o) { synchronized(mutex) { return c.contains(o); } } #else @Override public boolean contains(Object o) { synchronized(mutex) { return c.contains(o); } } #endif @Override @Primitive public boolean containsAll(Collection c) { synchronized(mutex) { return this.c.containsAll(c); } } @Override @Primitive public boolean containsAny(Collection c) { synchronized(mutex) { return this.c.containsAny(c); } } @Override public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.containsAll(c); } } @Override public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.containsAny(c); } } @Override public int size() { synchronized(mutex) { return c.size(); } } @Override public boolean isEmpty() { synchronized(mutex) { return c.isEmpty(); } } @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return c.iterator(); } @Override public COLLECTION KEY_GENERIC_TYPE copy() { synchronized(mutex) { return c.copy(); } } @Override @Primitive public boolean remove(Object o) { synchronized(mutex) { return c.remove(o); } } @Override @Primitive public boolean removeAll(Collection c) { synchronized(mutex) { return this.c.removeAll(c); } } @Override @Primitive public boolean retainAll(Collection c) { synchronized(mutex) { return this.c.retainAll(c); } } #if !TYPE_OBJECT @Override public boolean REMOVE_KEY(KEY_TYPE o) { synchronized(mutex) { return c.REMOVE_KEY(o); } } #endif @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.removeAll(c); } } @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.removeAll(c, r); } } @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.retainAll(c); } } @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.retainAll(c, r); } } #if PRIMITIVES @Override public boolean remIf(JAVA_PREDICATE filter){ synchronized(mutex) { return c.remIf(filter); } } #endif @Override public void clear() { synchronized(mutex) { c.clear(); } } @Override public Object[] toArray() { synchronized(mutex) { return c.toArray(); } } #if !TYPE_OBJECT @Override public T[] toArray(T[] a) { synchronized(mutex) { return c.toArray(a); } } @Override public KEY_TYPE[] TO_ARRAY() { synchronized(mutex) { return c.TO_ARRAY(); } } @Override public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { synchronized(mutex) { return c.TO_ARRAY(a); } } @Override public void forEach(CONSUMER action) { synchronized(mutex) { c.forEach(action); } } @Override @Deprecated public void forEach(Consumer action) { synchronized(mutex) { c.forEach(action); } } #else @Override public E[] toArray(E[] a) { synchronized(mutex) { return c.toArray(a); } } @Override public void forEach(Consumer action) { synchronized(mutex) { c.forEach(action); } } #endif @Override public int hashCode() { synchronized(mutex) { return c.hashCode(); } } @Override public boolean equals(Object obj) { if(obj == this) return true; synchronized(mutex) { return c.equals(obj); } } @Override public String toString() { synchronized(mutex) { return c.toString(); } } @Override public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { synchronized(mutex) { c.forEach(input, action); } } @Override public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesAny(filter); } } @Override public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesNone(filter); } } @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesAll(filter); } } #if TYPE_OBJECT public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { synchronized(mutex) { return c.reduce(identity, operator); } } #else @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } } #endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } } @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } } } /** * Unmodifyable Collection Wrapper for the unmodifyableCollection method * @Type(T) */ public static class UnmodifiableCollection KEY_GENERIC_TYPE implements COLLECTION KEY_GENERIC_TYPE { COLLECTION KEY_GENERIC_TYPE c; UnmodifiableCollection(COLLECTION KEY_GENERIC_TYPE c) { this.c = c; } @Override public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(KEY_TYPE[] e, int offset, int length) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT @Override public boolean contains(KEY_TYPE o) { return c.contains(o); } #else @Override public boolean contains(Object o) { return c.contains(o); } #endif @Override public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return this.c.containsAll(c); } @Override public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { return this.c.containsAny(c); } @Override @Primitive public boolean containsAny(Collection c) { return this.c.containsAny(c); } @Override @Primitive public boolean containsAll(Collection c) { return this.c.containsAll(c); } @Override public int size() { return c.size(); } @Override public boolean isEmpty() { return c.isEmpty(); } @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(c.iterator()); } @Override public COLLECTION KEY_GENERIC_TYPE copy() { return c.copy(); } @Override @Deprecated public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT @Override public boolean REMOVE_KEY(KEY_TYPE o) { throw new UnsupportedOperationException(); } #endif @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); } #if PRIMITIVES @Override public boolean remIf(JAVA_PREDICATE filter){ throw new UnsupportedOperationException(); } #endif @Override public void clear() { throw new UnsupportedOperationException(); } @Override public Object[] toArray() { return c.toArray(); } #if !TYPE_OBJECT @Override public T[] toArray(T[] a) { return c.toArray(a); } @Override public KEY_TYPE[] TO_ARRAY() { return c.TO_ARRAY(); } @Override public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { return c.TO_ARRAY(a); } @Override public void forEach(CONSUMER action) { c.forEach(action); } @Override @Deprecated public void forEach(Consumer action) { c.forEach(action); } #else @Override public E[] toArray(E[] a) { return c.toArray(a); } @Override public void forEach(Consumer action) { c.forEach(action); } #endif @Override public int hashCode() { return c.hashCode(); } @Override public boolean equals(Object obj) { return obj == this || c.equals(obj); } @Override public String toString() { return c.toString(); } @Override public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { c.forEach(input, action); } @Override public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesAny(filter); } @Override public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesNone(filter); } @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesAll(filter); } #if TYPE_OBJECT public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { return c.reduce(identity, operator); } #else @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); } #endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); } @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); } } /** * Empty Collection implementation for the empty collection function * @Type(T) */ public static class EmptyCollection KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE { @Override public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(KEY_TYPE[] e, int offset, int length) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT @Override public boolean contains(KEY_TYPE o) { return false; } @Override public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { return c.isEmpty(); } @Override public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { return false; } #else @Override public boolean contains(Object o) { return false; } #endif @Override @Primitive public boolean containsAny(Collection c) { return false; } @Override @Primitive public boolean containsAll(Collection c) { return c.isEmpty(); } @Override public int hashCode() { return 0; } @Override public boolean equals(Object o) { if(o == this) return true; if(!(o instanceof Collection)) return false; return ((Collection)o).isEmpty(); } @Override @Deprecated public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } @Override @Primitive public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } #if !TYPE_OBJECT @Override public boolean REMOVE_KEY(KEY_TYPE o) { throw new UnsupportedOperationException(); } #endif @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } #if PRIMITIVES @Override public boolean remIf(JAVA_PREDICATE filter){ throw new UnsupportedOperationException(); } #endif @Override public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; } #if !TYPE_OBJECT @Override public T[] toArray(T[] a) { if(a != null && a.length > 0) a[0] = null; return a; } @Override public KEY_TYPE[] TO_ARRAY() { return ARRAYS.EMPTY_ARRAY; } @Override public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { if(a != null && a.length > 0) a[0] = EMPTY_KEY_VALUE; return a; } #else @Override public E[] toArray(E[] a) { if(a != null && a.length > 0) a[0] = EMPTY_KEY_VALUE; return a; } #endif @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.empty(); } @Override public void clear() {} @Override public int size() { return 0; } @Override public EmptyCollection KEY_GENERIC_TYPE copy() { return this; } } }