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

br.com.objectos.io.WatchDirectoryJava6 Maven / Gradle / Ivy

/*
 * Copyright (C) 2011-2021 Objectos Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.com.objectos.io;

import static br.com.objectos.collections.Collections.newGrowableMap;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

final class WatchDirectoryJava6 implements DirectoryContentsVisitor {

  private final Checker checker = new Checker();

  private final Map directories = newGrowableMap();

  private final Directory directory;

  private final Set events;

  private final Map files = newGrowableMap();

  private final WatchServiceListener listener;

  public WatchDirectoryJava6(Directory directory,
                             WatchServiceListener listener,
                             Set events) {
    this.directory = directory;

    this.listener = listener;

    this.events = events;
  }

  @Override
  public final void visitDirectory(Directory child) throws IOException {
    String key;
    key = child.getName();

    directories.put(key, child);
  }

  @Override
  public final void visitRegularFile(RegularFile file) throws IOException {
    String key;
    key = file.getName();

    RegularFileWrapper wrapper;
    wrapper = new RegularFileWrapper(file);

    files.put(key, wrapper);
  }

  final void checkForEvents() {
    try {
      checker.clear();

      directory.visitContents(checker);

      checker.doDeleteEvents();
    } catch (IOException e) {
      // this visitor does not throw IOException
    }
  }

  final void register() {
    try {
      directory.visitContents(this);
    } catch (IOException e) {
      // this visitor does not throw IOException
    }
  }

  private class Checker implements DirectoryContentsVisitor {

    @Override
    public final void visitDirectory(Directory child) throws IOException {
      String key;
      key = child.getName();

      if (directories.containsKey(key)) {
        return;
      }

      directories.put(key, child);

      if (isCreatedEnabled()) {
        listener.onDirectoryCreated(child);
      }
    }

    @Override
    public final void visitRegularFile(RegularFile file) throws IOException {
      String key;
      key = file.getName();

      RegularFileWrapper thisFile;
      thisFile = files.get(key);

      if (thisFile == null) {
        RegularFileWrapper wrapper;
        wrapper = new RegularFileWrapper(file);

        files.put(key, wrapper);

        if (isCreatedEnabled()) {
          listener.onRegularFileCreated(file);
        }
      }

      else if (isModifiedEnabled() && thisFile.modified(file)) {
        listener.onRegularFileModified(file);
      }
    }

    private boolean isModifiedEnabled() {
      return events.contains(EventKind.MODIFIED);
    }

    final void clear() {}

    final void doDeleteEvents() {
      if (events.contains(EventKind.DELETED)) {
        throw new UnsupportedOperationException("Implement me");
      }
    }

    private boolean isCreatedEnabled() {
      return events.contains(EventKind.CREATED);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy