]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/RuleList.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / RuleList.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
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  */
14 /*  the License.                                     */
15 /*                                                   */
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.         */
21 /*                                                   */
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                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc;
31
32 import java.util.Vector;
33
34 /**
35  * RuleItem.
36  */
37 class RuleItem {
38         /**
39          * Handlers.
40          */
41         public ListHandler[] handlers;
42         /**
43          * Value.
44          */
45         public Object value;
46 }
47
48 /**
49  * Implements a set of rules.
50  */
51 public class RuleList {
52         private Vector _list;
53         private Object _def;
54
55         /**
56          * Create a new, empty, set of rules.
57          */
58         public RuleList() {
59                 _list = new Vector();
60                 _def = null;
61         }
62
63         /**
64          * Set the default value. By default, this value is null.
65          * 
66          * @param v
67          *          new default value.
68          */
69         public void setDefaultValue(Object v) {
70                 _def = v;
71         }
72
73         /**
74          * Get the default value.
75          * 
76          * @return the default value.
77          */
78         public Object getDefaultValue() {
79                 return _def;
80         }
81
82         /**
83          * Add a new rule. The rule uses the given array of list patterns (see
84          * ListHandler) and has the given value.
85          * 
86          * @param pattern
87          *          array of pattern.
88          * @param value
89          *          rule value.
90          */
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;
97                 item.value = value;
98                 _list.insertElementAt(item, _list.size());
99         }
100
101         private boolean match(RuleItem item, String[] pattern) {
102                 ListHandler[] handlers = item.handlers;
103                 if (pattern.length != handlers.length)
104                         return false;
105                 for (int i = 0; i < handlers.length; i++)
106                         if (!handlers[i].inList(pattern[i]))
107                                 return false;
108                 return true;
109         }
110
111         /**
112          * Find the first matching rule for the given values.
113          * 
114          * @param values
115          *          array of values to be tested againts patterns.
116          * @return value of the first matching rule, or default value if no matcing
117          *         rule is found.
118          */
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))
124                                 return item.value;
125                 }
126                 return _def;
127         }
128 }