Collections - griffio/griffio.github.io GitHub Wiki
com.mysema.query.collections
Query entities that are generated can be used with a CollQueryFactory to replace this Mundane Java code that maps an input collection to an output collection.
private List<String> uniqueSalaryNames(Collection<EmployeeSalary> employeeSalaries) {
Set<String> result = Sets.newHashSet();
for (EmployeeSalary salary : employeeSalaries) {
for (SalaryDetail detail : salary.getSalaryDetails()) {
if (RelevantSalaryUtil.isSalaryRelevant(detail.getSalaryName())) {
result.add(detail.getSalaryName());
}
}
}
return newArrayList(result);
}
List<String> uniqueSalaryNames = CollQueryFactory
.from(QEmployeeSalary.employeeSalary, employeeSalary)
.innerJoin(QEmployeeSalary.employeeSalary.salaryDetail, QSalaryDetail.salaryDetail)
.where(QSalaryDetail.salaryDetail.isSalaryRelevant())
.distinct()
.list(QEmployeeSalary.employeeSalary.salaryName);
Aggregate or 'fold' a collection using Mundane Java. Even the Guava library doesn't advocate higher-order functional programming using Java.
public BigDecimal sum(List<SalaryDetail> salaryDetails) {
BigDecimal sum = BigDecimal.ZERO;
for (SalaryDetail salaryDetail : salaryDetails) {
sum = sum.add(salaryDetail.getSalary());
}
return sum;
};
BigDecimal sum = CollQueryFactory
.from(QSalaryDetail.salaryDetail, salaryDetails)
.singleResult(QSalaryDetail.salaryDetail.salary.sum());
com.mysema.query
A post-processor transformer for aggregation that works with com.mysema.query.group.* classes.
Takes a collection and returns a collection containing a new projection of the aggregate for each group. Here, multiple salaries with the same name will be grouped into a new element containing the sum total of the group.
List<SalaryDetail> aggregatedSalaries = CollQueryFactory.from(QSalaryDetail.salaryDetail, salaryDetails)
.transform(GroupBy.groupBy(QSalaryDetail.salaryDetail.salaryName)
.list(QSalaryDetail.create(QSalaryDetail.salaryDetail.salaryName,
GroupBy.sum(QSalaryDetail.salaryDetail.salary))));