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

com.coditory.quark.context.HierarchyIterator Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.coditory.quark.context;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

final class HierarchyIterator implements Iterator> {
    public static Set> getClassHierarchy(Class forClass) {
        Iterable> iterable = () -> new HierarchyIterator(forClass);
        return StreamSupport
                .stream(iterable.spliterator(), false)
                .collect(Collectors.toSet());
    }

    private final Queue> remaining = new LinkedList<>();
    private final Set> visited = new LinkedHashSet<>();

    public HierarchyIterator(Class initial) {
        append(initial);
    }

    private void append(Class toAppend) {
        if (toAppend != null && !visited.contains(toAppend)) {
            remaining.add(toAppend);
            visited.add(toAppend);
        }
    }

    @Override
    public boolean hasNext() {
        return remaining.size() > 0;
    }

    @Override
    public Class next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        Class polled = remaining.poll();
        append(polled.getSuperclass());
        for (Class superInterface : polled.getInterfaces()) {
            append(superInterface);
        }
        return polled;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy