Rimosso Coollection

This commit is contained in:
Giuseppe Scorrano 2022-02-21 10:29:25 +01:00
parent 6dcc711006
commit b8dbde5ee1
19 changed files with 0 additions and 472 deletions

View File

@ -1,49 +0,0 @@
package it.integry.integrywmsnative.core.coollection;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
import it.integry.integrywmsnative.core.coollection.matcher.custom.Contains;
import it.integry.integrywmsnative.core.coollection.matcher.custom.Equals;
import it.integry.integrywmsnative.core.coollection.matcher.custom.EqualsIgnoreCase;
import it.integry.integrywmsnative.core.coollection.matcher.custom.GreaterThan;
import it.integry.integrywmsnative.core.coollection.matcher.custom.IsNull;
import it.integry.integrywmsnative.core.coollection.matcher.custom.LessThan;
import it.integry.integrywmsnative.core.coollection.matcher.custom.Not;
import it.integry.integrywmsnative.core.coollection.query.Query;
import java.util.Collection;
public class Coollection {
public static <T> Query<T> from(Collection<T> collection) {
return new Query<T>(collection);
}
public static Matcher eq(Object value) {
return new Equals(value);
}
public static Matcher contains(String value) {
return new Contains(value);
}
public static Matcher eqIgnoreCase(String value) {
return new EqualsIgnoreCase(value);
}
public static Matcher not(Matcher matcher) {
return new Not(matcher);
}
public static Matcher greaterThan(Number value) {
return new GreaterThan(value);
}
public static Matcher lessThan(Number value) {
return new LessThan(value);
}
public static Matcher isNull() {
return new IsNull();
}
}

View File

@ -1,7 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher;
public interface Matcher {
boolean match(Object value);
}

View File

@ -1,18 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class Contains implements Matcher {
private final String matcherValue;
public Contains(String matcherValue) {
this.matcherValue = matcherValue;
}
@Override
public boolean match(Object value) {
return String.valueOf(value).contains(matcherValue);
}
}

View File

@ -1,18 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class Equals implements Matcher {
private final Object value;
public Equals(Object value) {
this.value = value;
}
@Override
public boolean match(Object anotherValue) {
return value.equals(anotherValue);
}
}

View File

@ -1,18 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class EqualsIgnoreCase implements Matcher {
private final String value;
public EqualsIgnoreCase(String value) {
this.value = value;
}
@Override
public boolean match(Object anotherValue) {
return (value).equalsIgnoreCase((String) anotherValue);
}
}

View File

@ -1,21 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class GreaterThan implements Matcher {
private final Number value;
public GreaterThan(Number value) {
this.value = value;
}
@Override
public boolean match(Object matchValue) {
if (matchValue == null) {
return false;
}
return ((Number) matchValue).doubleValue() > value.doubleValue();
}
}

View File

@ -1,12 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class IsNull implements Matcher {
@Override
public boolean match(Object value) {
return value == null;
}
}

View File

@ -1,21 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class LessThan implements Matcher {
private final Number value;
public LessThan(Number value) {
this.value = value;
}
@Override
public boolean match(Object matchValue) {
if (matchValue == null) {
return false;
}
return ((Number) matchValue).doubleValue() < value.doubleValue();
}
}

View File

@ -1,18 +0,0 @@
package it.integry.integrywmsnative.core.coollection.matcher.custom;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
public class Not implements Matcher {
private final Matcher matcher;
public Not(Matcher matcher) {
this.matcher = matcher;
}
@Override
public boolean match(Object value) {
return !matcher.match(value);
}
}

View File

@ -1,90 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
import it.integry.integrywmsnative.core.coollection.query.criteria.Criteria;
import it.integry.integrywmsnative.core.coollection.query.criteria.CriteriaList;
import it.integry.integrywmsnative.core.coollection.query.order.Order;
import it.integry.integrywmsnative.core.coollection.query.order.OrderCriteria;
import it.integry.integrywmsnative.core.coollection.query.specification.custom.AndSpecification;
import it.integry.integrywmsnative.core.coollection.query.specification.custom.OrSpecification;
public class Query<T> {
private final Collection<T> collection;
private CriteriaList<T> criterias;
private OrderCriteria<T> orderCriteria;
public Query(Collection<T> collection) {
this.collection = collection;
criterias = new CriteriaList<T>();
}
public Query<T> where(String method, Matcher matcher) {
Criteria<T> criteria = new Criteria<T>(method, matcher);
criterias.add(criteria);
return this;
}
public Query<T> and(String method, Matcher matcher) {
Criteria<T> criteria = new Criteria<T>(method, matcher);
criteria.setSpecification(new AndSpecification<T>());
criterias.add(criteria);
return this;
}
public Query<T> or(String method, Matcher matcher) {
Criteria<T> criteria = new Criteria<T>(method, matcher);
criteria.setSpecification(new OrSpecification<T>());
criterias.add(criteria);
return this;
}
public Query<T> orderBy(String method, Order order) {
orderCriteria = new OrderCriteria<T>(method, order);
return this;
}
public Query<T> orderBy(String method) {
return orderBy(method, Order.ASC);
}
public List<T> all() {
List<T> all = new ArrayList<T>();
for (T item : collection) {
if (criterias.match(item)) {
all.add(item);
}
}
if (orderCriteria != null) {
all = orderCriteria.sort(all);
}
return all;
}
public T first() {
List<T> all = cloneCollection(collection);
if (orderCriteria != null) {
all = orderCriteria.sort(all);
}
for (T item : all) {
if (criterias.match(item)) {
return item;
}
}
return null;
}
private List<T> cloneCollection(Collection<T> collection) {
List<T> list = new ArrayList<T>();
for (T item : collection) {
list.add(item);
}
return list;
}
}

