[The FindAll generic method] retrieves all the elements that match the conditions defined by the specified predicate.
System.Array.FindAll(arrayOfObjects, _
New Predicate(Of String)(AddressOf MatchRange) _
)
'Delegate implementation for Predicate needed in System.Array.FindAll
Function MatchRange(ByVal n As String) As Boolean
Return Ranges.MatchRange(n, RANGE_START, RANGE_END)
End Function
/**
* Predicate interface used to emulate .NET
* <code>System.Predicate(Of T)</code>.
*/
package com.blogspot.crossedlogic.rangesearching;
public interface Predicate<T> {
/**
* @param obj - object to test against predicate criteria.
* @return boolean flag indicating a match (true) or no match (false).
*/
public boolean isMatch(T obj);
}
package com.blogspot.crossedlogic.rangesearching;
import java.util.ArrayList;
import java.util.List;
public class Predicates {
/**
* Replicate .NET findAll and <code>Predicate</code>.
*
* @param <T>
* @param array - an array of objects of type <T>.
* @param match -
* <p>
* instance of an implementation of the Predicate
* interface.
* </p>
* @return List<T> representing all matches found.
* @see com.blogspot.crossedlogic.rangesearching.Predicate
*/
public static <T> List<T> findAll(T[] array,
Predicate<T> match) {
List<T> lst = new ArrayList<T>();
for (T obj : array) {
if (match.isMatch(obj)) {
lst.add(obj);
}
}
return lst;
}
/**
* Replicate .NET findAll and <code>Predicate</code>.
*
* @param <T>
* @param list - collection of objects of type <T>.
* @param match - instance of an implementation of the
* Predicate interface.
* @return List<T> representing all matches found.
* @see com.blogspot.crossedlogic.rangesearching.Predicate
*/
@SuppressWarnings("unchecked")
public static <T> List<T> findAll(List<T> list,
Predicate<T> match) {
return findAll((T[]) list.toArray(), match);
}
}
Originally, I implemented only the findAll with array argument in signature as it allowed for use with both arrays and lists through one function, but given the ability to overload methods I added the second as it makes calls easier when searching lists as you can simply pass the List object without having to convert it to an array first.
package com.expertsexchange.articles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import com.blogspot.crossedlogic.rangesearching.Predicate;
import com.blogspot.crossedlogic.rangesearching.Predicates;
public class PredicateMatchExample {
/**
* @param args
*/
public static void main(String[] args) {
List<Number> numbers = new ArrayList<Number>();
List<Number> matches;
for (int i = new Random(System.currentTimeMillis())
.nextInt(10); i < 100; i += 7) {
numbers.add(i);
}
displayNumbers(numbers, "All Numbers");
// find all the perfect squares.
matches = Predicates.findAll(numbers
.toArray(new Number[] {}),
new Predicate<Number>() {
@Override
public boolean isMatch(Number n) {
double sqrt = Math.sqrt(n.doubleValue());
return Math.ceil(sqrt) == sqrt;
}
});
displayNumbers(matches, "Perfect Squares");
// find all numbers less than 50.
matches = Predicates.findAll(numbers,
new Predicate<Number>() {
@Override
public boolean isMatch(Number n) {
// mimic .NET delegate method;
// utilize existing method for
// implementation.
return isLessThan50(n);
}
});
displayNumbers(matches, "Less Than 50");
}
static boolean isLessThan50(Number n) {
return n.intValue() < 50;
}
static void displayNumbers(List<Number> numbers, String title) {
System.out.printf("%s: %s.", title,
(numbers.size() > 0) ? Arrays.toString(
numbers.toArray()).replaceAll(
"^\\[|\\]$", "") : "none");
System.out.println("");
}
}
As mentioned earlier, the anonymous inner class within Java allows you to utilize methods from your main class similar to the delegate function in .NET. Looks a little more than one line, but hopefully you agree that this is not a lot of Java code for what you are getting.
Predicates.findAll(numbers, new Predicate<Number>() {
@Override
public boolean isMatch(Number n) {
return isLessThan50(n);
}
});
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented:http://java.sun.com/docs/white/sidebar.html
And this follow-up article goes into strongly typed delegates (it references above, so good to read in order linked here) :
http://weblogs.java.net/blog/alexwinston/archive/2005/04/strongly_types_1.html
Regards,
Kevin