diff --git a/src/main/java/org/cactoos/scalar/Equality.java b/src/main/java/org/cactoos/scalar/Equality.java
index 3771d94537..694b83b34c 100644
--- a/src/main/java/org/cactoos/scalar/Equality.java
+++ b/src/main/java/org/cactoos/scalar/Equality.java
@@ -29,11 +29,16 @@
/**
* Equality.
*
+ * Returns:
+ * the value {@code 0} if {@code x == y};
+ * the value {@code -1} if {@code x < y};
+ * the value {@code 1} if {@code x > y}
+ *
*
There is no thread-safety guarantee.
*
* @author Vedran Vatavuk (123vgv@gmail.com)
* @version $Id$
- * @param Type of result
+ * @param Type of input
* @since 0.30.1
*/
public final class Equality implements Scalar {
@@ -62,22 +67,27 @@ public Equality(final T lft, final T rght) {
public Integer value() throws Exception {
final byte[] lft = this.left.asBytes();
final byte[] rght = this.right.asBytes();
+ return new Ternary<>(
+ () -> lft.length == rght.length,
+ () -> Equality.compare(lft, rght),
+ () -> Integer.signum(lft.length - rght.length)
+ ).value();
+ }
+
+ /**
+ * Compare two byte arrays of the same size.
+ * @param lft Left array
+ * @param rght Right array
+ * @return Integer Comparison result
+ */
+ private static int compare(final byte[] lft, final byte[] rght) {
int result = 0;
- final int max = Math.max(lft.length, rght.length);
- for (int idx = 0; idx < max; ++idx) {
- if (idx >= lft.length) {
- result = -1;
- break;
- }
- if (idx >= rght.length) {
- result = 1;
- break;
- }
+ for (int idx = rght.length - 1; idx > 0; --idx) {
result = lft[idx] - rght[idx];
if (result != 0) {
break;
}
}
- return (int) Math.signum(result);
+ return Integer.signum(result);
}
}
diff --git a/src/test/java/org/cactoos/scalar/EqualityTest.java b/src/test/java/org/cactoos/scalar/EqualityTest.java
index 9f7df16ea4..8f9e3edfd6 100644
--- a/src/test/java/org/cactoos/scalar/EqualityTest.java
+++ b/src/test/java/org/cactoos/scalar/EqualityTest.java
@@ -25,8 +25,8 @@
import java.nio.ByteBuffer;
import org.cactoos.Bytes;
+import org.cactoos.matchers.ScalarHasValue;
import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
import org.junit.Test;
/**
@@ -44,8 +44,8 @@ public void notEqualLeft() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(0), new EqualityTest.Weight(500)
- ).value(),
- Matchers.equalTo(-1)
+ ),
+ new ScalarHasValue<>(-1)
);
}
@@ -54,8 +54,8 @@ public void notEqualRight() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(0)
- ).value(),
- Matchers.equalTo(1)
+ ),
+ new ScalarHasValue<>(1)
);
}
@@ -64,8 +64,8 @@ public void notEqualLeftWithSameSize() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(400), new EqualityTest.Weight(500)
- ).value(),
- Matchers.equalTo(-1)
+ ),
+ new ScalarHasValue<>(-1)
);
}
@@ -74,8 +74,8 @@ public void notEqualRightWithSameSize() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(400)
- ).value(),
- Matchers.equalTo(1)
+ ),
+ new ScalarHasValue<>(1)
);
}
@@ -84,8 +84,8 @@ public void equal() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(500)
- ).value(),
- Matchers.equalTo(0)
+ ),
+ new ScalarHasValue<>(0)
);
}
@@ -112,7 +112,7 @@ public byte[] asBytes() {
return new UncheckedScalar<>(
new Ternary<>(
this.kilos == 0,
- new byte[]{},
+ new byte[]{0, 0},
ByteBuffer.allocate(4).putInt(this.kilos).array()
)
).value();