package com.pengyifan.commons.io;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
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.lang3.Validate;

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

  protected final String path;
  protected final FileFilter filter;
  protected boolean debug;

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

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

  public final void process()
      throws Exception {

    preprocess();

    File dir = new File(path);
    List<String> basenames = null;
    if (!dir.exists()) {
      throw new IllegalArgumentException("Cannot find file/dir: " + dir);
    } else if (dir.isFile()) {
      // check if it is a filtered file
      Validate.isTrue(filter.accept(dir), "Not a %s file: %s", filter, dir);
      String basename = FilenameUtils.getBaseName(dir.getAbsolutePath());
      basenames = Collections.singletonList(basename);
      dir = new File(FilenameUtils.getFullPath(dir.getAbsolutePath()));
    } else {
      // filter
      File[] files = dir.listFiles(filter);
      Arrays.sort(files);
      basenames = new ArrayList<String>();
      for (File f : files) {
        basenames.add(FilenameUtils.getBaseName(f.getAbsolutePath()));
      }
    }
    for (String basename : basenames) {
      readResource(basename);
      preprocessFile(basename);
      processFile(basename);
      postprocessFile(basename);
    }
    postprocess();
  }

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

  /**
   * Pre-process and initiates the given directory.
   */
  protected void preprocess()
      throws Exception {

  }

  /**
   * Post-processes and completes the given directory.
   */
  protected void postprocess()
      throws Exception {

  }

  protected void processFile(String basename)
      throws Exception {

  }

  /**
   * read resources according to the basename
   * 
   * @deprecated replaced by {@link #preprocessFile(String)}.
   * @param basename
   * @throws IOException
   */
  @Deprecated
  protected void readResource(String basename)
      throws IOException {
    //
  }

  protected void preprocessFile(String basename)
      throws Exception {

  }

  protected void postprocessFile(String basename)
      throws Exception {

  }
}

@@ -540,22 +540,16 @@
 otected 
-final 
 String p
@@ -553,16 +553,16 @@
 g path;%0A
+
   protec
@@ -904,27 +904,8 @@
  %7B%0A%0A
-    preprocess();%0A%0A
     
@@ -1012,22 +1012,9 @@
 ew I
-llegalArgument
+O
 Exce
@@ -1335,31 +1335,23 @@
 ;%0A      
-dir = new File(
+path = 
 Filename
@@ -1386,25 +1386,24 @@
 olutePath())
-)
 ;%0A    %7D else
@@ -1642,22 +1642,45 @@
       %7D%0A
-
     %7D%0A
+    %0A    preprocess();%0A
     for 

fix the bug in BatchProcessor