/*
 * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * SVN: $Id$
 */

package com.dmdirc.parser;

import java.util.ArrayList;
import java.util.List;

/**
 * IRC Parser Ignore list.
 *
 * @author Shane Mc Cormack
 * @version $Id$
 */
public class RegexStringList {
	
	/** Arraylist storing ignore patterns. */
	protected final List<String> ignoreInfo = new ArrayList<String>();

	/**
	 * Creates a new instance of RegexStringList.
	 */
	public RegexStringList() {
		// Do nothing
	}

	/**
	 * Creates a new instance of RegexStringList, with the specified items.
	 *
	 * @param items Items to add to this RegexStringList
	 */ 
	public RegexStringList(final List<String> items) {
		ignoreInfo.addAll(items);
	}
	
	/**
	 * Add a new ignore pattern to the ignore list.
	 *
	 * @param pattern Regex syntax for the ignore (Pattern is matched case-insensitively as ^pattern$)
	 */
	public void add(final String pattern) {
		for (String target : ignoreInfo) {
			if (pattern.equalsIgnoreCase(target)) {
				return;
			}
		}
		
		ignoreInfo.add(pattern);
	}
        
	/**
	 * Adds a set of patterns to the list.
	 *
	 * @param patterns A list of patterns to be added
	 */
	public void addAll(final List<String> patterns) {
		for (String pattern : patterns) {
			add(pattern);
		}
	}
	
	/**
	 * Delete an ignore from the list.
	 *
	 * @param position Position in the list to remove
	 */
	public void remove(final int position) {
		if (position < this.count()) {
			ignoreInfo.remove(position);
		}
	}
	
	/**
	 * Clear the ignore list.
	 */
	public void clear() {
		ignoreInfo.clear();
	}	
	
	/**
	 * Check if a string matches any of the ignores in the list.
	 *
	 * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
	 * @return integer showing the position of the first match in the ignore list (-1 if none)
	 */
	public int matches(final String check) {
		for (int i = 0; i < this.count(); ++i) {
			if (check.matches("(?i)"+this.get(i))) {
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * Check if a string matches a specific ignore in the list.
	 *
	 * @param position Position to check
	 * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
	 * @return boolean true/false
	 */
	public boolean matches(final int position, final String check) {
		if (position < this.count()) {
			return check.matches("(?i)"+this.get(position));
		} else {
			return false;
		}
	}
	
	/**
	 * Get the ignore pattern in a given position in the list.
	 *
	 * @param position Position to check
	 * @return String showing the pattern. ("" if position isn't valid)
	 */
	public String get(final int position) {
		if (position < this.count()) {
			return ignoreInfo.get(position);
		} else {
			return "";
		}
	}
	
	/**
	 * Change the ignore pattern in a given position in the list.
	 *
	 * @param position Position to change
	 * @param pattern New pattern
	 */
	public void set(final int position, final String pattern) {
		if (position < this.count()) {
			ignoreInfo.set(position, pattern);
		}
	}
	
	/**
	 * Get the amount of ignores in the list.
	 *
	 * @return int showing the number of ignores
	 */
	public int count() {
		return ignoreInfo.size();
	}

}

@@ -1359,18 +1359,17 @@
 gList %7B%0A
-%09
 %0A
+
 %09/** Arr
@@ -1717,17 +1717,16 @@
 ist%0A%09 */
- 
 %0A%09public
@@ -1776,39 +1776,58 @@
 %7B%0A%09%09
-ignoreInfo.addAll
+for (String item : items) %7B%0A%09%09%09add
 (item
-s
 );%0A
+%09%09%7D%0A
 %09%7D%0A
-%09
 %0A
+
 %09/**
@@ -2131,11 +2131,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%09ig
@@ -2162,17 +2162,9 @@
 %0A%09%7D%0A
-        
 %0A
+
 %09/**
@@ -2367,34 +2367,33 @@
 attern);%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Delete 
@@ -2583,34 +2583,33 @@
 sition);%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Clear t
@@ -2676,19 +2676,17 @@
 ar();%0A%09%7D
-%09%0A%09
+%0A
 %0A%09/**%0A%09 
@@ -3098,26 +3098,25 @@
 turn -1;%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Che
@@ -3513,34 +3513,33 @@
 n false;%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Get the
@@ -3837,34 +3837,33 @@
 turn %22%22;%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 * Change 
@@ -4132,17 +4132,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09/**%0A%09 

Fix addAll()