I want to filter a java.util.Collection
based on a predicate.
https://code.google.com/p/joquery/
Supports different possibilities,
Given collection,
Collection testList = new ArrayList<>();
of type,
class Dto
{
private int id;
private String text;
public int getId()
{
return id;
}
public int getText()
{
return text;
}
}
Filter
Java 7
Filter query = CQ.filter(testList)
.where()
.property("id").eq().value(1);
Collection filtered = query.list();
Java 8
Filter query = CQ.filter(testList)
.where()
.property(Dto::getId)
.eq().value(1);
Collection filtered = query.list();
Also,
Filter query = CQ.filter()
.from(testList)
.where()
.property(Dto::getId).between().value(1).value(2)
.and()
.property(Dto::grtText).in().value(new string[]{"a","b"});
Sorting (also available for the Java 7)
Filter query = CQ.filter(testList)
.orderBy()
.property(Dto::getId)
.property(Dto::getName)
Collection sorted = query.list();
Grouping (also available for the Java 7)
GroupQuery query = CQ.query(testList)
.group()
.groupBy(Dto::getId)
Collection> grouped = query.list();
Joins (also available for the Java 7)
Given,
class LeftDto
{
private int id;
private String text;
public int getId()
{
return id;
}
public int getText()
{
return text;
}
}
class RightDto
{
private int id;
private int leftId;
private String text;
public int getId()
{
return id;
}
public int getLeftId()
{
return leftId;
}
public int getText()
{
return text;
}
}
class JoinedDto
{
private int leftId;
private int rightId;
private String text;
public JoinedDto(int leftId,int rightId,String text)
{
this.leftId = leftId;
this.rightId = rightId;
this.text = text;
}
public int getLeftId()
{
return leftId;
}
public int getRightId()
{
return rightId;
}
public int getText()
{
return text;
}
}
Collection leftList = new ArrayList<>();
Collection rightList = new ArrayList<>();
Can be Joined like,
Collection results = CQ.query().from(leftList)
.innerJoin(CQ.query().from(rightList))
.on(LeftFyo::getId, RightDto::getLeftId)
.transformDirect(selection -> new JoinedDto(selection.getLeft().getText()
, selection.getLeft().getId()
, selection.getRight().getId())
)
.list();
Expressions
Filter query = CQ.filter()
.from(testList)
.where()
.exec(s -> s.getId() + 1).eq().value(2);