From 06d8ee5896d7ed9fc2342cc6365a9b905ace463d Mon Sep 17 00:00:00 2001 From: Vedran Date: Thu, 3 May 2018 19:06:01 +0200 Subject: [PATCH 1/5] #308 Equality --- .../java/org/cactoos/scalar/Equality.java | 83 ++++++++++++ .../java/org/cactoos/scalar/EqualityTest.java | 121 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 src/main/java/org/cactoos/scalar/Equality.java create mode 100644 src/test/java/org/cactoos/scalar/EqualityTest.java diff --git a/src/main/java/org/cactoos/scalar/Equality.java b/src/main/java/org/cactoos/scalar/Equality.java new file mode 100644 index 0000000000..3771d94537 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Equality.java @@ -0,0 +1,83 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import org.cactoos.Bytes; +import org.cactoos.Scalar; + +/** + * Equality. + * + *

There is no thread-safety guarantee. + * + * @author Vedran Vatavuk (123vgv@gmail.com) + * @version $Id$ + * @param Type of result + * @since 0.30.1 + */ +public final class Equality implements Scalar { + + /** + * Left. + */ + private final T left; + + /** + * Right. + */ + private final T right; + + /** + * Ctor. + * @param lft Left + * @param rght Right + */ + public Equality(final T lft, final T rght) { + this.left = lft; + this.right = rght; + } + + @Override + public Integer value() throws Exception { + final byte[] lft = this.left.asBytes(); + final byte[] rght = this.right.asBytes(); + 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; + } + result = lft[idx] - rght[idx]; + if (result != 0) { + break; + } + } + return (int) Math.signum(result); + } +} diff --git a/src/test/java/org/cactoos/scalar/EqualityTest.java b/src/test/java/org/cactoos/scalar/EqualityTest.java new file mode 100644 index 0000000000..9f7df16ea4 --- /dev/null +++ b/src/test/java/org/cactoos/scalar/EqualityTest.java @@ -0,0 +1,121 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import java.nio.ByteBuffer; +import org.cactoos.Bytes; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Equality}. + * @author Vedran Vatavuk (123vgv@gmail.com) + * @version $Id$ + * @since 0.30.1 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class EqualityTest { + + @Test + public void notEqualLeft() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Weight(0), new EqualityTest.Weight(500) + ).value(), + Matchers.equalTo(-1) + ); + } + + @Test + public void notEqualRight() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Weight(500), new EqualityTest.Weight(0) + ).value(), + Matchers.equalTo(1) + ); + } + + @Test + public void notEqualLeftWithSameSize() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Weight(400), new EqualityTest.Weight(500) + ).value(), + Matchers.equalTo(-1) + ); + } + + @Test + public void notEqualRightWithSameSize() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Weight(500), new EqualityTest.Weight(400) + ).value(), + Matchers.equalTo(1) + ); + } + + @Test + public void equal() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Weight(500), new EqualityTest.Weight(500) + ).value(), + Matchers.equalTo(0) + ); + } + + /** + * Weight. + */ + private static final class Weight implements Bytes { + + /** + * Kilos. + */ + private final int kilos; + + /** + * Ctor. + * @param kls Kilos + */ + Weight(final int kls) { + this.kilos = kls; + } + + @Override + public byte[] asBytes() { + return new UncheckedScalar<>( + new Ternary<>( + this.kilos == 0, + new byte[]{}, + ByteBuffer.allocate(4).putInt(this.kilos).array() + ) + ).value(); + } + } +} From 3e7881f36540b89e67614501c2fa77f927adadb7 Mon Sep 17 00:00:00 2001 From: Vedran Date: Fri, 4 May 2018 20:02:39 +0200 Subject: [PATCH 2/5] #308 modified algorithm --- .../java/org/cactoos/scalar/Equality.java | 34 ++++++++++++------- .../java/org/cactoos/scalar/EqualityTest.java | 24 ++++++------- 2 files changed, 34 insertions(+), 24 deletions(-) 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(); From 7a4e6a532c84c393c3e4d27ad9132edcc42b4c59 Mon Sep 17 00:00:00 2001 From: Vedran Date: Fri, 4 May 2018 23:27:22 +0200 Subject: [PATCH 3/5] #308 removed static method --- .../java/org/cactoos/scalar/Equality.java | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Equality.java b/src/main/java/org/cactoos/scalar/Equality.java index 694b83b34c..5e1f8df9da 100644 --- a/src/main/java/org/cactoos/scalar/Equality.java +++ b/src/main/java/org/cactoos/scalar/Equality.java @@ -69,25 +69,17 @@ public Integer value() throws Exception { final byte[] rght = this.right.asBytes(); return new Ternary<>( () -> lft.length == rght.length, - () -> Equality.compare(lft, rght), + () -> { + int result = 0; + for (int idx = rght.length - 1; idx > 0; --idx) { + result = lft[idx] - rght[idx]; + if (result != 0) { + break; + } + } + return Integer.signum(result); + }, () -> 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; - for (int idx = rght.length - 1; idx > 0; --idx) { - result = lft[idx] - rght[idx]; - if (result != 0) { - break; - } - } - return Integer.signum(result); - } } From e98ec3a2dc485a7a8f78a014af37d67e01a59a50 Mon Sep 17 00:00:00 2001 From: Vedran Date: Sat, 5 May 2018 10:19:04 +0200 Subject: [PATCH 4/5] #308 bug fixed --- .../java/org/cactoos/scalar/Equality.java | 2 +- .../java/org/cactoos/scalar/EqualityTest.java | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Equality.java b/src/main/java/org/cactoos/scalar/Equality.java index 5e1f8df9da..a64ce9ab57 100644 --- a/src/main/java/org/cactoos/scalar/Equality.java +++ b/src/main/java/org/cactoos/scalar/Equality.java @@ -71,7 +71,7 @@ public Integer value() throws Exception { () -> lft.length == rght.length, () -> { int result = 0; - for (int idx = rght.length - 1; idx > 0; --idx) { + for (int idx = rght.length - 1; idx >= 0; --idx) { result = lft[idx] - rght[idx]; if (result != 0) { break; diff --git a/src/test/java/org/cactoos/scalar/EqualityTest.java b/src/test/java/org/cactoos/scalar/EqualityTest.java index 8f9e3edfd6..21dbccfd19 100644 --- a/src/test/java/org/cactoos/scalar/EqualityTest.java +++ b/src/test/java/org/cactoos/scalar/EqualityTest.java @@ -23,7 +23,6 @@ */ package org.cactoos.scalar; -import java.nio.ByteBuffer; import org.cactoos.Bytes; import org.cactoos.matchers.ScalarHasValue; import org.hamcrest.MatcherAssert; @@ -43,7 +42,7 @@ public final class EqualityTest { public void notEqualLeft() throws Exception { MatcherAssert.assertThat( new Equality<>( - new EqualityTest.Weight(0), new EqualityTest.Weight(500) + new EqualityTest.Letters("A"), new EqualityTest.Letters("AB") ), new ScalarHasValue<>(-1) ); @@ -53,7 +52,7 @@ public void notEqualLeft() throws Exception { public void notEqualRight() throws Exception { MatcherAssert.assertThat( new Equality<>( - new EqualityTest.Weight(500), new EqualityTest.Weight(0) + new EqualityTest.Letters("AB"), new EqualityTest.Letters("A") ), new ScalarHasValue<>(1) ); @@ -63,7 +62,7 @@ public void notEqualRight() throws Exception { public void notEqualLeftWithSameSize() throws Exception { MatcherAssert.assertThat( new Equality<>( - new EqualityTest.Weight(400), new EqualityTest.Weight(500) + new EqualityTest.Letters("A"), new EqualityTest.Letters("B") ), new ScalarHasValue<>(-1) ); @@ -73,7 +72,7 @@ public void notEqualLeftWithSameSize() throws Exception { public void notEqualRightWithSameSize() throws Exception { MatcherAssert.assertThat( new Equality<>( - new EqualityTest.Weight(500), new EqualityTest.Weight(400) + new EqualityTest.Letters("B"), new EqualityTest.Letters("A") ), new ScalarHasValue<>(1) ); @@ -83,7 +82,17 @@ public void notEqualRightWithSameSize() throws Exception { public void equal() throws Exception { MatcherAssert.assertThat( new Equality<>( - new EqualityTest.Weight(500), new EqualityTest.Weight(500) + new EqualityTest.Letters("A"), new EqualityTest.Letters("A") + ), + new ScalarHasValue<>(0) + ); + } + + @Test + public void compareEmptyArrays() throws Exception { + MatcherAssert.assertThat( + new Equality<>( + new EqualityTest.Letters(""), new EqualityTest.Letters("") ), new ScalarHasValue<>(0) ); @@ -92,30 +101,24 @@ public void equal() throws Exception { /** * Weight. */ - private static final class Weight implements Bytes { + private static final class Letters implements Bytes { /** - * Kilos. + * Bytes. */ - private final int kilos; + private final String text; /** * Ctor. - * @param kls Kilos + * @param txt Text */ - Weight(final int kls) { - this.kilos = kls; + Letters(final String txt) { + this.text = txt; } @Override public byte[] asBytes() { - return new UncheckedScalar<>( - new Ternary<>( - this.kilos == 0, - new byte[]{0, 0}, - ByteBuffer.allocate(4).putInt(this.kilos).array() - ) - ).value(); + return this.text.getBytes(); } } } From 107803f959087b581a0b08b07965e0bebe712a48 Mon Sep 17 00:00:00 2001 From: Vedran Date: Sat, 5 May 2018 17:10:35 +0200 Subject: [PATCH 5/5] #308 updated since tag --- src/main/java/org/cactoos/scalar/Equality.java | 2 +- src/test/java/org/cactoos/scalar/EqualityTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Equality.java b/src/main/java/org/cactoos/scalar/Equality.java index a64ce9ab57..75eb39d42a 100644 --- a/src/main/java/org/cactoos/scalar/Equality.java +++ b/src/main/java/org/cactoos/scalar/Equality.java @@ -39,7 +39,7 @@ * @author Vedran Vatavuk (123vgv@gmail.com) * @version $Id$ * @param Type of input - * @since 0.30.1 + * @since 0.31 */ public final class Equality implements Scalar { diff --git a/src/test/java/org/cactoos/scalar/EqualityTest.java b/src/test/java/org/cactoos/scalar/EqualityTest.java index 21dbccfd19..5e905ca474 100644 --- a/src/test/java/org/cactoos/scalar/EqualityTest.java +++ b/src/test/java/org/cactoos/scalar/EqualityTest.java @@ -32,7 +32,7 @@ * Test case for {@link Equality}. * @author Vedran Vatavuk (123vgv@gmail.com) * @version $Id$ - * @since 0.30.1 + * @since 0.31 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) */