KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > filters > CSVFilter


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle.filters;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * <p>
28  * This filter accepts an integer that matches a CSV value, where
29  * each value is an integer or a range of integers.
30  * </p>
31  * @author Rick Giles
32  * @author o_sukhodolsky
33  */

34 class CSVFilter implements IntFilter
35 {
36     /** filter set */
37     private final Set JavaDoc mFilters = new HashSet JavaDoc();
38
39     /**
40      * Adds a IntFilter to the set.
41      * @param aFilter the IntFilter to add.
42      */

43     public void addFilter(IntFilter aFilter)
44     {
45         mFilters.add(aFilter);
46     }
47
48     /**
49      * Returns the IntFilters of the filter set.
50      * @return the IntFilters of the filter set.
51      */

52     protected Set JavaDoc getFilters()
53     {
54         return mFilters;
55     }
56
57     /**
58      * Constructs a <code>CSVFilter</code> from a CSV, Comma-Separated Values,
59      * string. Each value is an integer, or a range of integers. A range of
60      * integers is of the form integer-integer, such as 1-10.
61      * Note: integers must be non-negative.
62      * @param aPattern the CSV string.
63      * @throws NumberFormatException if a component substring does not
64      * contain a parsable integer.
65      */

66     public CSVFilter(String JavaDoc aPattern)
67         throws NumberFormatException JavaDoc
68     {
69         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(aPattern, ",");
70         while (tokenizer.hasMoreTokens()) {
71             final String JavaDoc token = tokenizer.nextToken().trim();
72             final int index = token.indexOf("-");
73             if (index == -1) {
74                 final int matchValue = Integer.parseInt(token);
75                 addFilter(new IntMatchFilter(matchValue));
76             }
77             else {
78                 final int lowerBound =
79                     Integer.parseInt(token.substring(0, index));
80                 final int upperBound =
81                     Integer.parseInt(token.substring(index + 1));
82                 addFilter(new IntRangeFilter(lowerBound, upperBound));
83             }
84         }
85     }
86
87     /**
88      * Determines whether an Integer matches a CSV integer value.
89      * @param aInt the Integer to check.
90      * @return true if aInt is an Integer that matches a CSV value.
91      */

92     public boolean accept(Integer JavaDoc aInt)
93     {
94         final Iterator JavaDoc it = getFilters().iterator();
95         while (it.hasNext()) {
96             final IntFilter filter = (IntFilter) it.next();
97             if (filter.accept(aInt)) {
98                 return true;
99             }
100         }
101         return false;
102     }
103
104     /** {@inheritDoc} */
105     public String JavaDoc toString()
106     {
107         return mFilters.toString();
108     }
109
110     /** {@inheritDoc} */
111     public int hashCode()
112     {
113         return mFilters.hashCode();
114     }
115
116     /** {@inheritDoc} */
117     public boolean equals(Object JavaDoc aObject)
118     {
119         if (aObject instanceof CSVFilter) {
120             final CSVFilter other = (CSVFilter) aObject;
121             return this.mFilters.equals(other.mFilters);
122         }
123         return false;
124     }
125 }
126
Popular Tags