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

io.devcon5.classutils.ClassStreams Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
package io.devcon5.classutils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * Utility class for dealing with classes and streams
 */
public final class ClassStreams {

    private ClassStreams() {
    }

    /**
     * Creates a stream of classes representing the supertypes (excluding Object.class) of the specified type.
     *
     * @param type
     *         the type whose supertypes should be returned as a stream
     *
     * @return stream of the supertypes of the specified class
     */
    public static Stream supertypes(final Class type) {
        return selfAndSupertypes(type.getSuperclass());
    }

    /**
     * Creates a stream of classes containing the class itself and all its the supertypes (excluding Object.class) of
     * the specified type.
     *
     * @param type
     *         the type whose supertypes should be returned as a stream
     *
     * @return stream of the class itself and its supertypes
     */
    public static Stream selfAndSupertypes(final Class type) {
        Class current = type;
        final List result = new ArrayList<>();
        do {
            result.add(current);
            current = current.getSuperclass();
        } while (current != Object.class);
        return result.stream();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy