File Source: RegexStringList.java
/*
P/P * Method: com.dmdirc.parser.irc.RegexStringList__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.parser.irc;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.regex.PatternSyntaxException;
28
29 /**
30 * IRC Parser Ignore list.
31 *
32 * @author Shane Mc Cormack
33 */
34 public class RegexStringList {
35
36 /** Arraylist storing ignore patterns. */
37 protected final List<String> ignoreInfo = new ArrayList<String>();
38
39 /**
40 * Creates a new instance of RegexStringList.
41 */
/*
P/P * Method: void com.dmdirc.parser.irc.RegexStringList()
*
* Postconditions:
* this.ignoreInfo == &new ArrayList(RegexStringList#1)
* new ArrayList(RegexStringList#1) num objects == 1
*/
42 public RegexStringList() {
43 // Do nothing
44 }
45
46 /**
47 * Creates a new instance of RegexStringList, with the specified items.
48 *
49 * @param items Items to add to this RegexStringList
50 */
/*
P/P * Method: void com.dmdirc.parser.irc.RegexStringList(List)
*
* Preconditions:
* items != null
*
* Postconditions:
* this.ignoreInfo == &new ArrayList(RegexStringList#1)
* new ArrayList(RegexStringList#1) num objects == 1
*/
51 public RegexStringList(final List<String> items) {
52 addAll(items);
53 }
54
55 /**
56 * Add a new ignore pattern to the ignore list.
57 *
58 * @param pattern Regex syntax for the ignore (Pattern is matched case-insensitively as ^pattern$)
59 */
60 public void add(final String pattern) {
/*
P/P * Method: void add(String)
*
* Preconditions:
* this.ignoreInfo != null
* (soft) pattern != null
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@62: {0}, {1}
* java.util.Iterator:hasNext(...)@61: {0}, {1}
*/
61 for (String target : ignoreInfo) {
62 if (pattern.equalsIgnoreCase(target)) {
63 return;
64 }
65 }
66
67 ignoreInfo.add(pattern);
68 }
69
70 /**
71 * Adds a set of patterns to the list.
72 *
73 * @param patterns A list of patterns to be added
74 */
75 public void addAll(final List<String> patterns) {
/*
P/P * Method: void addAll(List)
*
* Preconditions:
* patterns != null
* (soft) this.ignoreInfo != null
*
* Presumptions:
* java.util.Iterator:next(...)@76 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@76: {0}, {1}
*/
76 for (String pattern : patterns) {
77 add(pattern);
78 }
79 }
80
81 /**
82 * Delete an ignore from the list.
83 *
84 * @param position Position in the list to remove
85 */
86 public void remove(final int position) {
/*
P/P * Method: void remove(int)
*
* Preconditions:
* this.ignoreInfo != null
*/
87 if (position < this.count()) {
88 ignoreInfo.remove(position);
89 }
90 }
91
92 /**
93 * Clear the ignore list.
94 */
95 public void clear() {
/*
P/P * Method: void clear()
*
* Preconditions:
* this.ignoreInfo != null
*/
96 ignoreInfo.clear();
97 }
98
99 /**
100 * Check if a string matches any of the ignores in the list.
101 *
102 * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
103 * @return integer showing the position of the first match in the ignore list (-1 if none)
104 * @throws PatternSyntaxException if one of the items in the list is an invalid regex
105 */
106 public int matches(final String check) throws PatternSyntaxException {
/*
P/P * Method: int matches(String)
*
* Preconditions:
* this.ignoreInfo != null
* (soft) check != null
*
* Postconditions:
* return_value in {-1..232-2}
*
* Test Vectors:
* java.lang.String:matches(...)@108: {0}, {1}
*/
107 for (int i = 0; i < this.count(); ++i) {
108 if (check.matches("(?i)"+this.get(i))) {
109 return i;
110 }
111 }
112 return -1;
113 }
114
115 /**
116 * Check if a string matches a specific ignore in the list.
117 *
118 * @param position Position to check
119 * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
120 * @return boolean true/false
121 * @throws PatternSyntaxException if the item is an invalid regex
122 */
123 public boolean matches(final int position, final String check) throws PatternSyntaxException {
/*
P/P * Method: bool matches(int, String)
*
* Preconditions:
* this.ignoreInfo != null
* (soft) check != null
*
* Postconditions:
* init'ed(return_value)
*/
124 if (position < this.count()) {
125 return check.matches("(?i)"+this.get(position));
126 } else {
127 return false;
128 }
129 }
130
131 /**
132 * Get the ignore pattern in a given position in the list.
133 *
134 * @param position Position to check
135 * @return String showing the pattern. ("" if position isn't valid)
136 */
137 public String get(final int position) {
/*
P/P * Method: String get(int)
*
* Preconditions:
* this.ignoreInfo != null
*
* Postconditions:
* init'ed(return_value)
*/
138 if (position < this.count()) {
139 return ignoreInfo.get(position);
140 } else {
141 return "";
142 }
143 }
144
145 /**
146 * Change the ignore pattern in a given position in the list.
147 *
148 * @param position Position to change
149 * @param pattern New pattern
150 */
151 public void set(final int position, final String pattern) {
/*
P/P * Method: void set(int, String)
*
* Preconditions:
* this.ignoreInfo != null
*/
152 if (position < this.count()) {
153 ignoreInfo.set(position, pattern);
154 }
155 }
156
157 /**
158 * Get the amount of ignores in the list.
159 *
160 * @return int showing the number of ignores
161 */
162 public int count() {
/*
P/P * Method: int count()
*
* Preconditions:
* this.ignoreInfo != null
*
* Postconditions:
* init'ed(return_value)
*/
163 return ignoreInfo.size();
164 }
165
166 }
SofCheck Inspector Build Version : 2.17854
| RegexStringList.java |
2009-Jun-25 01:54:24 |
| RegexStringList.class |
2009-Sep-02 17:04:12 |