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

chaschev.lang.Iterables2 Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
package chaschev.lang;

import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;

import java.util.Collections;
import java.util.List;

/**
 * @author Andrey Chaschev [email protected]
 */
public class Iterables2 {
    public static  Iterable projectField(Iterable fromIterable, Class elClass, Class fieldClass, String name) {
        Function field = (Function) Functions2.field(name);
        return Iterables.transform(fromIterable, field);
    }

    public static  Iterable projectField(Iterable fromIterable, String name) {
        List r = null;

        F nonNull = Iterables.find(fromIterable, Predicates.notNull(), null);

        if(nonNull == null) {
            if (Iterables.isEmpty(fromIterable)) {
                r = Collections.emptyList();
            } else {
                r = Collections.singletonList(null);
            }
        }

        if(r != null) return r;

        Class aClass = nonNull.getClass();

        return projectField(fromIterable, aClass, null, name);
    }

    public static  Iterable projectMethod(Iterable fromIterable, Class elClass, Class methodReturnClass, String name) {
        Function field = (Function) Functions2.method(name);
        return Iterables.transform(fromIterable, field);
    }

    public static  Iterable projectMethod(Iterable fromIterable, String name) {
        List r = null;

        F nonNull = Iterables.find(fromIterable, Predicates.notNull(), null);

        if(nonNull == null) {
            if (Iterables.isEmpty(fromIterable)) {
                r = Collections.emptyList();
            } else {
                r = Collections.nCopies(Iterables.size(fromIterable), null);
            }
        }

        if(r != null) return r;

        Class aClass = nonNull.getClass();

        return projectMethod(fromIterable, aClass, null, name);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy