1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
10 /* PJIRC is free software; you can redistribute */
11 /* it and/or modify it under the terms of the GNU */
12 /* General Public License as published by the */
13 /* Free Software Foundation; version 2 or later of */
16 /* PJIRC is distributed in the hope that it will */
17 /* be useful, but WITHOUT ANY WARRANTY; without */
18 /* even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
20 /* General Public License for more details. */
22 /* You should have received a copy of the GNU */
23 /* General Public License along with PJIRC; if */
24 /* not, write to the Free Software Foundation, */
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
26 /* MA 02111-1307 USA */
28 /*****************************************************/
32 import java.util.Vector;
41 public ListHandler[] handlers;
49 * Implements a set of rules.
51 public class RuleList {
56 * Create a new, empty, set of rules.
64 * Set the default value. By default, this value is null.
69 public void setDefaultValue(Object v) {
74 * Get the default value.
76 * @return the default value.
78 public Object getDefaultValue() {
83 * Add a new rule. The rule uses the given array of list patterns (see
84 * ListHandler) and has the given value.
91 public void addRule(String[] pattern, Object value) {
92 ListHandler[] handlers = new ListHandler[pattern.length];
93 for (int i = 0; i < pattern.length; i++)
94 handlers[i] = new ListHandler(pattern[i]);
95 RuleItem item = new RuleItem();
96 item.handlers = handlers;
98 _list.insertElementAt(item, _list.size());
101 private boolean match(RuleItem item, String[] pattern) {
102 ListHandler[] handlers = item.handlers;
103 if (pattern.length != handlers.length)
105 for (int i = 0; i < handlers.length; i++)
106 if (!handlers[i].inList(pattern[i]))
112 * Find the first matching rule for the given values.
115 * array of values to be tested againts patterns.
116 * @return value of the first matching rule, or default value if no matcing
119 public Object findValue(String[] values) {
120 int l = _list.size();
121 for (int i = 0; i < l; i++) {
122 RuleItem item = (RuleItem) _list.elementAt(i);
123 if (match(item, values))