/** * Evaluates which of p and q is first. * * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! * @param propertyNames - the property name or names, CSV format. If multiple properties, use the {@link #compare(Object, Object, String...) varargs} overloaded version of this method. */ @Deprecated @SuppressWarnings("unchecked") public static <T> int compare(final T p, final T q, final String propertyNames) { final Iterable<String> propertyNamesIter = csvToIterable(propertyNames); return compare(p, q, propertyNamesIter); }
/** * Returns the hashCode for the object using the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! * @param propertyNames - the property name or names, CSV format. If multiple properties, use the {@link #hashCode(Object, String...)} varargs} overloaded version of this method. */ @Deprecated public static int hashCode(Object obj, String propertyNames) { final Iterable<String> propertyNamesIter = csvToIterable(propertyNames); return hashCode(obj, propertyNamesIter); }
/** * Returns a string representation of two objects, considering just the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! * @param propertyNames - the property name or names, CSV format. If multiple properties, use the {@link #toString(Object, String...)} varargs} overloaded version of this method. */ @Deprecated public String toStringOf(Object p, String propertyNames) { final Iterable<String> propertyNamesIter = csvToIterable(propertyNames); return toStringOf(p, propertyNamesIter); }
/** * Evaluates which of p and q is first. * * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! */ @Deprecated @SuppressWarnings("unchecked") public static <T> int compare(final T p, final T q, final String... propertyNames) { final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames); return compare(p, q, propertyNamesIter); }
/** * Returns the hashCode for the object using the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! */ @Deprecated public static int hashCode(Object obj, String... propertyNames) { final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames); return hashCode(obj, propertyNamesIter); }
/** * Returns a string representation of two objects, considering just the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! */ @Deprecated public String toStringOf(Object p, String... propertyNames) { final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames); return toStringOf(p, propertyNamesIter); }
/** * Returns whether two objects are equal, considering just the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! */ @Deprecated public static boolean equals(Object p, Object q, String... propertyNames) { if(p==null && q==null) { return true; } if(p==null || q==null) { return false; } if(p.getClass() != q.getClass()) { return false; } final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames); return equals(p, q, propertyNamesIter); }
/** * Returns whether two objects are equal, considering just the specified property name(s). * @deprecated - please be aware that this utility heavily uses reflection. We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention! * @param propertyNames - the property name or names, CSV format. If multiple properties, use the {@link #equals(Object, Object, String...)} varargs} overloaded version of this method. */ @Deprecated public static boolean equals(Object p, Object q, String propertyNames) { if(p==null && q==null) { return true; } if(p==null || q==null) { return false; } if(p.getClass() != q.getClass()) { return false; } final Iterable<String> propertyNamesIter = csvToIterable(propertyNames); return equals(p, q, propertyNamesIter); }
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 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 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 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