Filter.accept() now takes AuditEvent, not an Object.

This commit is contained in:
Oleg Sukhodolsky 2003-11-07 04:49:54 +00:00
parent 7abbd6af2f
commit 8d242382f8
16 changed files with 154 additions and 118 deletions

View File

@ -20,10 +20,9 @@ public class FilesFilter
setFiles("^$");
}
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
final AuditEvent event = (AuditEvent) aObject;
final String fileName = event.getFileName();
final String fileName = aEvent.getFileName();
return ((fileName == null) || !mFileRegexp.match(fileName));
}

View File

@ -43,19 +43,19 @@
A <span class="code">Checker</span> has a set of <span class="code">Filter</span>s
that decide which audit events the <span class="code">Checker</span> reports through its listeners.
Interface <span class="code">Filter</span> and class <span class="code">FilterSet</span>
are intended to support general <span class="code">Object</span> filtering using a set of <span class="code">Filter</span>s.
are intended to support general <span class="code">AuditEvent</span> filtering using a set of <span class="code">Filter</span>s.
<p>
<p class="body">
A <span class="code">Filter</span> has <span class="code">boolean</span> method <span class="code">accept(Object)</span>
A <span class="code">Filter</span> has <span class="code">boolean</span> method <span class="code">accept(AuditEvent)</span>
that returns <span class="code">true</span> if the <span class="code">Filter</span> accepts the
<span class="code">Object</span> parameter and returns <span class="code">false</span> if the <span class="code">Filter</span>
<span class="code">AuditEvent</span> parameter and returns <span class="code">false</span> if the <span class="code">Filter</span>
rejects it.
</p>
<p class="body">
A <span class="code">FilterSet</span> is a particular <span class="code">Filter</span>
that contains a set of <span class="code">Filter</span>s. A <span class="code">FilterSet</span> accepts an
<span class="code">Object</span> if and only if
all <span class="code">Filter</span>s in the set accept the <span class="code">Object</span>.
<span class="code">AuditEvent</span> if and only if
all <span class="code">Filter</span>s in the set accept the <span class="code">AuditEvent</span>.
</p>
<p class="body">
Here is a UML diagram for interface <span class="code">Filter</span>
@ -73,9 +73,7 @@
<a href="api/com/puppycrawl/tools/checkstyle/api/AutomaticBean.html">AutomaticBean</a>
with mutator method <span class="code">setFiles(String)</span> that receives the file name pattern.
An <span class="code">AutomaticBean</span> uses JavaBean introspection to set JavaBean properties such as
<span class="code">files</span>. Method <span class="code">accept(Object)</span> has the precondition
that the <span class="code">Object</span> parameter is an <span class="code">AuditEvent</span>, which
is the case when the <span class="code">Filter</span> is created for a <span class="code">Checker</span>.
<span class="code">files</span>.
</p>
<pre class="body">
@ -101,10 +99,9 @@ public class FilesFilter
setFiles("^$");
}
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
final AuditEvent event = (AuditEvent) aObject;
final String fileName = event.getFileName();
final String fileName = aEvent.getFileName();
return ((fileName == null) || !mFileRegexp.match(fileName));
}
@ -138,7 +135,7 @@ public class FilesFilter
documentation. Please do not hesitate to ask questions on the user mailing list,
this will help us to improve this document. Please ask your questions as
precisely as possible. We will not be able to answer questions like &quot;I want
to write a listener but I don&#39;t know how, can you help me?&quot;. Tell us
to write a filter but I don&#39;t know how, can you help me?&quot;. Tell us
what you are trying to do (the purpose of the filter), what you have
understood so far, and what exactly you are getting stuck on.
</p>

View File

@ -19,16 +19,16 @@
package com.puppycrawl.tools.checkstyle.api;
/**
* An interface for filtering objects.
* An interface for filtering AuditEvents.
* @author Rick Giles
*/
public interface Filter
{
/**
* Determines whether or not a filtered Object is accepted.
* @param aObject the Object to filter.
* @return true if the aObject is accepted.
* Determines whether or not a filtered AuditEvent is accepted.
* @param aEvent the AudtiEvent to filter.
* @return true if the aEvent is accepted.
*/
boolean accept(Object aObject);
boolean accept(AuditEvent aEvent);
}

View File

