FileDocCategorySizeDatePackage
DeclarationFilter.javaAPI DocJava SE 5 API8955Fri Aug 26 14:55:14 BST 2005com.sun.mirror.util

DeclarationFilter

public class DeclarationFilter extends Object
A filter for selecting just the items of interest from a collection of declarations. The filter is said to select or to match those declarations. Filters can be created in several ways: by the static methods described below, by negating or composing existing filters, or by subclasses that implement arbitrary matching rules.

A subclass can create an arbitrary filter simply by implementing the {@link #matches(Declaration)} method.

Examples.

Selecting the public declarations from a collection:

result = FILTER_PUBLIC.filter(decls); 
Selecting class declarations (including enums):
classFilter = DeclarationFilter.getFilter(ClassDeclaration.class);
result = classFilter.filter(decls); 
Selecting class declarations but excluding enums:
enumFilter = DeclarationFilter.getFilter(EnumDeclaration.class);
compoundFilter = classFilter.and(enumFilter.not());
result = compoundFilter.filter(decls); 
Selecting declarations named "Bob":
nameFilter = new DeclarationFilter() {
public boolean matches(Declaration d) {
return d.getSimpleName().equals("Bob");
}
};
result = nameFilter.filter(decls); 
author
Joseph D. Darcy
author
Scott Seligman
version
1.2 04/07/19
since
1.5

Fields Summary
public static final DeclarationFilter
FILTER_PUBLIC
A filter that selects only public declarations.
public static final DeclarationFilter
FILTER_PROTECTED
A filter that selects only protected declarations.
public static final DeclarationFilter
FILTER_PUBLIC_OR_PROTECTED
A filter that selects only public or protected declarations.
public static final DeclarationFilter
FILTER_PACKAGE
A filter that selects only package-private (default) declarations.
public static final DeclarationFilter
FILTER_PRIVATE
A filter that selects only private declarations.
Constructors Summary
public DeclarationFilter()
Constructs an identity filter: one that selects all declarations.



                   
      
    
Methods Summary
public com.sun.mirror.util.DeclarationFilterand(com.sun.mirror.util.DeclarationFilter f)
Returns a filter that selects those declarations selected by both this filter and another.

param
f filter to be composed with this one
return
a filter that selects those declarations selected by both this filter and another

	final DeclarationFilter f1 = this;
	final DeclarationFilter f2 = f;
	return new DeclarationFilter() {
	    public boolean matches(Declaration d) {
		return f1.matches(d) && f2.matches(d);
	    }
	};
    
public java.util.Collectionfilter(java.util.Collection decls)
Returns the declarations matched by this filter. The result is a collection of the same type as the argument; the {@linkplain #filter(Collection, Class) two-parameter version} of filter offers control over the result type.

param
type of the declarations being filtered
param
decls declarations being filtered
return
the declarations matched by this filter

	ArrayList<D> res = new ArrayList<D>(decls.size());
	for (D d : decls) {
	    if (matches(d)) {
		res.add(d);
	    }
	}
	return res;
    
public java.util.Collectionfilter(java.util.Collection decls, java.lang.Class resType)
Returns the declarations matched by this filter, with the result being restricted to declarations of a given kind. Similar to the simpler {@linkplain #filter(Collection) single-parameter version} of filter, but the result type is specified explicitly.

param
type of the declarations being returned
param
decls declarations being filtered
param
resType type of the declarations being returned -- the reflective view of D
return
the declarations matched by this filter, restricted to those of the specified type

	ArrayList<D> res = new ArrayList<D>(decls.size());
	for (Declaration d : decls) {
	    if (resType.isInstance(d) && matches(d)) {
		res.add(resType.cast(d));
	    }
	}
	return res;
    
public static com.sun.mirror.util.DeclarationFiltergetFilter(java.util.Collection mods)
Returns a filter that selects declarations containing all of a collection of modifiers.

param
mods the modifiers to match (non-null)
return
a filter that matches declarations containing mods

	return new DeclarationFilter() {
	    public boolean matches(Declaration d) {
		return d.getModifiers().containsAll(mods);
	    }
	};
    
public static com.sun.mirror.util.DeclarationFiltergetFilter(java.lang.Class kind)
Returns a filter that selects declarations of a particular kind. For example, there may be a filter that selects only class declarations, or only fields. The filter will select declarations of the specified kind, and also any subtypes of that kind; for example, a field filter will also select enum constants.

param
kind the kind of declarations to select
return
a filter that selects declarations of a particular kind

	return new DeclarationFilter() {
	    public boolean matches(Declaration d) {
		return kind.isInstance(d);
	    }
	};
    
public booleanmatches(com.sun.mirror.declaration.Declaration decl)
Tests whether this filter matches a given declaration. The default implementation always returns true; subclasses should override this.

param
decl the declaration to match
return
true if this filter matches the given declaration

	return true;
    
public com.sun.mirror.util.DeclarationFilternot()
Returns a filter that selects those declarations not selected by this filter.

return
a filter that selects those declarations not selected by this filter

	return new DeclarationFilter() {
	    public boolean matches(Declaration d) {
		return !DeclarationFilter.this.matches(d);
	    }
	};
    
public com.sun.mirror.util.DeclarationFilteror(com.sun.mirror.util.DeclarationFilter f)
Returns a filter that selects those declarations selected by either this filter or another.

param
f filter to be composed with this one
return
a filter that selects those declarations selected by either this filter or another

	final DeclarationFilter f1 = this;
	final DeclarationFilter f2 = f;
	return new DeclarationFilter() {
	    public boolean matches(Declaration d) {
		return f1.matches(d) || f2.matches(d);
	    }
	};