Val Blant
Val Blant

Reputation: 1762

LambdaJ index() and key type conversion

I just started looking at LambdaJ, and immediately ran into a problem. I don't think I am doing anything weird, yet I can't figure out how to do this.

I have a List of Administrators:

List<Administrator> allAdmins; 

I have a map I want these Administrators to be mapped to:

Map<String, Administrator> adminIdToAdmin = new HashMap<String, Administrator>();

The problem is that IDs in the Administrator class are Longs, not Strings. So, I tried the following:

adminIdToAdmin = index(allAdmins, on(Administrator.class).getAdministratorId().toString());

which doesn't work. It fails with:

ch.lambdaj.function.argument.ArgumentConversionException: Unable to convert the placeholder -2147483647 in a valid argument
    at ch.lambdaj.function.argument.ArgumentsFactory.actualArgument(ArgumentsFactory.java:92)
    at ch.lambdaj.function.convert.ArgumentConverter.<init>(ArgumentConverter.java:29)
    at ch.lambdaj.Lambda.index(Lambda.java:1133)

If I change my map to contain Longs and get rid of toString(), the error goes away.

What's the right way to do this?

Upvotes: 1

Views: 650

Answers (2)

Enrico Giurin
Enrico Giurin

Reputation: 2273

I had exactly the same issue, with the same placeholder value: -2147483647. I replaced that block of code, which was applying the lambdaj extract method to all the elements of a collection, with the classical iteration over a collection and now my application works. I guess this is a bug related to the version 2.3.3 of om.googlecode.lambdaj lambdaj version 2.3.3 .

Before (failing)

 return ch.lambdaj.Lambda.extract(sourceMsg.getAttachments(), on(Attachment.class)
                .getDataHandler().getDataSource())

After (working)

List<DataSource> lds= new ArrayList<DataSource>();
        Collection<Attachment> attachmentList = sourceMsg.getAttachments();
        for(Attachment a:attachmentList)
        {
            result.add(a.getDataHandler().getDataSource());
        }
        return lds;

Upvotes: 0

Val Blant
Val Blant

Reputation: 1762

As per Mario Fusco (LambdaJ creator):

This is a well known and well documented limitation of lambdaj: you cannot further reference a final class like Long because it is not proxable.

The 2 available solutions are:

  1. add a getAdministratorIdAsString() to the Administrator class
  2. write your own Converter and pass it to the index method

Upvotes: 1

Related Questions