Skip to content

Commit

Permalink
yegor256#308 modified algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedran authored and Vedran committed May 4, 2018
1 parent 06d8ee5 commit 3e7881f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
34 changes: 22 additions & 12 deletions src/main/java/org/cactoos/scalar/Equality.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
* <p>There is no thread-safety guarantee.
*
* @author Vedran Vatavuk (123vgv@gmail.com)
* @version $Id$
* @param <T> Type of result
* @param <T> Type of input
* @since 0.30.1
*/
public final class Equality<T extends Bytes> implements Scalar<Integer> {
Expand Down Expand Up @@ -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);
}
}
24 changes: 12 additions & 12 deletions src/test/java/org/cactoos/scalar/EqualityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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();
Expand Down

0 comments on commit 3e7881f

Please sign in to comment.