private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) { final Iterable<Clause> clauses = clausesFor(propertyNamesIter); ComparisonChain chain = ComparisonChain.start(); for (final Clause clause : clauses) { final Comparable<T> propertyValueOfP = (Comparable<T>) clause.getValueOf(p); final Comparable<T> propertyValueOfQ = (Comparable<T>) clause.getValueOf(q); chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering()); } return chain.result(); } //endregion
@Override public Clause apply(String input) { return Clause.parse(input); } });
private static boolean equals(final Object p, final Object q, final Iterable<String> propertyNamesIter) { final Iterable<Clause> clauses = clausesFor(propertyNamesIter); for (final Clause clause : clauses) { final Object pValue = clause.getValueOf(p); final Object qValue = clause.getValueOf(q); if(!Objects.equal(pValue, qValue)) { return false; } } return true; } //endregion
private String toStringOf(final Object p, final Iterable<String> propertyNamesIter) { final ToStringHelper stringHelper = Objects.toStringHelper(p); for (final Clause clause : clausesFor(propertyNamesIter)) { stringHelper.add(clause.getPropertyName(), asString(clause, p)); } return stringHelper.toString(); }
static Clause parse(String input) { final Matcher matcher = pattern.matcher(input); if(!matcher.matches()) { return null; } return new Clause(matcher.group(1), Direction.valueOfElseAsc(matcher.group(2))); } Clause(String propertyName, Direction direction) {
public Object getValueOf(Object obj) { if(obj == null) { return null; } final String methodNameSuffix = upperFirst(propertyName); final String getMethodName = "get" + methodNameSuffix; try { final Method getterMethod = obj.getClass().getMethod(getMethodName); return getterMethod.invoke(obj); } catch (NoSuchMethodException e) { final String isMethodName = "is" + methodNameSuffix; try { final Method getterMethod = obj.getClass().getMethod(isMethodName); return getterMethod.invoke(obj); } catch (NoSuchMethodException ex) { throw new IllegalArgumentException("No such method ' " + getMethodName + "' or '" + isMethodName + "'", e); } catch (Exception e1) { // some other reason; for example, a JDOUserException if the object has been deleted and interaction with its properties is not permitted. throw new RuntimeException(e1); } } catch (Exception e) { // some other reason; for example, a JDOUserException if the object has been deleted and interaction with its properties is not permitted. throw new RuntimeException(e); } } private static String upperFirst(final String str) {
private static boolean equals(final Object p, final Object q, final Iterable<String> propertyNamesIter) { final Iterable<Clause> clauses = clausesFor(propertyNamesIter); for (final Clause clause : clauses) { final Object pValue = clause.getValueOf(p); final Object qValue = clause.getValueOf(q); if(!Objects.equal(pValue, qValue)) { return false; } } return true; } //endregion
private String toStringOf(final Object p, final Iterable<String> propertyNamesIter) { final ToStringHelper stringHelper = Objects.toStringHelper(p); for (final Clause clause : clausesFor(propertyNamesIter)) { stringHelper.add(clause.getPropertyName(), asString(clause, p)); } return stringHelper.toString(); }
private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) { if(p == null) { return -1;} if(q == null) { return +1;} if(p.getClass() != q.getClass()) { // just sort on the class type return Ordering.natural().onResultOf(new Function<Object, String>() { @Override public String apply(final Object o) { return o.getClass().getSimpleName(); } }).compare(p, q); } final Iterable<Clause> clauses = clausesFor(propertyNamesIter); ComparisonChain chain = ComparisonChain.start(); for (final Clause clause : clauses) { final Comparable<T> propertyValueOfP = (Comparable<T>) clause.getValueOf(p); final Comparable<T> propertyValueOfQ = (Comparable<T>) clause.getValueOf(q); chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering()); } return chain.result(); } //endregion
private static int hashCode(final Object obj, final Iterable<String> propertyNamesIter) { final List<Object> propertyValues = Lists.newArrayList(); for (final Clause clause : clausesFor(propertyNamesIter)) { final Object propertyValue = clause.getValueOf(obj); if(propertyValue != null) { propertyValues.add(propertyValue); } } return Objects.hashCode(propertyValues.toArray()); } //endregion
private static int hashCode(final Object obj, final Iterable<String> propertyNamesIter) { final List<Object> propertyValues = Lists.newArrayList(); for (final Clause clause : clausesFor(propertyNamesIter)) { final Object propertyValue = clause.getValueOf(obj); if(propertyValue != null) { propertyValues.add(propertyValue); } } return Objects.hashCode(propertyValues.toArray()); } //endregion
private String asString(final Clause clause, Object p) { final Object value = clause.getValueOf(p); if(value == null) { return null; } for (ObjectContracts.ToStringEvaluator evaluator : evaluators) { if(evaluator.canEvaluate(value)) { return evaluator.evaluate(value); } } return value.toString(); }
private String asString(final Clause clause, Object p) { final Object value = clause.getValueOf(p); if(value == null) { return null; } for (ToStringEvaluator evaluator : evaluators) { if(evaluator.canEvaluate(value)) { return evaluator.evaluate(value); } } return value.toString(); }