All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vertexium.util.IterableUtils Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
package org.vertexium.util;

import org.vertexium.Element;

import java.lang.reflect.Array;
import java.util.*;

import static org.vertexium.util.CloseableUtils.closeQuietly;

public class IterableUtils {
    @SuppressWarnings("unchecked")
    public static  List toList(Iterable iterable) {
        if (iterable instanceof List) {
            return (List) iterable;
        }
        List results = new ArrayList<>();
        for (T o : iterable) {
            results.add(o);
        }
        closeQuietly(iterable);
        return results;
    }

    public static  List toList(Iterator iterator) {
        List results = new ArrayList<>();
        while (iterator.hasNext()) {
            T o = iterator.next();
            results.add(o);
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    public static  Set toSet(Iterable iterable) {
        if (iterable instanceof Set) {
            return (Set) iterable;
        }
        Set results = new HashSet<>();
        for (T o : iterable) {
            results.add(o);
        }
        closeQuietly(iterable);
        return results;
    }

    @SuppressWarnings("unchecked")
    public static  Set toSet(T[] iterable) {
        Set results = new HashSet<>();
        Collections.addAll(results, iterable);
        return results;
    }

    @SuppressWarnings("unchecked")
    public static  T[] toArray(Iterable iterable, Class type) {
        if (iterable instanceof Collection) {
            T[] array = (T[]) Array.newInstance(type, ((Collection) iterable).size());
            return ((Collection) iterable).toArray(array);
        }
        List list = toList(iterable);
        T[] array = (T[]) Array.newInstance(type, list.size());
        return list.toArray(array);
    }

    public static  int count(Iterable iterable) {
        if (iterable instanceof Collection) {
            return ((Collection) iterable).size();
        }

        int count = 0;
        for (T ignore : iterable) {
            count++;
        }
        closeQuietly(iterable);
        return count;
    }

    public static  int count(Iterator iterator) {
        int count = 0;
        while (iterator.hasNext()) {
            count++;
            iterator.next();
        }
        closeQuietly(iterator);
        return count;
    }

    public static  boolean isEmpty(Iterable iterable) {
        Iterator iterator = iterable.iterator();
        try {
            return !iterator.hasNext();
        } finally {
            closeQuietly(iterator);
        }
    }

    public static  Iterable toIterable(final T[] arr) {
        return () -> new Iterator() {
            int index = 0;

            @Override
            public boolean hasNext() {
                return index < arr.length;
            }

            @Override
            public T next() {
                return arr[index++];
            }

            @Override
            public void remove() {
                throw new RuntimeException("Not supported");
            }
        };
    }

    public static  T single(final Iterable it) {
        Iterator i = it.iterator();
        if (!i.hasNext()) {
            closeQuietly(i, it);
            throw new IllegalStateException("No items found.");
        }

        T result = i.next();

        if (i.hasNext()) {
            closeQuietly(i, it);
            throw new IllegalStateException("More than 1 item found.");
        }

        closeQuietly(i, it);
        return result;
    }

    public static  T singleOrDefault(final Iterable it, T defaultValue) {
        Iterator i = it.iterator();
        if (!i.hasNext()) {
            closeQuietly(i, it);
            return defaultValue;
        }

        T result = i.next();

        if (i.hasNext()) {
            T nextValue = i.next();
            closeQuietly(i, it);
            throw new IllegalStateException("More than 1 item found. [" + result + ", " + nextValue + "...]");
        }

        closeQuietly(i, it);
        return result;
    }

    public static String join(Iterable items, String sep) {
        boolean first = true;
        StringBuilder sb = new StringBuilder();
        for (Object o : items) {
            if (!first) {
                sb.append(sep);
            }
            sb.append(o);
            first = false;
        }
        return sb.toString();
    }

    @SuppressWarnings("unchecked")
    public static Iterable toElementIterable(Iterable elements) {
        return (Iterable) elements;
    }

    public static  Map toMapById(Iterable elements) {
        Map result = new HashMap<>();
        for (T element : elements) {
            if (element != null) {
                result.put(element.getId(), element);
            }
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy