io.polaris.core.collection.Iterables Maven / Gradle / Ivy
package io.polaris.core.collection;
import javax.annotation.Nonnull;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author Qt
* @since 1.8
*/
public class Iterables {
public static Iterator iterator(Enumeration enumeration) {
return new Iterator() {
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}
@Override
public E next() {
return enumeration.nextElement();
}
};
}
public static Enumeration enumeration(Iterable iterable) {
return enumeration(iterable.iterator());
}
public static Enumeration enumeration(Iterator iterator) {
return new Enumeration() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public E nextElement() {
return iterator.next();
}
};
}
public static , E> C asCollection(Supplier supplier, Enumeration enumeration) {
C c = supplier.get();
while (enumeration.hasMoreElements()) {
c.add(enumeration.nextElement());
}
return c;
}
public static , E> C asCollection(Supplier supplier, E... iterable) {
C c = supplier.get();
Collections.addAll(c, iterable);
return c;
}
public static , E> C asCollection(Supplier supplier, Iterable iterable) {
C c = supplier.get();
for (E e : iterable) {
c.add(e);
}
return c;
}
public static , E> C asCollection(Supplier supplier, Iterator iterator) {
C c = supplier.get();
while (iterator.hasNext()) {
c.add(iterator.next());
}
return c;
}
public static List asList(Enumeration enumeration) {
List c = new ArrayList<>();
while (enumeration.hasMoreElements()) {
c.add(enumeration.nextElement());
}
return c;
}
public static Set asSet(Enumeration enumeration) {
Set c = new HashSet<>();
while (enumeration.hasMoreElements()) {
c.add(enumeration.nextElement());
}
return c;
}
public static List asList(E... iterable) {
List c = new ArrayList<>();
Collections.addAll(c, iterable);
return c;
}
public static Set asSet(E... iterable) {
Set c = new HashSet<>();
Collections.addAll(c, iterable);
return c;
}
public static List asList(Iterable iterable) {
List c = new ArrayList<>();
for (E e : iterable) {
c.add(e);
}
return c;
}
public static Set asSet(Iterable iterable) {
Set c = new HashSet<>();
for (E e : iterable) {
c.add(e);
}
return c;
}
public static List asList(Iterator iterator) {
List c = new ArrayList<>();
while (iterator.hasNext()) {
c.add(iterator.next());
}
return c;
}
public static Set asSet(Iterator iterator) {
Set c = new HashSet<>();
while (iterator.hasNext()) {
c.add(iterator.next());
}
return c;
}
public static E[] copyOf(E[] array) {
return Arrays.copyOf(array, array.length);
}
public static Iterator convert(Iterator iterator, Function converter) {
return new Iterator() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public T next() {
return converter.apply(iterator.next());
}
@Override
public void remove() {
iterator.next();
}
};
}
public static T[] convert(S[] array, T[] target, Function converter) {
if (target.length < array.length) {
if (target.getClass() == Object[].class) {
target = (T[]) new Object[array.length];
} else {
target = (T[]) Array.newInstance(target.getClass().getComponentType(), array.length);
}
}
for (int i = 0; i < array.length; i++) {
target[i] = (T) converter.apply((S) array[i]);
}
return target;
}
public static Set convert(Set set, Function converter, Function reconvert) {
return new Set() {
@Override
public int size() {
return set.size();
}
@Override
public boolean isEmpty() {
return set.isEmpty();
}
@Override
public boolean contains(Object o) {
try {
return set.contains(converter.apply((S) o));
} catch (ClassCastException e) {
return false;
}
}
@Override
public Iterator iterator() {
return convert(set.iterator(), converter);
}
@Override
public Object[] toArray() {
Object[] origin = set.toArray();
Object[] array = new Object[origin.length];
for (int i = 0; i < array.length; i++) {
array[i] = converter.apply((S) origin[i]);
}
return array;
}
@Override
public E[] toArray(E[] a) {
int size = size();
if (a.length < size) {
if (a.getClass() == Object[].class) {
a = (E[]) new Object[size];
} else {
a = (E[]) Array.newInstance(a.getClass().getComponentType(), size);
}
}
Object[] origin = set.toArray();
for (int i = 0; i < size; i++) {
a[i] = (E) converter.apply((S) origin[i]);
}
return a;
}
@Override
public boolean add(T t) {
if (reconvert != null) {
return set.add(reconvert.apply(t));
} else {
throw new UnsupportedOperationException();
}
}
@Override
public boolean remove(Object o) {
if (reconvert != null) {
return set.remove(reconvert.apply((T) o));
} else {
throw new UnsupportedOperationException();
}
}
@Override
public boolean containsAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection extends T> c) {
if (reconvert != null) {
boolean changed = false;
for (T t : c) {
if (set.add(reconvert.apply(t))) {
changed = true;
}
}
return changed;
} else {
throw new UnsupportedOperationException();
}
}
@Override
public boolean retainAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
};
}
public static boolean isMatchAll(S[] array1, T[] array2, BiFunction matcher) {
if (array1 == null && array2 == null) {
return true;
}
if (array1 == null || array2 == null) {
return false;
}
if (array1.length != array1.length) {
return false;
}
for (int i = 0; i < array1.length; i++) {
Boolean matched = matcher.apply(array1[i], array2[i]);
if (!matched) {
return false;
}
}
return true;
}
public static boolean isEmpty(Collection array) {
return array == null || array.isEmpty();
}
public static boolean isEmpty(E[] array) {
return array == null || array.length == 0;
}
public static boolean isNotEmpty(Collection array) {
return array != null && !array.isEmpty();
}
public static boolean isNotEmpty(E[] array) {
return array != null && array.length > 0;
}
public static boolean hasNull(E[] array) {
if (array == null) {
return false;
}
for (E e : array) {
if (e == null) {
return true;
}
}
return false;
}
public static boolean hasNull(Iterable array) {
if (array == null) {
return false;
}
for (E e : array) {
if (e == null) {
return true;
}
}
return false;
}
public static boolean isMatchAny(E[] array, Function matcher) {
if (array == null) {
return false;
}
for (E e : array) {
if (matcher.apply(e)) {
return true;
}
}
return false;
}
public static boolean isMatchAny(Iterable array, Function matcher) {
if (array == null) {
return false;
}
for (E e : array) {
if (matcher.apply(e)) {
return true;
}
}
return false;
}
public static boolean isMatchAll(E[] array, Function matcher) {
if (array == null) {
return false;
}
for (E e : array) {
if (!matcher.apply(e)) {
return false;
}
}
return true;
}
public static boolean isMatchAll(Iterable array, Function matcher) {
if (array == null) {
return false;
}
for (E e : array) {
if (!matcher.apply(e)) {
return false;
}
}
return true;
}
public static String toArrayString(@Nonnull Object obj) {
if (obj == null) {
return null;
} else if (obj instanceof long[]) {
return Arrays.toString((long[]) obj);
} else if (obj instanceof int[]) {
return Arrays.toString((int[]) obj);
} else if (obj instanceof short[]) {
return Arrays.toString((short[]) obj);
} else if (obj instanceof char[]) {
return Arrays.toString((char[]) obj);
} else if (obj instanceof byte[]) {
return Arrays.toString((byte[]) obj);
} else if (obj instanceof boolean[]) {
return Arrays.toString((boolean[]) obj);
} else if (obj instanceof float[]) {
return Arrays.toString((float[]) obj);
} else if (obj instanceof double[]) {
return Arrays.toString((double[]) obj);
} else if (obj.getClass().isArray()) {
try {
return Arrays.deepToString((Object[]) obj);
} catch (Exception ignore) {
}
}
return obj.toString();
}
}