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

org.reflections8.util.ReflectionsIterables Maven / Gradle / Ivy

There is a newer version: 0.11.7
Show newest version
package org.reflections8.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.function.Function;

/**
 * @author aschoerk
 */
public class ReflectionsIterables {

    public static boolean isEmpty(Iterable iterable) {
        return iterable == null || !iterable.iterator().hasNext();
    }

    public static  T getOnlyElement(Iterable iterable) {
        if (iterable == null)
            throw new NoSuchElementException();
        final Iterator iterator = iterable.iterator();
        if (!iterator.hasNext())
            throw new NoSuchElementException();
        T res = iterator.next();
        if (iterator.hasNext())
            throw new IllegalArgumentException();
        return res;
    }

    public static  Iterable concat(Iterable a,
                                         Iterable b) {
        List res = new ArrayList<>();
        for (T aEl: a) {
            res.add(aEl);
        }
        for (T bEl: b) {
            res.add(bEl);
        }
        return res;
    }

    public static  Set makeSetOf(Iterable a) {
        HashSet res = new HashSet<>();

        for (T aEl: a) {
            res.add(aEl);
        }
        return res;
    }


    public static boolean 	contains(Iterable iterable, Object element) {
        for (Object el: iterable) {
            if (el.equals(element))
                return true;
        }
        return false;
    }

    public static  Iterable transform(Iterable fromIterable,
                                              Function function) {
        final ArrayList res = new ArrayList<>();
        if (fromIterable != null) {
            for (F el : fromIterable) {
                res.add(function.apply(el));
            }
        }
        return res;
    }

    public static  Set transformToSet(Iterable fromIterable,
                                              Function function) {
        Set res = new HashSet<>();
        if (fromIterable != null) {
            for (F el : fromIterable) {
                res.add(function.apply(el));
            }
        }
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy