public static boolean isEmptyMiddlePackage(@NotNull PsiDirectory dir,
                                             boolean strictlyEmpty,
                                             @Nullable PsiFileSystemItemFilter filter) {
    final VirtualFile[] files = dir.getVirtualFile().getChildren();
    if (files.length == 0) {
      return false;
    }
    PsiManager manager = dir.getManager();
    int subpackagesCount = 0;
    int directoriesCount = 0;
    for (VirtualFile file : files) {
      if (FileTypeManager.getInstance().isFileIgnored(file)) continue;
      if (!file.isDirectory()) {
        if (filter == null) return false;
        PsiFile childFile = manager.findFile(file);
        if (childFile != null && filter.shouldShow(childFile)) return false;
      }
      PsiDirectory childDir = manager.findDirectory(file);
      if (childDir != null && (filter == null || filter.shouldShow(childDir))) {
        directoriesCount++;
        if (strictlyEmpty && directoriesCount > 1) return false;
        if (JavaDirectoryService.getInstance().getPackage(childDir) != null) {
          subpackagesCount++;
        }
      }
    }
    if (strictlyEmpty) {
      return directoriesCount == subpackagesCount && directoriesCount == 1;
    }
    return directoriesCount == subpackagesCount && directoriesCount > 0;
  }
public static boolean isEmptyMiddlePackage(@NotNull PsiDirectory dir,
                                             boolean strictlyEmpty,
                                             @Nullable PsiFileSystemItemFilter filter)