public class GeneralFilter
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
CONTAINS |
static int |
ENDS_WITH |
static int |
EQUALS |
static int |
GREATER_THAN |
static int |
KEYS |
static int |
LESS_THAN |
static int |
NOT_CONTAINS |
static int |
NOT_EQUALS |
static int |
STARTS_WITH |
static int |
VALUES |
Constructor and Description |
---|
GeneralFilter(java.lang.Class c) |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(java.lang.Object o)
Cycle over all our fields and check to see if this object
matches.
|
void |
addField(java.lang.String field,
boolean value,
int type)
Add a new field for a boolean comparison.
|
void |
addField(java.lang.String field,
java.util.Date max,
java.util.Date min)
Add a new field to check for the date to be in the specified range.
|
void |
addField(java.lang.String field,
java.util.Date value,
int type)
Add a new field for a Date comparison.
|
void |
addField(java.lang.String field,
double max,
double min)
Add a new field for a number range comparison.
|
void |
addField(java.lang.String field,
double value,
int type)
Add a new field for a number comparison.
|
void |
addField(java.lang.String field,
java.lang.Object object,
java.util.Comparator comp,
int type)
Add a new field for an Object comparison.
|
void |
addField(java.lang.String field,
java.lang.Object object,
int type)
Add a new field for an Object comparison.
|
void |
addField(java.lang.String field,
java.lang.String value,
int type)
Add a new field for a String comparison.
|
java.util.Collection |
filter(java.util.Collection collection)
Iterate over the Collection and filter given our fields, return the filtered
objects in a new Collection.
|
void |
filter(java.util.Collection collection,
java.util.Collection newCollection)
Iterate over the Collection and filter given our fields.
|
java.util.List |
filter(java.util.List list)
Cycle over the List and filter given our fields.
|
void |
filter(java.util.List list,
java.util.List newList)
Cycle over the List and filter given our fields.
|
void |
filter(java.util.List list,
java.util.Set newSet)
Iterate over the List and filter the objects it contains.
|
java.util.Map |
filter(java.util.Map map,
int type)
Iterate over the Map and filter either the Keys or Values in the Map
given our fields.
|
void |
filter(java.util.Map map,
int type,
java.util.Map newMap)
Iterate over the Map and filter either the Keys or Values in the Map
given our fields.
|
java.lang.Object[] |
filter(java.lang.Object[] objects)
Filter an array of objects and return a new array of the filtered
objects.
|
java.util.Set |
filter(java.util.Set set)
Iterate over the Set and filter given our fields, return the filtered
objects in a new Set.
|
void |
filter(java.util.Set set,
java.util.List newList)
Iterate over the Set and filter the objects it contains.
|
void |
filter(java.util.Set set,
java.util.Set newSet)
Iterate over the Set and filter the objects it contains.
|
java.util.SortedMap |
filter(java.util.SortedMap map,
int type)
Iterate over the SortedMap and filter either the Keys or Values in the SortedMap
given our fields.
|
java.util.SortedSet |
filter(java.util.SortedSet set)
Iterate over the SortedSet and filter the objects it contains.
|
void |
filterAndRemove(java.util.Collection col)
Cycle over the Collection and filter given our fields directly from the passed in
Set.
|
void |
filterAndRemove(java.util.List list)
Cycle over the List and filter given our fields directly from the passed in
List.
|
void |
filterAndRemove(java.util.Map map,
int type)
Iterate over the Map and filter given our fields directly from the passed in
Map.
|
void |
filterAndRemove(java.util.Set set)
Cycle over the Set and filter given our fields directly from the passed in
Set.
|
java.lang.Class |
getFilterClass()
Get the class that we are filtering.
|
boolean |
getNullAcceptPolicy()
Get the null accept policy, this governs what happens when a null is
observed from the result of an accessor chain call,
true
indicates that the value is accepted, false indicates
that it is rejected. |
void |
setFieldValue(java.lang.String field,
boolean value)
Set a new value for the specified boolean filter field.
|
void |
setFieldValue(java.lang.String field,
java.util.Date value)
Set a new date value for the specified date filter field.
|
void |
setFieldValue(java.lang.String field,
java.util.Date max,
java.util.Date min)
Set new date range values for the specified date filter field.
|
void |
setFieldValue(java.lang.String field,
double value)
Set new maximum and minimum values for the specified number filter field.
|
void |
setFieldValue(java.lang.String field,
double max,
double min)
Set new maximum and minimum values for the specified number filter field.
|
void |
setFieldValue(java.lang.String field,
java.lang.Object value)
Set the value for the specified field.
|
void |
setFieldValue(java.lang.String field,
java.lang.String value)
Set the value for the specified String filter field.
|
void |
setNullAcceptPolicy(boolean policy)
Specify what the policy should be when a null value is returned from an
accesor chain, in other words, when the comparison is made should
a
null value return true or false . |
java.lang.String |
toString()
Output the filter fields as a String suitable for debugging.
|
public static final int EQUALS
public static final int NOT_EQUALS
public static final int GREATER_THAN
public static final int LESS_THAN
public static final int CONTAINS
public static final int NOT_CONTAINS
public static final int STARTS_WITH
public static final int ENDS_WITH
public static final int KEYS
public static final int VALUES
public void setNullAcceptPolicy(boolean policy)
null
value return true
or false
.policy
- The policy to use.public boolean getNullAcceptPolicy()
true
indicates that the value is accepted, false
indicates
that it is rejected.public void addField(java.lang.String field, java.util.Date max, java.util.Date min) throws java.lang.IllegalArgumentException
field
- The field spec.max
- The upper date.min
- The lower date.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor.public void setFieldValue(java.lang.String field, java.util.Date max, java.util.Date min) throws java.lang.IllegalArgumentException
field
- The field spec.max
- The upper date.min
- The lower date.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, java.util.Date value, int type) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The Date to check against.type
- The type, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method.public void setFieldValue(java.lang.String field, java.util.Date value) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The new value.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, boolean value, int type) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The Date to check against.type
- The type, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method.public void setFieldValue(java.lang.String field, boolean value) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The new value.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, double value, int type) throws java.lang.IllegalArgumentException
false
returned from the checking of the field in the filter,
probably not what you want... If you want to check a range then
use addField(String,double,double)
.field
- The field spec.value
- The value to check against.type
- The type, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method.public void setFieldValue(java.lang.String field, double value) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The new value.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, double max, double min)
field
- The field spec.max
- The maximum value to check against.min
- The minimum value to check against.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor.public void setFieldValue(java.lang.String field, double max, double min) throws java.lang.IllegalArgumentException
field
- The field spec.max
- The new maximum to check against.min
- The new minimum to check against.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, java.lang.String value, int type) throws java.lang.IllegalArgumentException
addField(String,Object,Comparator,int)
or addField(String,Object,int)
.field
- The field spec.value
- The value to compare against.type
- The type of comparison, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method.public void setFieldValue(java.lang.String field, java.lang.String value) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The value to compare against.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, java.lang.Object object, int type) throws java.lang.IllegalArgumentException
compareTo([Object returned from field spec call])
passing the object
returned to the compareTo method of the object passed in here. It is your responsibility
to ensure that the object returned from the field spec call will NOT cause
a ClassCastException to be thrown in the compareTo method.
field
- The field spec.value
- The value to compare against.type
- The type of comparison, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method, or if the passed in object does not implement
the Comparable interface.public void setFieldValue(java.lang.String field, java.lang.Object value) throws java.lang.IllegalArgumentException
field
- The field spec.value
- The value to compare against.java.lang.IllegalArgumentException
- If we can't find the specified field.public void addField(java.lang.String field, java.lang.Object object, java.util.Comparator comp, int type) throws java.lang.IllegalArgumentException
compare
method we pass the object passed into this method as the first argument
and the object returned from the field spec call as the second argument.
field
- The field spec.value
- The value to compare against.type
- The type of comparison, see above for valid values.java.lang.IllegalArgumentException
- If the field spec is not valid for the
class set in the constructor. Or if you pass in an invalid
type for this method, or if the passed in object does not implement
the Comparable interface.public java.lang.Class getFilterClass()
public boolean accept(java.lang.Object o) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
o
- The object to check the fields against.true
if all our fields match, false
otherwise.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.Set set, java.util.Set newSet) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the newSet parameter. Since we use
an Iterator to cycle over the Set the ordering of the values added
to the newSet is dependent on the ordering of the newSet Set.
set
- The Set to filter.newSet
- The Set to add successfully filtered objects to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.Set set, java.util.List newList) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the newList parameter. Since we use
an Iterator to cycle over the Set the ordering of the values added
to the newList is dependent on the ordering of the set Set.
set
- The Set to filter.newList
- The List to add successfully filtered objects to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.List list, java.util.Set newSet) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the newSet parameter.
list
- The List to filter.newSet
- The Set to add successfully filtered objects to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.Map map, int type, java.util.Map newMap) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the newMap parameter. Since we use
an Iterator to cycle over the Map the ordering of the values added
to the newMap is dependent on the ordering of the newMap Map.
map
- The Map to filter.type
- The type to filter on, either keys or values.newMap
- The map to add successfully filtered keys/values to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.Collection collection, java.util.Collection newCollection) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
filter(List,List)
method since that uses List.size and it has been shown that using the get
method can be an order of magnitude faster than using the Iterator. This
method is really here to allow filtering of things like Sets.
It would be interesting to determine whether performing the following would
be more efficient (i.e. faster...) than using the Iterator:
// Get as an Object array. Object[] objs = collection.toArray (); int size = objs.length; for (int i = 0; i < size; i++) { if (this.accept (objs[i])) { newCollection.add (objs[i]); } }
If you find this to be the case, please contact code-monkey@gentlyweb.com.
The bottom line is, if you have a List to filter then use the filter(List,List)
method rather than this one.
collection
- The Collection to filter.newCollection
- The Collection to add successfully filtered objects to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filter(java.util.List list, java.util.List newList) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
filter(Collection,Collection)
method since it uses the get method of List rather than an Iterator. However if
you need to iterate over the List rather than use direct access then
cast as a Collection and call filter(Collection,Collection)
instead.list
- The List to filter.newList
- The List to add successfully filtered objects to.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public java.lang.Object[] filter(java.lang.Object[] objects) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
objects
- The objects to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public java.util.Map filter(java.util.Map map, int type) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the new Map.
filter(SortedMap,int)
instead. There is
a potential problem here in that we can only call the default no argument
constructor for the new Map, if you are using a HashMap and have tuned
it with a load factor and capacity then this method will ruin that and
we recommend that you use: filter(Map,int,Map)
instead.map
- The Map to filter.type
- The type to filter on, either keys or values.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of Map
it will then contain a nested exception with the "real"
reason for the failure.public java.util.SortedMap filter(java.util.SortedMap map, int type) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the new SortedMap.
map
- The SortedMap to filter.type
- The type to filter on, either keys or values.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of SortedMap
it will then contain a nested exception with the "real"
reason for the failure.public java.util.Collection filter(java.util.Collection collection) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
filter(Collection,Collection)
.
filter(List,List)
method rather than this one.
#filter(SortedSet,int)
instead, this is to
preserve any Comparator that may be used in sorting the Collection.
collection
- The Collection to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of Collection
it will then contain a nested exception with the "real"
reason for the failure.public java.util.Set filter(java.util.Set set) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
filter(Set,Set)
.
#filter(SortedSet,int)
instead, this is to
preserve any Comparator that may be used in sorting the Set.
set
- The Set to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of Set
it will then contain a nested exception with the "real"
reason for the failure.public java.util.SortedSet filter(java.util.SortedSet set) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
accept(Object)
method will then be added to the new SortedMap.
set
- The SortedMet to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of SortedSet
it will then contain a nested exception with the "real"
reason for the failure.public java.util.List filter(java.util.List list) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
filter(Collection,Collection)
method since it uses the get method of List rather than an Iterator. However if
you need to iterate over the List rather than use direct access then
cast as a Collection and call filter(Collection,Collection)
instead and then
cast the return as a List.
list
- The List to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date. It is also thrown
if we cannot create the new instance of List
it will then contain a nested exception with the "real"
reason for the failure.public void filterAndRemove(java.util.List list) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
GeneralFilter gf = new GeneralFilter (MyObjectClass); // ... configure the filter ... List myList = gf.filter (myList);
This is because we have to here use an Iterator to strip out the unwanted
objects rather than using the get method which is what filter(List)
uses.
list
- The List to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filterAndRemove(java.util.Collection col) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
col
- The Collection to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filterAndRemove(java.util.Set set) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
set
- The Set to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public void filterAndRemove(java.util.Map map, int type) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, FilterException
set
- The Set to filter.java.lang.reflect.InvocationTargetException
- If we cannot execute the associated
Accessor
chain and get the
value.java.lang.IllegalAccessException
- If we cannot execute the associated
Accessor
chain because of a
security violation.FilterException
- Thrown if the return type is not what is
expected from the field type, for example if
we are checking a java.utli.Date but the returned type
is NOT of type java.util.Date.public java.lang.String toString()
toString
in class java.lang.Object