
com.github.geekonjava.query.Query Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Purify4J Show documentation
Show all versions of Purify4J Show documentation
Simple library for use to refine the Java Collection Objects
The newest version!
package com.github.geekonjava.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.github.geekonjava.matcher.Matcher;
import com.github.geekonjava.query.criteria.Criteria;
import com.github.geekonjava.query.criteria.CriteriaList;
import com.github.geekonjava.query.order.Order;
import com.github.geekonjava.query.order.OrderCriteria;
import com.github.geekonjava.query.specification.custom.AndSpecification;
import com.github.geekonjava.query.specification.custom.OrSpecification;
public class Query {
private final Collection collection;
private CriteriaList criterias;
private OrderCriteria orderCriteria;
public Query(Collection collection) {
this.collection = collection;
criterias = new CriteriaList();
}
public Query where(String method, Matcher matcher) {
Criteria criteria = new Criteria(method, matcher);
criterias.add(criteria);
return this;
}
public Query and(String method, Matcher matcher) {
Criteria criteria = new Criteria(method, matcher);
criteria.setSpecification(new AndSpecification());
criterias.add(criteria);
return this;
}
public Query or(String method, Matcher matcher) {
Criteria criteria = new Criteria(method, matcher);
criteria.setSpecification(new OrSpecification());
criterias.add(criteria);
return this;
}
public Query orderBy(String method, Order order) {
orderCriteria = new OrderCriteria(method, order);
return this;
}
public Query orderBy(String method) {
return orderBy(method, Order.ASC);
}
public List all() {
List all = new ArrayList();
for (T item : collection) {
if (criterias.match(item)) {
all.add(item);
}
}
if (orderCriteria != null) {
all = orderCriteria.sort(all);
}
return all;
}
public T first() {
List all = cloneCollection(collection);
if (orderCriteria != null) {
all = orderCriteria.sort(all);
}
for (T item : all) {
if (criterias.match(item)) {
return item;
}
}
return null;
}
private List cloneCollection(Collection collection) {
List list = new ArrayList();
for (T item : collection) {
list.add(item);
}
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy