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

com.remondis.remap.ClassHierarchyIterator Maven / Gradle / Ivy

There is a newer version: 4.3.7
Show newest version
package com.remondis.remap;

import static java.util.Objects.nonNull;

import java.util.*;

/**
 * Iterator that enables to interate over the complete class hierarchy of a type including superclasses and interfaces.
 */
public class ClassHierarchyIterator implements Iterator> {
  private Queue> remaining = new LinkedList<>();
  private Set> visited = new LinkedHashSet<>();

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

  private void append(Class toAppend) {
    if (nonNull(toAppend) && !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