View File

@ -1,34 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.criteria;
import it.integry.integrywmsnative.core.coollection.matcher.Matcher;
import it.integry.integrywmsnative.core.coollection.query.specification.Specification;
import it.integry.integrywmsnative.core.coollection.reflection.Phanton;
public class Criteria<T> {
private final String method;
private final Matcher matcher;
private Specification<T> specification;
public Criteria(String method, Matcher matcher) {
this.method = method;
this.matcher = matcher;
}
public void setSpecification(Specification<T> specification) {
this.specification = specification;
}
public Specification<T> specification() {
return specification;
}
public boolean match(T item) {
try {
Object value = Phanton.from(item).call(method);
return matcher.match(value);
} catch (Exception err) {
throw new RuntimeException(err);
}
}
}

View File

@ -1,34 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.criteria;
import java.util.ArrayList;
import java.util.List;
public class CriteriaList<T> {
private List<Criteria<T>> criterias;
public CriteriaList() {
criterias = new ArrayList<Criteria<T>>();
}
public void add(Criteria<T> criteria) {
criterias.add(criteria);
}
public boolean match(T item) {
if (criterias.size() == 0) {
return true;
}
if (criterias.size() == 1) {
return criterias.get(0).match(item);
}
boolean matched = true;
for (int i = criterias.size() - 1; i > 0; i--) {
Criteria<T> one = criterias.get(i);
Criteria<T> other = criterias.get(i - 1);
matched = matched && one.specification().match(item, one, other);
}
return matched;
}
}

View File

@ -1,5 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.order;
public enum Order {
ASC, DESC
}

View File

@ -1,30 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.order;
import it.integry.integrywmsnative.core.coollection.reflection.Phanton;
import java.util.Comparator;
public class OrderComparator<T> implements Comparator<T> {
private final String method;
public OrderComparator(String method) {
this.method = method;
}
@Override
@SuppressWarnings("unchecked")
public int compare(T one, T other) {
Object oneValue = Phanton.from(one).call(method);
Object otherValue = Phanton.from(other).call(method);
if (oneValue == null || otherValue == null) {
return 0;
}
if (oneValue instanceof Comparable) {
return ((Comparable<Object>) oneValue).compareTo(otherValue);
} else {
return 0;
}
}
}

View File

@ -1,24 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.order;
import java.util.Collections;
import java.util.List;
public class OrderCriteria<T> {
private final String method;
private final Order order;
public OrderCriteria(String method, Order order) {
this.method = method;
this.order = order;
}
public List<T> sort(List<T> list) {
Collections.sort(list, new OrderComparator<T>(method));
if (order == Order.DESC) {
Collections.reverse(list);
}
return list;
}
}

View File

@ -1,9 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.specification;
import it.integry.integrywmsnative.core.coollection.query.criteria.Criteria;
public interface Specification<T> {
boolean match(T item, Criteria<T> one, Criteria<T> other);
}

View File

@ -1,13 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.specification.custom;
import it.integry.integrywmsnative.core.coollection.query.criteria.Criteria;
import it.integry.integrywmsnative.core.coollection.query.specification.Specification;
public class AndSpecification<T> implements Specification<T> {
@Override
public boolean match(T item, Criteria<T> one, Criteria<T> other) {
return one.match(item) && other.match(item);
}
}

View File

@ -1,14 +0,0 @@
package it.integry.integrywmsnative.core.coollection.query.specification.custom;
import it.integry.integrywmsnative.core.coollection.query.criteria.Criteria;
import it.integry.integrywmsnative.core.coollection.query.specification.Specification;
public class OrSpecification<T> implements Specification<T> {
@Override
public boolean match(T item, Criteria<T> one, Criteria<T> other) {
return one.match(item) || other.match(item);
}
}

View File

@ -1,37 +0,0 @@
package it.integry.integrywmsnative.core.coollection.reflection;
import java.lang.reflect.Field;
public class Phanton<T> {
private final T target;
private Class<?> clazz;
private Phanton(T target) {
this.target = target;
clazz = target.getClass();
}
public static <T> Phanton<T> from(T target) {
return new Phanton<T>(target);
}
public Object call(String name) {
return invoke(name);
}
private Object invoke(String name) {
for (final Field field : clazz.getDeclaredFields()) {
try {
if (name.equals(field.getName())) {
field.setAccessible(Boolean.TRUE);
return field.get(target);
}
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
}
}
throw new RuntimeException("No such property with name " + name);
}
}