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.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()
      throws IOException {
    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()
      throws IOException;

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

  protected abstract void processFile(String basename)
      throws IOException;

  protected abstract void readResource(String basename)
      throws IOException;
}

@@ -2131,16 +2131,81 @@
 ;%0A      
+preprocessFile(basename);%0A      processFile(basename);%0A      post
 processF
@@ -2390,33 +2390,24 @@
   protected 
-abstract 
 void preproc
@@ -2432,25 +2432,31 @@
  IOException
-;
+ %7B%0A%0A  %7D
 %0A%0A  /**%0A   *
@@ -2520,33 +2520,24 @@
   protected 
-abstract 
 void postpro
@@ -2559,33 +2559,39 @@
 rows IOException
-;
+ %7B%0A%0A  %7D
 %0A%0A  protected ab
@@ -2584,33 +2584,24 @@
   protected 
-abstract 
 void process
@@ -2650,91 +2650,451 @@
 tion
-;
+ %7B
 %0A%0A  
+%7D%0A%0A  /**%0A   * read resources according to the basename%0A   * %0A   * @de
 pr
-ot
 ec
+a
 ted 
-abstract void readResource(String basename)%0A      throws IOException;
+replaced by %7B@link #preprocessFile(String)%7D.%0A   * @param basename%0A   * @throws IOException%0A   */%0A  @Deprecated%0A  protected void readResource(String basename)%0A      throws IOException %7B%0A    //%0A  %7D%0A%0A  protected void preprocessFile(String basename)%0A      throws IOException %7B%0A%0A  %7D%0A%0A  protected void postprocessFile(String basename)%0A      throws IOException %7B%0A%0A  %7D
 %0A%7D%0A

change methods in BatchProcessor