예제 #1
0
  private FileNaming lookupChildInCache(
      final FileNaming folder, final String childName, boolean lookupExisting) {
    final File f = new File(folder.getFile(), childName);
    final Integer id = NamingFactory.createID(f);

    class FakeNaming implements FileNaming {
      public FileNaming lastEqual;

      public String getName() {
        return childName;
      }

      public FileNaming getParent() {
        return folder;
      }

      public boolean isRoot() {
        return false;
      }

      public File getFile() {
        return f;
      }

      public Integer getId() {
        return id;
      }

      public FileNaming rename(String name, ProvidedExtensions.IOHandler h) {
        // not implemented, as it will not be called
        throw new IllegalStateException();
      }

      @Override
      public boolean equals(Object obj) {
        if (hashCode() == obj.hashCode() && getName().equals(((FileNaming) obj).getName())) {
          assert lastEqual == null : "Just one can be there"; // NOI18N
          if (obj instanceof FileNaming) {
            lastEqual = (FileNaming) obj;
          }
          return true;
        }
        return false;
      }

      @Override
      public int hashCode() {
        return id.intValue();
      }

      public boolean isFile() {
        return this.getFile().isFile();
      }

      public boolean isDirectory() {
        return !isFile();
      }
    }
    FakeNaming fake = new FakeNaming();

    final Set<FileNaming> cache = (lookupExisting) ? getExisting(false) : getNotExisting(false);
    if (cache.contains(fake)) {
      assert fake.lastEqual != null : "If cache contains the object, we set lastEqual"; // NOI18N
      assert fake.lastEqual.getName().equals(childName)
          : "childName: " + childName + " equals: " + fake.lastEqual;
      return fake.lastEqual;
    } else {
      return null;
    }
  }