package com.pengyifan.commons.io;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;

/**
 * 
 * If path is a filtered file, then process the file. If path is a directory,
 * then process all filtered files. Otherwise throw Exception
 * 
 * @author Yifan Peng
 * 
 */
public abstract class BatchProcessor {

  protected String               path;
  protected File                 dir;
  protected List<String>         basenames;
  protected boolean              debug;

  public final static FileFilter TEXT_FILTER = FileFilterUtils
                                                 .suffixFileFilter(".txt");

  public BatchProcessor(String path) {
    this(path, TEXT_FILTER);
  }

  public BatchProcessor(File path) {
    this(path.toString());
  }

  public BatchProcessor(File path, FileFilter filter) {
    this(path.toString(), filter);
  }

  public BatchProcessor(String path, FileFilter filter) {
    this.path = path;
    debug = false;

    dir = new File(path);
    if (!dir.exists()) {
      throw new IllegalArgumentException("Cannot find file/dir: " + dir);
    } else if (dir.isFile()) {
      // check if it is a [.txt] file
      if (filter.accept(dir)) {
        String basename = FilenameUtils.getBaseName(dir.getAbsolutePath());
        basenames = Collections.singletonList(basename);
        dir = new File(FilenameUtils.getFullPath(dir.getAbsolutePath()));
      } else {
        throw new IllegalArgumentException("Not a " + filter + " file: " + dir);
      }
    } else {
      // filter [.txt]
      File[] files = dir.listFiles(filter);
      Arrays.sort(files);
      basenames = new ArrayList<String>();
      for (File f : files) {
        basenames.add(FilenameUtils.getBaseName(f.getAbsolutePath()));
      }
    }
  }

  public final void process() {
    preprocess();
    for (String basename : basenames) {
      readResource(basename);
      processFile(basename);
    }
    postprocess();
  }

  public void setDebug(boolean debug) {
    this.debug = debug;
  }

  /**
   * Pre-process and initiates the given directory.
   */
  protected abstract void preprocess();

  /**
   * Post-processes and completes the given directory.
   */
  protected abstract void postprocess();

  protected abstract void processFile(String basename);

  protected abstract void readResource(String basename);
}

@@ -76,16 +76,44 @@
 Filter;%0A
+import java.io.IOException;%0A
 import j
@@ -2009,16 +2009,41 @@
 rocess()
+%0A      throws IOException
  %7B%0A    p
@@ -2351,24 +2351,49 @@
 preprocess()
+%0A      throws IOException
 ;%0A%0A  /**%0A   
@@ -2489,16 +2489,41 @@
 rocess()
+%0A      throws IOException
 ;%0A%0A  pro
@@ -2571,16 +2571,41 @@
 asename)
+%0A      throws IOException
 ;%0A%0A  pro
@@ -2654,12 +2654,37 @@
 asename)
+%0A      throws IOException
 ;%0A%7D%0A

throw IOException in BatchProcessor