/*
 * StopList.java
 *
 * Created on June 5, 2004, 9:23 PM
 */

package spamfilter.email;

import java.util.TreeSet;
import java.io.*;

/**
 *
 * @author  AaronStJ
 */
public class StopList {
    
    /** Creates a new instance of StopList */
    private TreeSet words = new TreeSet();
    
    public StopList(String fileName) {
        String list = FileToString(fileName);                
        
        String tempWords[] = list.split("[ \n\t\r]+");
        
        for(int i = 0; i < tempWords.length; i++)
            words.add(tempWords[i].toLowerCase());
        
    }
    
    // returns true if word exists in the stoplist
    public boolean constainsWord(String word) {               
        return words.contains(word);
    }      
    
    private static String FileToString(String fileName){
        try{
            FileReader filer = new FileReader(fileName);        
            StringBuffer contents = new StringBuffer();


            char cbuf[] = new char[256];

            int read = filer.read(cbuf);

            while(read !=-1) {
               contents.append(cbuf);
               read = filer.read(cbuf);
            }

            // close the file, otherwise waste memory
            filer.close();
            return new String(contents);   
        }   catch(IOException e){}
        
        return null; //if file dosn't exist null is returned
    }
    
}
