Simple select method (lambdaj) gets Exception

user1209133 picture user1209133 · Apr 13, 2012 · Viewed 8.9k times · Source

I'm newbie to lambdaj. It seems that a great feature for Java programming.

So I created a very simple program for evaluating.

But I got a Exception for codes below. Could you help me what is wrong?

--EDITED added no argument constructor and public variable encapsulated for class X. Thanks @AVD.

import java.util.Arrays;
import java.util.List;

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

public class Main {

    private static class X {
        private String name;
        public X(){
        }
        public X(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static void main(String[] args) {
        List<X> xs = Arrays.asList(
                new X("aaa"),
                new X("aaa"),
                new X("bbb")
        );

        List<X> s = select(xs, having(on(X.class).getName().equals("aaa")));

    }
}

result is :

Exception in thread "main" ch.lambdaj.function.argument.ArgumentConversionException: Unable to convert the placeholder false in a valid argument
    at ch.lambdaj.function.argument.ArgumentsFactory.actualArgument(ArgumentsFactory.java:92)
    at ch.lambdaj.function.matcher.HasArgumentWithValue.havingValue(HasArgumentWithValue.java:70)
    at ch.lambdaj.function.matcher.HasArgumentWithValue.havingValue(HasArgumentWithValue.java:58)
    at ch.lambdaj.Lambda.having(Lambda.java:1193)
    at Main.main(Main.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Answer

dash1e picture dash1e · Apr 13, 2012

Change your expression in this way

List<X> s = select(xs, having(on(X.class).getName(), Matchers.equalTo("aaa")));

and it will work.

You can use your expression only if

on(X.class).getName()

returns directly a Boolean value, for example

List<X> s = select(xs, having(on(X.class).isLowerCaseString());

where isLowerCaseString() is a method of X that returns true if all the name letters are lowecase.