com.myjavatools.lib.foundation
Class Filter<T>

java.lang.Object
  extended by com.myjavatools.lib.foundation.Function<T,java.lang.Boolean>
      extended by com.myjavatools.lib.foundation.Filter<T>

public abstract class Filter<T>
extends Function<T,java.lang.Boolean>

Filter is a Function that returns Boolean values; boolean accept() is an alias for Function.apply(). A good example of Filter is java.io.FileFilter, unfortunately not derived from Filter. To implement this class you should define accept().

Since:
5.0
Version:
6.0 04/28/06
See Also:
Function, Iterator

Constructor Summary
Filter()
           
 
Method Summary
abstract  boolean accept(T x)
          checks whether an object is accepted by the filter
static
<T> Filter<T>
and(Filter<T>... filters)
          Creates conjunction of filters.
 java.lang.Boolean apply(T x)
          Filter.apply() is the same as accept();
 java.lang.Iterable<T> filter(java.lang.Iterable<T> iterable)
          filters an Iterable
 java.util.Iterator<T> filter(java.util.Iterator<T> source)
          Returns filtered iterator
static
<T> Filter<T>
not(Filter<T> filter)
          Creates negation of a filter.
static
<T> Filter<T>
or(Filter<T>... filters)
          Creates disjunction of filters.
static
<T> Filter<T>
toFilter(Function<T,java.lang.Double> f)
          creates a filter that accepts only x such that f(x) > 0
 
Methods inherited from class com.myjavatools.lib.foundation.Function
apply, apply, apply, compose, compose, forMap, forMap, function, function, id, toMap, toMap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Filter

public Filter()
Method Detail

accept

public abstract boolean accept(T x)
checks whether an object is accepted by the filter

Parameters:
x - T an element to check for acceptance
Returns:
boolean true if x is accepted, false otherwise

apply

public java.lang.Boolean apply(T x)
Filter.apply() is the same as accept();

Specified by:
apply in class Function<T,java.lang.Boolean>
Parameters:
x - T an element to which this filter is applied
Returns:
Boolean result of applying this filter to x

toFilter

public static <T> Filter<T> toFilter(Function<T,java.lang.Double> f)
creates a filter that accepts only x such that f(x) > 0

Parameters:
f - Function<T,Double>
Returns:
Filter as defined above

filter

public java.util.Iterator<T> filter(java.util.Iterator<T> source)
Returns filtered iterator

Parameters:
source - Iterator original iterator
Returns:
Iterator filtered iterator the resulting iterator will contain only those elements of source that satisfy this filter

Example:

new Filter<String>() { boolean accept(String s) { return s.startsWith("M"); }}.
 filter(Arrays.asList(new String[] {"New", "Magic", "Logic"}).iterator());
  
returns an iterator returning just "Magic".


filter

public java.lang.Iterable<T> filter(java.lang.Iterable<T> iterable)
filters an Iterable

Parameters:
iterable - Iterable
Returns:
Iterable (view) that contains values that this filter approves

and

public static <T> Filter<T> and(Filter<T>... filters)
Creates conjunction of filters.

Parameters:
filters - (vararg)
Returns:
Filter that returns true only on those x for which all filters return true

or

public static <T> Filter<T> or(Filter<T>... filters)
Creates disjunction of filters.

Returns:
Filter that returns false only on those x for which all filters return false

not

public static <T> Filter<T> not(Filter<T> filter)
Creates negation of a filter.

Parameters:
filter - Filter
Returns:
Filter that returns true on those x for which source filter return false