1

I am using lamdaj expressions to filter the lists based on certain criteria. Inorder to find matches hamcrest matcher is being used.

My question is, I would like to filter the list based on more than one condition using AND and OR operator and i do not know how to do it.

For example, At present i have the below expression

List<CheckPlanForecastData> filteredSubFleet = select(
                            forecastList,
                            having(on(CheckPlanForecastData.class).getSubFleetCode(),
                            Matchers.equalTo(report.getSubFleet())));

Here i have filtered the list based on getSubFleetCode(). I would like to add another criteria along with getSubFleetCode() which i do not know how construct the expression.

Please help.

prabu
  • 1,247
  • 7
  • 21
  • 33

2 Answers2

4

The return type of having() has two methods to do this sort of chaining: and(Matcher) and or(Matcher). See http://lambdaj.googlecode.com/svn/trunk/html/apidocs/ch/lambdaj/function/matcher/LambdaJMatcher.html.

Best regards, Dietrich

dschulten
  • 2,994
  • 1
  • 27
  • 44
1

Maybe you can use Matchers.allOf, Matchers.anyOf and Matchers.not to express your logic.

Please check The Hamcrest Tutorial.

Logical

allOf - matches if all matchers match, short circuits (like Java &&)

anyOf - matches if any matchers match, short circuits (like Java ||)

not - matches if the wrapped matcher doesn't match and vice versa

A simple sample:

import org.hamcrest.Matchers;

import java.util.ArrayList;
import java.util.List;

import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.select;

public class TestLambdaJ {

    public static void main(String[] args) {
        List<Order> orders = new ArrayList<>();
        Order order1 = new Order();
        order1.addItem("Apple");
        order1.addItem("Banana");
        order1.addItem("Orange");
        orders.add(order1);

        Order order2 = new Order();
        order2.addItem("Apple");
        order2.addItem("Cherry");
        order2.addItem("Strawberry");
        orders.add(order2);

        Order order3 = new Order();
        order3.addItem("Olive");
        order3.addItem("Banana");
        order3.addItem("Strawberry");
        orders.add(order3);

        List<Order> ordersWithApple = select(orders,
                having(on(Order.class).getItems(),
                        Matchers.allOf(
                                Matchers.hasItem("Apple"),
                                Matchers.hasItem("Banana"))));
    }
}

class Order {
    List<String> items = new ArrayList<>();

    public void addItem(String item) {
        items.add(item);
    }

    public List<String> getItems() {
        return items;
    }
}
longhua
  • 4,142
  • 21
  • 28
  • Thanks for the response. But i would like to filter based on more than one criteria in a single statement. For ex, In your sample the class Order has got an instance variable items, consider if you have another instance variable quantity then how will you filter the list with respect to both the variables? – prabu Sep 13 '13 at 11:19
  • @prabu Can you express your logic in this way: for example, let's assume that Order has another variable called price. 1, filter Orders using items variable; 2, filter Orders using price variable; 3, join the results of #1 and #2 together. – longhua Sep 17 '13 at 03:05
  • i did the same.. Thanks for the help – prabu Sep 17 '13 at 03:33