@ -23,9 +23,9 @@ import java.util.Iterator;
import java.util.Set;
/**
* A filter set applies filters to Objects.
* If a filter in the set rejects an Object, then the
* Object is rejected. Otherwise, the Object is accepted.
* A filter set applies filters to AuditEvents.
* If a filter in the set rejects an AuditEvent, then the
* AuditEvent is rejected. Otherwise, the AuditEvent is accepted.
* @author Rick Giles
*/
public class FilterSet
@ -86,12 +86,12 @@ public class FilterSet
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
final Iterator it = mFilters.iterator();
while (it.hasNext()) {
final Filter filter = (Filter) it.next();
if (!filter.accept(aObject)) {
if (!filter.accept(aEvent)) {
return false;
}
}

View File

@ -18,26 +18,47 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.FilterSet;
/**
* <p>
* This filter accepts an integer that matches a CSV value, where
* each value is an integer or a range of integers.
* </p>
* @author Rick Giles
* @author o_sukhodolsky
*/
public class CSVFilter
extends FilterSet
class CSVFilter implements IntFilter
{
/** filter set */
private Set mFilters = new HashSet();
/**
* Adds a IntFilter to the set.
* @param aFilter the IntFilter to add.
*/
public void addFilter(IntFilter aFilter)
{
mFilters.add(aFilter);
}
/**
* Returns the IntFilters of the filter set.
* @return the IntFilters of the filter set.
*/
protected Set getFilters()
{
return mFilters;
}
/**
* Constructs a <code>CSVFilter</code> from a CSV, Comma-Separated Values,
* string. Each value is an integer, or a range of integers. A range of
* integers is of the form integer-integer, such as 1-10.
* Note: integers must be non-negative.
* @param aPattern the CSV string.
* @throws NumberFormatException if a component substring does not
* contain a parsable integer.
@ -64,22 +85,43 @@ public class CSVFilter
}
/**
* Determines whether an Object matches a CSV integer value.
* @param aObject the Object to check.
* @return true if aObject is an Integer that matches a CSV value.
* Determines whether an Integer matches a CSV integer value.
* @param aInt the Integer to check.
* @return true if aInt is an Integer that matches a CSV value.
*/
public boolean accept(Object aObject)
public boolean accept(Integer aInt)
{
if (!(aObject instanceof Integer)) {
return false;
}
final Iterator it = getFilters().iterator();
while (it.hasNext()) {
final Filter filter = (Filter) it.next();
if (filter.accept(aObject)) {
final IntFilter filter = (IntFilter) it.next();
if (filter.accept(aInt)) {
return true;
}
}
return false;
}
/** @see java.lang.Object#toString() */
public String toString()
{
return mFilters.toString();
}
/** @see java.lang.Object#hashCode() */
public int hashCode()
{
return mFilters.hashCode();
}
/** @see java.lang.Object#equals(java.lang.Object) */
public boolean equals (Object aObject)
{
if (aObject instanceof CSVFilter) {
final CSVFilter other = (CSVFilter) aObject;
return this.mFilters.equals(other.mFilters);
}
else {
return false;
}
}
}

View File

@ -0,0 +1,34 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2003 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
/**
* An interface for filtering Integer.
* @author o_sukhodolsky
*/
interface IntFilter
{
/**
* Determines whether or not a filtered Integer is accepted.
* @param aInt the Integer to filter.
* @return true if the aInt is accepted.
*/
boolean accept(Integer aInt);
}

View File

@ -18,14 +18,11 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.api.Filter;
/**
* This filter accepts a matching Integer.
* @author Rick Giles
*/
public class IntMatchFilter
implements Filter
class IntMatchFilter implements IntFilter
{
/** the matching Integer */
private Integer mMatchValue;
@ -39,10 +36,10 @@ public class IntMatchFilter
mMatchValue = new Integer(aMatchValue);
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
/** @see com.puppycrawl.tools.checkstyle.filters.IntFilter */
public boolean accept(Integer aInt)
{
return mMatchValue.equals(aObject);
return mMatchValue.equals(aInt);
}
/** @see java.lang.Object#toString() */

View File

@ -18,14 +18,11 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.api.Filter;
/**
* This filter accepts an Integer in a range.
* @author Rick Giles
*/
public class IntRangeFilter
implements Filter
class IntRangeFilter implements IntFilter
{
/** hash function multiplicand */
private static final int HASH_MULT = 29;
@ -48,14 +45,11 @@ public class IntRangeFilter
mUpperBound = new Integer(aUpperBound);
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
/** @see com.puppycrawl.tools.checkstyle.filters.IntFilter */
public boolean accept(Integer aInt)
{
if (!(aObject instanceof Integer)) {
return false;
}
return ((mLowerBound.compareTo(aObject) <= 0)
&& (mUpperBound.compareTo(aObject) >= 0));
return ((mLowerBound.compareTo(aInt) <= 0)
&& (mUpperBound.compareTo(aInt) >= 0));
}
/** @see java.lang.Object#hashCode() */

View File

@ -62,14 +62,9 @@ public class SeverityMatchFilter
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
if (!(aObject instanceof AuditEvent)) {
return false;
}
final AuditEvent event = (AuditEvent) aObject;
boolean result = mSeverityLevel.equals(event.getSeverityLevel());
boolean result = mSeverityLevel.equals(aEvent.getSeverityLevel());
if (mAcceptOnMatch) {
return result;
}

View File

@ -32,7 +32,6 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
* name and check name match the filter's file name and check name
* patterns, and the event's line is in the filter's line CSV or the
* check's columns is in the filter's column CSV.
* It rejects Objects that are not AuditEvents.
* </p>
* @author Rick Giles
*/
@ -115,19 +114,13 @@ public class SuppressElement
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
if (!(aObject instanceof AuditEvent)) {
return false;
}
final AuditEvent event = (AuditEvent) aObject;
// file and check match?
if ((event.getFileName() == null)
|| !mFileRegexp.match(event.getFileName())
|| (event.getLocalizedMessage() == null)
|| !mCheckRegexp.match(event.getSourceName()))
if ((aEvent.getFileName() == null)
|| !mFileRegexp.match(aEvent.getFileName())
|| (aEvent.getLocalizedMessage() == null)
|| !mCheckRegexp.match(aEvent.getSourceName()))
{
return true;
}
@ -139,7 +132,7 @@ public class SuppressElement
// reject if line matches a line CSV value.
if (mLineFilter != null) {
final Integer line = new Integer(event.getLine());
final Integer line = new Integer(aEvent.getLine());
if (mLineFilter.accept(line)) {
return false;
}
@ -147,7 +140,7 @@ public class SuppressElement
// reject if column matches a column CSV value.
if (mColumnFilter != null) {
final Integer column = new Integer(event.getColumn());
final Integer column = new Integer(aEvent.getColumn());
if (mColumnFilter.accept(column)) {
return false;
}

View File

@ -29,7 +29,6 @@ import com.puppycrawl.tools.checkstyle.api.FilterSet;
* <p>
* This filter accepts AuditEvents according to file, check, line, and
* column, as specified in a suppression file.
* It rejects Objects that are not AuditEvents.
* </p>
* @author Rick Giles
*/
@ -52,14 +51,9 @@ public class SuppressionFilter
}
/** @see com.puppycrawl.tools.checkstyle.api.Filter */
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
if (!(aObject instanceof AuditEvent)) {
return false;
}
else {
return mFilters.accept(aObject);
}
return mFilters.accept(aEvent);
}
/** @see java.lang.Object#toString() */

View File

@ -283,7 +283,7 @@ class DebugFilter implements Filter
{
private boolean mCalled;
public boolean accept(Object aObject)
public boolean accept(AuditEvent aEvent)
{
mCalled = true;
return true;

View File

@ -1,7 +1,5 @@
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.api.Filter;
import junit.framework.TestCase;
/** Tests CSVFilter */
@ -9,7 +7,7 @@ public class CSVFilterTest extends TestCase
{
public void testDecideSingle()
{
final Filter filter = new CSVFilter("0");
final IntFilter filter = new CSVFilter("0");
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("equal", filter.accept(new Integer(0)));
assertFalse("greater than", filter.accept(new Integer(1)));
@ -17,7 +15,7 @@ public class CSVFilterTest extends TestCase
public void testDecidePair()
{
final Filter filter = new CSVFilter("0, 2");
final IntFilter filter = new CSVFilter("0, 2");
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("equal 0", filter.accept(new Integer(0)));
assertFalse("greater than", filter.accept(new Integer(1)));
@ -26,7 +24,7 @@ public class CSVFilterTest extends TestCase
public void testDecideRange()
{
final Filter filter = new CSVFilter("0-2");
final IntFilter filter = new CSVFilter("0-2");
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("equal 0", filter.accept(new Integer(0)));
assertTrue("equal 1", filter.accept(new Integer(1)));
@ -36,7 +34,7 @@ public class CSVFilterTest extends TestCase
public void testDecideEmptyRange()
{
final Filter filter = new CSVFilter("2-0");
final IntFilter filter = new CSVFilter("2-0");
assertFalse("less than", filter.accept(new Integer(-1)));
assertFalse("equal 0", filter.accept(new Integer(0)));
assertFalse("equal 1", filter.accept(new Integer(1)));
@ -46,7 +44,7 @@ public class CSVFilterTest extends TestCase
public void testDecideRangePlusValue()
{
final Filter filter = new CSVFilter("0-2, 10");
final IntFilter filter = new CSVFilter("0-2, 10");
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("equal 0", filter.accept(new Integer(0)));
assertTrue("equal 1", filter.accept(new Integer(1)));

View File

@ -2,21 +2,20 @@ package com.puppycrawl.tools.checkstyle.filters;
import junit.framework.TestCase;
import com.puppycrawl.tools.checkstyle.api.FilterSet;
/** Tests SuppressElementFilter */
// TODO: this test should be removed/rewritten
public class FilterSetTest extends TestCase
{
private FilterSet filter;
private CSVFilter filter;
public void setUp()
{
filter = new FilterSet();
filter = new CSVFilter("");
}
public void testEmptyChain()
{
assertTrue("0", filter.accept(new Integer(0)));
assertFalse("0", filter.accept(new Integer(0)));
}
public void testOneFilter()
@ -24,7 +23,6 @@ public class FilterSetTest extends TestCase
filter.addFilter(new IntMatchFilter(0));
assertTrue("0", filter.accept(new Integer(0)));
assertFalse("1", filter.accept(new Integer(1)));
assertFalse("\"0\"", filter.accept("0"));
}
public void testMultipleFilter()
@ -32,9 +30,8 @@ public class FilterSetTest extends TestCase
filter.addFilter(new IntMatchFilter(0));
filter.addFilter(new IntRangeFilter(0, 2));
assertTrue("0", filter.accept(new Integer(0)));
assertFalse("1", filter.accept(new Integer(1)));
assertFalse("\"0\"", filter.accept("0"));
assertTrue("1", filter.accept(new Integer(1)));
filter.addFilter(new IntRangeFilter(3, 4));
assertFalse("0 not in [3,4]", filter.accept(new Integer(0)));
assertTrue("0 is in [3,4]", filter.accept(new Integer(0)));
}
}

View File

@ -1,7 +1,5 @@
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.api.Filter;
import junit.framework.TestCase;
/** Tests IntMatchFilter */
@ -9,7 +7,7 @@ public class IntMatchFilterTest extends TestCase
{
public void testDecide()
{
final Filter filter = new IntMatchFilter(0);
final IntFilter filter = new IntMatchFilter(0);
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("equal", filter.accept(new Integer(0)));
assertFalse("greater than", filter.accept(new Integer(1)));
@ -17,9 +15,9 @@ public class IntMatchFilterTest extends TestCase
public void testEquals()
{
final Filter filter = new IntMatchFilter(0);
final Filter filter2 = new IntMatchFilter(0);
final Filter filter3 = new IntMatchFilter(1);
final IntFilter filter = new IntMatchFilter(0);
final IntFilter filter2 = new IntMatchFilter(0);
final IntFilter filter3 = new IntMatchFilter(1);
assertEquals("0", filter, filter2);
assertFalse("0 != 1", filter.equals(filter3));
assertFalse("0 != this", filter.equals(this));

View File

@ -1,7 +1,5 @@
package com.puppycrawl.tools.checkstyle.filters;
import com.puppycrawl.tools.checkstyle.api.Filter;
import junit.framework.TestCase;
/** Tests IntRangeFilter */
@ -9,7 +7,7 @@ public class IntRangeFilterTest extends TestCase
{
public void testDecide()
{
final Filter filter = new IntRangeFilter(0, 10);
final IntFilter filter = new IntRangeFilter(0, 10);
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("in range", filter.accept(new Integer(0)));
assertTrue("in range", filter.accept(new Integer(5)));
@ -19,7 +17,7 @@ public class IntRangeFilterTest extends TestCase
public void testDecideSingle()
{
final Filter filter = new IntRangeFilter(0, 0);
final IntFilter filter = new IntRangeFilter(0, 0);
assertFalse("less than", filter.accept(new Integer(-1)));
assertTrue("in range", filter.accept(new Integer(0)));
assertFalse("greater than", filter.accept(new Integer(1)));
@ -27,7 +25,7 @@ public class IntRangeFilterTest extends TestCase
public void testDecideEmpty()
{
final Filter filter = new IntRangeFilter(10, 0);
final IntFilter filter = new IntRangeFilter(10, 0);
assertFalse("out", filter.accept(new Integer(-1)));
assertFalse("out", filter.accept(new Integer(0)));
assertFalse("out", filter.accept(new Integer(5)));
@ -37,10 +35,10 @@ public class IntRangeFilterTest extends TestCase
public void testEquals()
{
final Filter filter = new IntRangeFilter(0, 2);
final Filter filter2 = new IntRangeFilter(0, 2);
final Filter filter3 = new IntRangeFilter(0, 1);
final Filter filter4 = new IntRangeFilter(1, 2);
final IntFilter filter = new IntRangeFilter(0, 2);
final IntFilter filter2 = new IntRangeFilter(0, 2);
final IntFilter filter3 = new IntRangeFilter(0, 1);
final IntFilter filter4 = new IntRangeFilter(1, 2);
assertEquals("[0,2] == [0,2]", filter, filter2);
assertFalse("[0,2] != [0,1]", filter.equals(filter3));
assertFalse("[0,2] != [1,2]", filter.equals(filter4));