class ActivityFragmentLeakInstanceFilter implements CaptureObjectInstanceFilter {

  @Override
  public Set<InstanceObject> filter(@NotNull Set<InstanceObject> instances, @NotNull ClassDb classDatabase) {
    Set<ClassDb.ClassEntry> allActivitySubclasses = classDatabase.getEntriesByName(ACTIVTY_CLASS_NAME).stream()
      .flatMap(classEntry -> classDatabase.getDescendantClasses(classEntry.getClassId()).stream())
      .collect(Collectors.toSet());

    Set<ClassDb.ClassEntry> allFragmentSubclasses = FRAGMENT_CLASS_NAMES.stream()
      .flatMap(className -> classDatabase.getEntriesByName(className).stream())
      .flatMap(classEntry -> classDatabase.getDescendantClasses(classEntry.getClassId()).stream())
      .collect(Collectors.toSet());

    return instances.stream().filter(instance -> {
      if (allActivitySubclasses.contains(instance.getClassEntry()) && isPotentialActivityLeak(instance)) {
        return true;
      }
      else if (allFragmentSubclasses.contains(instance.getClassEntry()) && isPotentialFragmentLeak(instance)) {
        return true;
      }
      return false;
    }).collect(Collectors.toSet());
  }

}