Skip to content

Commit 25c8bda

Browse files
committed
Revert "conditional helpers throw ClassCastExceptions when arguments are different classes fix jknack#827"
This reverts commit 56e53f3.
1 parent 0a5b2ee commit 25c8bda

File tree

2 files changed

+9
-71
lines changed

2 files changed

+9
-71
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/helper/ConditionalHelpers.java

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
*/
1818
package com.github.jknack.handlebars.helper;
1919

20-
import static org.apache.commons.lang3.Validate.isTrue;
21-
22-
import java.io.IOException;
23-
import java.util.Objects;
24-
2520
import com.github.jknack.handlebars.Handlebars;
2621
import com.github.jknack.handlebars.Helper;
2722
import com.github.jknack.handlebars.Options;
2823
import com.github.jknack.handlebars.TagType;
24+
import static org.apache.commons.lang3.Validate.isTrue;
25+
import org.apache.commons.lang3.builder.EqualsBuilder;
26+
27+
import java.io.IOException;
2928

3029
/**
3130
* Implementation of equals, greater, lessThan, and, or, etc.. operators.
@@ -60,7 +59,7 @@ public enum ConditionalHelpers implements Helper<Object> {
6059
eq {
6160
@Override public Object apply(final Object a, final Options options) throws IOException {
6261
Object b = options.param(0, null);
63-
boolean result = eq(a, b);
62+
boolean result = new EqualsBuilder().append(a, b).isEquals();
6463
if (options.tagType == TagType.SECTION) {
6564
return result ? options.fn() : options.inverse();
6665
}
@@ -95,7 +94,7 @@ public enum ConditionalHelpers implements Helper<Object> {
9594
neq {
9695
@Override public Object apply(final Object a, final Options options) throws IOException {
9796
Object b = options.param(0, null);
98-
boolean result = !eq(a, b);
97+
boolean result = !new EqualsBuilder().append(a, b).isEquals();
9998
if (options.tagType == TagType.SECTION) {
10099
return result ? options.fn() : options.inverse();
101100
}
@@ -395,46 +394,8 @@ public enum ConditionalHelpers implements Helper<Object> {
395394
* @return Int.
396395
*/
397396
protected int cmp(final Object a, final Object b) {
398-
try {
399-
isTrue(a instanceof Comparable, "Not a comparable: " + a);
400-
isTrue(b instanceof Comparable, "Not a comparable: " + b);
401-
return ((Comparable) a).compareTo(b);
402-
} catch (ClassCastException x) {
403-
return Double.compare(toDoubleOrError(a, x), toDoubleOrError(b, x));
404-
}
405-
}
406-
407-
/**
408-
* Compare two values. Number equality are treated as equals on they are like: 2 vs 2.0
409-
*
410-
* @param a First value.
411-
* @param b Second value.
412-
* @return True when equals.
413-
*/
414-
protected boolean eq(final Object a, final Object b) {
415-
boolean value = Objects.equals(a, b);
416-
if (!value) {
417-
if (a instanceof Number && b instanceof Number) {
418-
// int vs double: 2 vs 2.0
419-
return ((Number) a).doubleValue() == ((Number) b).doubleValue();
420-
}
421-
}
422-
return value;
423-
}
424-
425-
/**
426-
* Generate double from value or throw existing exception.
427-
* @param value Value to cast.
428-
* @param x Exception.
429-
* @return Double.
430-
*/
431-
private double toDoubleOrError(final Object value, final RuntimeException x) {
432-
if (value instanceof Double) {
433-
return (Double) value;
434-
}
435-
if (value instanceof Number) {
436-
return ((Number) value).doubleValue();
437-
}
438-
throw x;
397+
isTrue(a instanceof Comparable, "Not a comparable: " + a);
398+
isTrue(b instanceof Comparable, "Not a comparable: " + b);
399+
return ((Comparable) a).compareTo(b);
439400
}
440401
}

handlebars/src/test/java/com/github/jknack/handlebars/Issue827.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)