From 513b908f93a5b5f1e369ea239252b9cbceb74045 Mon Sep 17 00:00:00 2001 From: Balovnev Eduard Date: Tue, 28 Nov 2017 11:39:06 +0600 Subject: [PATCH 1/7] #460 Max and Min was renamed to HighestOf and LowestOf to support Comparable implementations --- src/main/java/org/cactoos/func/MaxFunc.java | 45 +++++++++ src/main/java/org/cactoos/func/MinFunc.java | 45 +++++++++ src/main/java/org/cactoos/scalar/Folded.java | 83 ++++++++++++++++ .../java/org/cactoos/scalar/HighestOf.java | 79 ++++++++++++++++ .../java/org/cactoos/scalar/LowestOf.java | 77 +++++++++++++++ .../org/cactoos/scalar/HighestOfTest.java | 94 +++++++++++++++++++ .../java/org/cactoos/scalar/LowestOfTest.java | 94 +++++++++++++++++++ 7 files changed, 517 insertions(+) create mode 100644 src/main/java/org/cactoos/func/MaxFunc.java create mode 100644 src/main/java/org/cactoos/func/MinFunc.java create mode 100644 src/main/java/org/cactoos/scalar/Folded.java create mode 100644 src/main/java/org/cactoos/scalar/HighestOf.java create mode 100644 src/main/java/org/cactoos/scalar/LowestOf.java create mode 100644 src/test/java/org/cactoos/scalar/HighestOfTest.java create mode 100644 src/test/java/org/cactoos/scalar/LowestOfTest.java diff --git a/src/main/java/org/cactoos/func/MaxFunc.java b/src/main/java/org/cactoos/func/MaxFunc.java new file mode 100644 index 0000000000..52c2ea18b1 --- /dev/null +++ b/src/main/java/org/cactoos/func/MaxFunc.java @@ -0,0 +1,45 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.func; + +import org.cactoos.BiFunc; + +/** + * Function, finding max among two items. + * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param comparable type + * @since 0.9 + */ +public final class MaxFunc> implements BiFunc { + @Override + public T apply(final T first, final T second) throws Exception { + T max = first; + if (second.compareTo(max) > 0) { + max = second; + } + return max; + } +} diff --git a/src/main/java/org/cactoos/func/MinFunc.java b/src/main/java/org/cactoos/func/MinFunc.java new file mode 100644 index 0000000000..b760d35278 --- /dev/null +++ b/src/main/java/org/cactoos/func/MinFunc.java @@ -0,0 +1,45 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.func; + +import org.cactoos.BiFunc; + +/** + * Function, finding min among two items. + * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param comparable type + * @since 0.9 + */ +public final class MinFunc> implements BiFunc { + @Override + public T apply(final T first, final T second) throws Exception { + T min = first; + if (second.compareTo(min) < 0) { + min = second; + } + return min; + } +} diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java new file mode 100644 index 0000000000..21b18f5830 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -0,0 +1,83 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.util.Iterator; +import java.util.NoSuchElementException; +import org.cactoos.BiFunc; +import org.cactoos.Scalar; + +/** + * Folds iterable via BiFunc + * + *

There is no thread-safety guarantee. + * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param Scalar type + * @since 0.9 + */ +public final class Folded implements Scalar { + + /** + * Items. + */ + private final Iterable> items; + + /** + * Folding function. + */ + private final BiFunc fold; + + /** + * Ctor. + * @param fold Folding function + * @param items The items + */ + public Folded(final BiFunc fold, final Iterable> items) { + this.items = items; + this.fold = fold; + } + + @Override + public T value() throws Exception { + final Iterator> iter = this.items.iterator(); + if (!iter.hasNext()) { + throw new NoSuchElementException( + "Can't find first element in an empty iterable" + ); + } + T acc = iter.next().value(); + while (iter.hasNext()) { + final T next = iter.next().value(); + acc = this.fold.apply(acc, next); + } + return acc; + } +} diff --git a/src/main/java/org/cactoos/scalar/HighestOf.java b/src/main/java/org/cactoos/scalar/HighestOf.java new file mode 100644 index 0000000000..09d4b1bc75 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/HighestOf.java @@ -0,0 +1,79 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.Scalar; +import org.cactoos.func.MaxFunc; +import org.cactoos.iterable.IterableOf; + +/** + * Find the greater among items. + * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @param Scalar type + * @see UncheckedScalar + * @see IoCheckedScalar + * @since 0.10 + */ +public final class HighestOf> implements Scalar { + + /** + * Items. + */ + private final Scalar result; + + /** + * Ctor. + * @param scalars The items + */ + @SafeVarargs + public HighestOf(final Scalar... scalars) { + this(new IterableOf<>(scalars)); + } + + /** + * Ctor. + * @param iterable The items + */ + public HighestOf(final Iterable> iterable) { + this.result = new Folded<>( + new MaxFunc<>(), + iterable + ); + } + + @Override + public T value() throws Exception { + return this.result.value(); + } + +} diff --git a/src/main/java/org/cactoos/scalar/LowestOf.java b/src/main/java/org/cactoos/scalar/LowestOf.java new file mode 100644 index 0000000000..8c3a89b6ba --- /dev/null +++ b/src/main/java/org/cactoos/scalar/LowestOf.java @@ -0,0 +1,77 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.Scalar; +import org.cactoos.func.MinFunc; +import org.cactoos.iterable.IterableOf; + +/** + * Find the smaller among items. + * + *

There is no thread-safety guarantee. + * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @param Scalar type + * @since 0.9 + */ +public final class LowestOf> implements Scalar { + + /** + * Items. + */ + private final Scalar result; + + /** + * Ctor. + * @param scalars The items + */ + @SafeVarargs + public LowestOf(final Scalar... scalars) { + this(new IterableOf<>(scalars)); + } + + /** + * Ctor. + * @param iterable The items + */ + public LowestOf(final Iterable> iterable) { + this.result = new Folded<>( + new MinFunc<>(), + iterable + ); + } + + @Override + public T value() throws Exception { + return this.result.value(); + } + +} diff --git a/src/test/java/org/cactoos/scalar/HighestOfTest.java b/src/test/java/org/cactoos/scalar/HighestOfTest.java new file mode 100644 index 0000000000..80a488f6af --- /dev/null +++ b/src/test/java/org/cactoos/scalar/HighestOfTest.java @@ -0,0 +1,94 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.util.Collections; +import java.util.NoSuchElementException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link HighestOf}. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @author Eduard Balovnev (bedward70@mail.ru) + * @version $Id$ + * @since 0.10 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class HighestOfTest { + + @Test(expected = NoSuchElementException.class) + public void maxAmongEmptyTest() throws Exception { + new HighestOf<>(() -> Collections.emptyIterator()).value(); + } + + @Test + public void maxAmongOneTest() throws Exception { + final int num = 10; + MatcherAssert.assertThat( + "Can't find the greater among one", + new HighestOf(() -> new Integer(num)).value(), + Matchers.equalTo(num) + ); + } + + @Test + public void maxAmongManyTest() throws Exception { + final int num = 10; + MatcherAssert.assertThat( + "Can't find the greater among many", + new HighestOf( + () -> new Integer(num), + () -> new Integer(0), + () -> new Integer(-1), + () -> new Integer(2) + ).value(), + Matchers.equalTo(num) + ); + } + + @Test + public void highestStringTest() throws Exception { + final String lowest = "Apple"; + final String highest = "Orange"; + MatcherAssert.assertThat( + "Can't find the highest string among many", + new HighestOf(() -> lowest, () -> highest).value(), + Matchers.equalTo(highest) + ); + } + + @Test + public void highestCharTest() throws Exception { + final char lowest = 'A'; + final char highest = 'B'; + MatcherAssert.assertThat( + "Can't find the highest char among many", + new HighestOf(() -> lowest, () -> highest).value(), + Matchers.equalTo(highest) + ); + } +} diff --git a/src/test/java/org/cactoos/scalar/LowestOfTest.java b/src/test/java/org/cactoos/scalar/LowestOfTest.java new file mode 100644 index 0000000000..3fcfedbcf0 --- /dev/null +++ b/src/test/java/org/cactoos/scalar/LowestOfTest.java @@ -0,0 +1,94 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 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.util.Collections; +import java.util.NoSuchElementException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link LowestOf}. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @author Eduard Balovnev (bedward70@mail.ru) + * @version $Id$ + * @since 0.10 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class LowestOfTest { + + @Test(expected = NoSuchElementException.class) + public void minAmongEmptyTest() throws Exception { + new LowestOf<>(() -> Collections.emptyIterator()).value(); + } + + @Test + public void minAmongOneTest() throws Exception { + final int num = 10; + MatcherAssert.assertThat( + "Can't find the smaller among one", + new LowestOf(() -> new Integer(num)).value(), + Matchers.equalTo(num) + ); + } + + @Test + public void minAmongManyTest() throws Exception { + final int num = -1; + MatcherAssert.assertThat( + "Can't find the smaller among many", + new LowestOf( + () -> new Integer(1), + () -> new Integer(0), + () -> new Integer(num), + () -> new Integer(2) + ).value(), + Matchers.equalTo(num) + ); + } + + @Test + public void lowestStringTest() throws Exception { + final String lowest = "Apple"; + final String highest = "Orange"; + MatcherAssert.assertThat( + "Can't find the lowest string among many", + new LowestOf(() -> lowest, () -> highest).value(), + Matchers.equalTo(lowest) + ); + } + + @Test + public void lowestCharTest() throws Exception { + final char lowest = 'A'; + final char highest = 'B'; + MatcherAssert.assertThat( + "Can't find the lowest char among many", + new LowestOf(() -> lowest, () -> highest).value(), + Matchers.equalTo(lowest) + ); + } +} From eb5ed804056d8ee7e7549494ad9f7676c2cad42e Mon Sep 17 00:00:00 2001 From: Balovnev Eduard Date: Tue, 28 Nov 2017 17:27:16 +0600 Subject: [PATCH 2/7] #460 removing of MaxFunc and MinFunc --- src/main/java/org/cactoos/func/MaxFunc.java | 45 ------------------- src/main/java/org/cactoos/func/MinFunc.java | 45 ------------------- .../java/org/cactoos/scalar/HighestOf.java | 11 ++++- .../java/org/cactoos/scalar/LowestOf.java | 11 ++++- 4 files changed, 18 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/org/cactoos/func/MaxFunc.java delete mode 100644 src/main/java/org/cactoos/func/MinFunc.java diff --git a/src/main/java/org/cactoos/func/MaxFunc.java b/src/main/java/org/cactoos/func/MaxFunc.java deleted file mode 100644 index 52c2ea18b1..0000000000 --- a/src/main/java/org/cactoos/func/MaxFunc.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.func; - -import org.cactoos.BiFunc; - -/** - * Function, finding max among two items. - * - * @author Alexander Dyadyushenko (gookven@gmail.com) - * @version $Id$ - * @param comparable type - * @since 0.9 - */ -public final class MaxFunc> implements BiFunc { - @Override - public T apply(final T first, final T second) throws Exception { - T max = first; - if (second.compareTo(max) > 0) { - max = second; - } - return max; - } -} diff --git a/src/main/java/org/cactoos/func/MinFunc.java b/src/main/java/org/cactoos/func/MinFunc.java deleted file mode 100644 index b760d35278..0000000000 --- a/src/main/java/org/cactoos/func/MinFunc.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 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.func; - -import org.cactoos.BiFunc; - -/** - * Function, finding min among two items. - * - * @author Alexander Dyadyushenko (gookven@gmail.com) - * @version $Id$ - * @param comparable type - * @since 0.9 - */ -public final class MinFunc> implements BiFunc { - @Override - public T apply(final T first, final T second) throws Exception { - T min = first; - if (second.compareTo(min) < 0) { - min = second; - } - return min; - } -} diff --git a/src/main/java/org/cactoos/scalar/HighestOf.java b/src/main/java/org/cactoos/scalar/HighestOf.java index 09d4b1bc75..81f0604a56 100644 --- a/src/main/java/org/cactoos/scalar/HighestOf.java +++ b/src/main/java/org/cactoos/scalar/HighestOf.java @@ -24,7 +24,6 @@ package org.cactoos.scalar; import org.cactoos.Scalar; -import org.cactoos.func.MaxFunc; import org.cactoos.iterable.IterableOf; /** @@ -66,7 +65,15 @@ public HighestOf(final Scalar... scalars) { */ public HighestOf(final Iterable> iterable) { this.result = new Folded<>( - new MaxFunc<>(), + (first, second) -> { + final T value; + if (first.compareTo(second) > 0) { + value = first; + } else { + value = second; + } + return value; + }, iterable ); } diff --git a/src/main/java/org/cactoos/scalar/LowestOf.java b/src/main/java/org/cactoos/scalar/LowestOf.java index 8c3a89b6ba..befd3d748d 100644 --- a/src/main/java/org/cactoos/scalar/LowestOf.java +++ b/src/main/java/org/cactoos/scalar/LowestOf.java @@ -24,7 +24,6 @@ package org.cactoos.scalar; import org.cactoos.Scalar; -import org.cactoos.func.MinFunc; import org.cactoos.iterable.IterableOf; /** @@ -64,7 +63,15 @@ public LowestOf(final Scalar... scalars) { */ public LowestOf(final Iterable> iterable) { this.result = new Folded<>( - new MinFunc<>(), + (first, second) -> { + final T value; + if (first.compareTo(second) < 0) { + value = first; + } else { + value = second; + } + return value; + }, iterable ); } From e62007c7929470ce44f29de6fcbfe6ec88ab4494 Mon Sep 17 00:00:00 2001 From: Eduard Balovnev Date: Fri, 19 Jan 2018 22:59:01 +0600 Subject: [PATCH 3/7] 460 up for new qulice requirements --- src/main/java/org/cactoos/scalar/Folded.java | 2 +- src/main/java/org/cactoos/scalar/HighestOf.java | 2 +- src/main/java/org/cactoos/scalar/LowestOf.java | 2 +- src/test/java/org/cactoos/scalar/HighestOfTest.java | 2 +- src/test/java/org/cactoos/scalar/LowestOfTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java index 21b18f5830..49446bd509 100644 --- a/src/main/java/org/cactoos/scalar/Folded.java +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -1,7 +1,7 @@ /** * The MIT License (MIT) * - * Copyright (c) 2017 Yegor Bugayenko + * 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 diff --git a/src/main/java/org/cactoos/scalar/HighestOf.java b/src/main/java/org/cactoos/scalar/HighestOf.java index 81f0604a56..31c4644296 100644 --- a/src/main/java/org/cactoos/scalar/HighestOf.java +++ b/src/main/java/org/cactoos/scalar/HighestOf.java @@ -1,7 +1,7 @@ /** * The MIT License (MIT) * - * Copyright (c) 2017 Yegor Bugayenko + * 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 diff --git a/src/main/java/org/cactoos/scalar/LowestOf.java b/src/main/java/org/cactoos/scalar/LowestOf.java index befd3d748d..1e8bb573b9 100644 --- a/src/main/java/org/cactoos/scalar/LowestOf.java +++ b/src/main/java/org/cactoos/scalar/LowestOf.java @@ -1,7 +1,7 @@ /** * The MIT License (MIT) * - * Copyright (c) 2017 Yegor Bugayenko + * 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 diff --git a/src/test/java/org/cactoos/scalar/HighestOfTest.java b/src/test/java/org/cactoos/scalar/HighestOfTest.java index 80a488f6af..5ce45cb8ed 100644 --- a/src/test/java/org/cactoos/scalar/HighestOfTest.java +++ b/src/test/java/org/cactoos/scalar/HighestOfTest.java @@ -1,7 +1,7 @@ /** * The MIT License (MIT) * - * Copyright (c) 2017 Yegor Bugayenko + * 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 diff --git a/src/test/java/org/cactoos/scalar/LowestOfTest.java b/src/test/java/org/cactoos/scalar/LowestOfTest.java index 3fcfedbcf0..d01742a955 100644 --- a/src/test/java/org/cactoos/scalar/LowestOfTest.java +++ b/src/test/java/org/cactoos/scalar/LowestOfTest.java @@ -1,7 +1,7 @@ /** * The MIT License (MIT) * - * Copyright (c) 2017 Yegor Bugayenko + * 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 From ce2851fc8c5d766d3526543d556dc30fd51cc989 Mon Sep 17 00:00:00 2001 From: Eduard Balovnev Date: Sat, 20 Jan 2018 09:21:37 +0600 Subject: [PATCH 4/7] 460 fix a review: Parameter names must be different from attribute names. --- src/main/java/org/cactoos/scalar/Folded.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java index 49446bd509..b13e3ef91d 100644 --- a/src/main/java/org/cactoos/scalar/Folded.java +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -53,16 +53,19 @@ public final class Folded implements Scalar { /** * Folding function. */ - private final BiFunc fold; + private final BiFunc function; /** * Ctor. * @param fold Folding function - * @param items The items + * @param scalars The scalars */ - public Folded(final BiFunc fold, final Iterable> items) { - this.items = items; - this.fold = fold; + public Folded( + final BiFunc fold, + final Iterable> scalars + ) { + this.items = scalars; + this.function = fold; } @Override @@ -76,7 +79,7 @@ public T value() throws Exception { T acc = iter.next().value(); while (iter.hasNext()) { final T next = iter.next().value(); - acc = this.fold.apply(acc, next); + acc = this.function.apply(acc, next); } return acc; } From 5c7337ac5269e1a376333d2a48ab8e79b023dbc9 Mon Sep 17 00:00:00 2001 From: Eduard Balovnev Date: Sat, 20 Jan 2018 10:27:10 +0600 Subject: [PATCH 5/7] 460 fix a review: You should be the @author --- src/main/java/org/cactoos/scalar/HighestOf.java | 1 + src/main/java/org/cactoos/scalar/LowestOf.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/org/cactoos/scalar/HighestOf.java b/src/main/java/org/cactoos/scalar/HighestOf.java index 31c4644296..b2266a51ce 100644 --- a/src/main/java/org/cactoos/scalar/HighestOf.java +++ b/src/main/java/org/cactoos/scalar/HighestOf.java @@ -37,6 +37,7 @@ *

There is no thread-safety guarantee. * * @author Fabricio Cabral (fabriciofx@gmail.com) + * @author Eduard Balovnev (bedward70@mail.ru) * @version $Id$ * @param Scalar type * @see UncheckedScalar diff --git a/src/main/java/org/cactoos/scalar/LowestOf.java b/src/main/java/org/cactoos/scalar/LowestOf.java index 1e8bb573b9..78dd990320 100644 --- a/src/main/java/org/cactoos/scalar/LowestOf.java +++ b/src/main/java/org/cactoos/scalar/LowestOf.java @@ -37,6 +37,7 @@ * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

* * @author Fabricio Cabral (fabriciofx@gmail.com) + * @author Eduard Balovnev (bedward70@mail.ru) * @version $Id$ * @param Scalar type * @since 0.9 From 86156dec198aee0b1697cb26afec58446c2a6115 Mon Sep 17 00:00:00 2001 From: Eduard Balovnev Date: Wed, 24 Jan 2018 19:37:52 +0600 Subject: [PATCH 6/7] 460 Bring back implementation of Folded --- src/main/java/org/cactoos/scalar/Folded.java | 1 + .../java/org/cactoos/scalar/HighestOf.java | 87 -------------- .../java/org/cactoos/scalar/LowestOf.java | 85 -------------- .../java/org/cactoos/scalar/FoldedTest.java | 106 ++++++++++++++++++ .../org/cactoos/scalar/HighestOfTest.java | 94 ---------------- .../java/org/cactoos/scalar/LowestOfTest.java | 94 ---------------- 6 files changed, 107 insertions(+), 360 deletions(-) delete mode 100644 src/main/java/org/cactoos/scalar/HighestOf.java delete mode 100644 src/main/java/org/cactoos/scalar/LowestOf.java create mode 100644 src/test/java/org/cactoos/scalar/FoldedTest.java delete mode 100644 src/test/java/org/cactoos/scalar/HighestOfTest.java delete mode 100644 src/test/java/org/cactoos/scalar/LowestOfTest.java diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java index b13e3ef91d..881c31eca8 100644 --- a/src/main/java/org/cactoos/scalar/Folded.java +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -39,6 +39,7 @@ * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

* * @author Alexander Dyadyushenko (gookven@gmail.com) + * @author Eduard Balovnev (bedward70@mail.ru) * @version $Id$ * @param Scalar type * @since 0.9 diff --git a/src/main/java/org/cactoos/scalar/HighestOf.java b/src/main/java/org/cactoos/scalar/HighestOf.java deleted file mode 100644 index b2266a51ce..0000000000 --- a/src/main/java/org/cactoos/scalar/HighestOf.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * 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.Scalar; -import org.cactoos.iterable.IterableOf; - -/** - * Find the greater among items. - * - *

This class implements {@link Scalar}, which throws a checked - * {@link Exception}. This may not be convenient in many cases. To make - * it more convenient and get rid of the checked exception you can - * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

- * - *

There is no thread-safety guarantee. - * - * @author Fabricio Cabral (fabriciofx@gmail.com) - * @author Eduard Balovnev (bedward70@mail.ru) - * @version $Id$ - * @param Scalar type - * @see UncheckedScalar - * @see IoCheckedScalar - * @since 0.10 - */ -public final class HighestOf> implements Scalar { - - /** - * Items. - */ - private final Scalar result; - - /** - * Ctor. - * @param scalars The items - */ - @SafeVarargs - public HighestOf(final Scalar... scalars) { - this(new IterableOf<>(scalars)); - } - - /** - * Ctor. - * @param iterable The items - */ - public HighestOf(final Iterable> iterable) { - this.result = new Folded<>( - (first, second) -> { - final T value; - if (first.compareTo(second) > 0) { - value = first; - } else { - value = second; - } - return value; - }, - iterable - ); - } - - @Override - public T value() throws Exception { - return this.result.value(); - } - -} diff --git a/src/main/java/org/cactoos/scalar/LowestOf.java b/src/main/java/org/cactoos/scalar/LowestOf.java deleted file mode 100644 index 78dd990320..0000000000 --- a/src/main/java/org/cactoos/scalar/LowestOf.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * 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.Scalar; -import org.cactoos.iterable.IterableOf; - -/** - * Find the smaller among items. - * - *

There is no thread-safety guarantee. - * - *

This class implements {@link Scalar}, which throws a checked - * {@link Exception}. This may not be convenient in many cases. To make - * it more convenient and get rid of the checked exception you can - * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

- * - * @author Fabricio Cabral (fabriciofx@gmail.com) - * @author Eduard Balovnev (bedward70@mail.ru) - * @version $Id$ - * @param Scalar type - * @since 0.9 - */ -public final class LowestOf> implements Scalar { - - /** - * Items. - */ - private final Scalar result; - - /** - * Ctor. - * @param scalars The items - */ - @SafeVarargs - public LowestOf(final Scalar... scalars) { - this(new IterableOf<>(scalars)); - } - - /** - * Ctor. - * @param iterable The items - */ - public LowestOf(final Iterable> iterable) { - this.result = new Folded<>( - (first, second) -> { - final T value; - if (first.compareTo(second) < 0) { - value = first; - } else { - value = second; - } - return value; - }, - iterable - ); - } - - @Override - public T value() throws Exception { - return this.result.value(); - } - -} diff --git a/src/test/java/org/cactoos/scalar/FoldedTest.java b/src/test/java/org/cactoos/scalar/FoldedTest.java new file mode 100644 index 0000000000..96d7e098b1 --- /dev/null +++ b/src/test/java/org/cactoos/scalar/FoldedTest.java @@ -0,0 +1,106 @@ +/** + * 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.util.Collections; +import java.util.NoSuchElementException; +import org.cactoos.Scalar; +import org.cactoos.iterable.IterableOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Folded}. + * + * @author Eduard Balovnev (bedward70@mail.ru) + * @version $Id$ + * @since 0.10 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class FoldedTest { + + @Test(expected = NoSuchElementException.class) + public void emptyListTest() throws Exception { + new Folded<>( + (first, last) -> first, + Collections.emptyList() + ) + .value(); + } + + @Test + public void singleTest() throws Exception { + final Integer single = 10; + MatcherAssert.assertThat( + "Can't find the single", + new Folded<>( + (first, last) -> first, + new IterableOf>((Scalar) () -> single) + ) + .value(), + Matchers.equalTo(single) + ); + } + + @Test + public void firstTest() throws Exception { + final String one = "Apple"; + final String two = "Banana"; + final String three = "Orange"; + MatcherAssert.assertThat( + "Can't find the first", + new Folded<>( + (first, last) -> first, + new IterableOf>( + (Scalar) () -> one, + (Scalar) () -> two, + (Scalar) () -> three + ) + ) + .value(), + Matchers.equalTo(one) + ); + } + + @Test + public void lastTest() throws Exception { + final Character one = 'A'; + final Character two = 'B'; + final Character three = 'O'; + MatcherAssert.assertThat( + "Can't find the last", + new Folded<>( + (first, last) -> last, + new IterableOf>( + (Scalar) () -> one, + (Scalar) () -> two, + (Scalar) () -> three + ) + ) + .value(), + Matchers.equalTo(three) + ); + } +} diff --git a/src/test/java/org/cactoos/scalar/HighestOfTest.java b/src/test/java/org/cactoos/scalar/HighestOfTest.java deleted file mode 100644 index 5ce45cb8ed..0000000000 --- a/src/test/java/org/cactoos/scalar/HighestOfTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * 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.util.Collections; -import java.util.NoSuchElementException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link HighestOf}. - * - * @author Fabricio Cabral (fabriciofx@gmail.com) - * @author Eduard Balovnev (bedward70@mail.ru) - * @version $Id$ - * @since 0.10 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class HighestOfTest { - - @Test(expected = NoSuchElementException.class) - public void maxAmongEmptyTest() throws Exception { - new HighestOf<>(() -> Collections.emptyIterator()).value(); - } - - @Test - public void maxAmongOneTest() throws Exception { - final int num = 10; - MatcherAssert.assertThat( - "Can't find the greater among one", - new HighestOf(() -> new Integer(num)).value(), - Matchers.equalTo(num) - ); - } - - @Test - public void maxAmongManyTest() throws Exception { - final int num = 10; - MatcherAssert.assertThat( - "Can't find the greater among many", - new HighestOf( - () -> new Integer(num), - () -> new Integer(0), - () -> new Integer(-1), - () -> new Integer(2) - ).value(), - Matchers.equalTo(num) - ); - } - - @Test - public void highestStringTest() throws Exception { - final String lowest = "Apple"; - final String highest = "Orange"; - MatcherAssert.assertThat( - "Can't find the highest string among many", - new HighestOf(() -> lowest, () -> highest).value(), - Matchers.equalTo(highest) - ); - } - - @Test - public void highestCharTest() throws Exception { - final char lowest = 'A'; - final char highest = 'B'; - MatcherAssert.assertThat( - "Can't find the highest char among many", - new HighestOf(() -> lowest, () -> highest).value(), - Matchers.equalTo(highest) - ); - } -} diff --git a/src/test/java/org/cactoos/scalar/LowestOfTest.java b/src/test/java/org/cactoos/scalar/LowestOfTest.java deleted file mode 100644 index d01742a955..0000000000 --- a/src/test/java/org/cactoos/scalar/LowestOfTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * 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.util.Collections; -import java.util.NoSuchElementException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link LowestOf}. - * - * @author Fabricio Cabral (fabriciofx@gmail.com) - * @author Eduard Balovnev (bedward70@mail.ru) - * @version $Id$ - * @since 0.10 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class LowestOfTest { - - @Test(expected = NoSuchElementException.class) - public void minAmongEmptyTest() throws Exception { - new LowestOf<>(() -> Collections.emptyIterator()).value(); - } - - @Test - public void minAmongOneTest() throws Exception { - final int num = 10; - MatcherAssert.assertThat( - "Can't find the smaller among one", - new LowestOf(() -> new Integer(num)).value(), - Matchers.equalTo(num) - ); - } - - @Test - public void minAmongManyTest() throws Exception { - final int num = -1; - MatcherAssert.assertThat( - "Can't find the smaller among many", - new LowestOf( - () -> new Integer(1), - () -> new Integer(0), - () -> new Integer(num), - () -> new Integer(2) - ).value(), - Matchers.equalTo(num) - ); - } - - @Test - public void lowestStringTest() throws Exception { - final String lowest = "Apple"; - final String highest = "Orange"; - MatcherAssert.assertThat( - "Can't find the lowest string among many", - new LowestOf(() -> lowest, () -> highest).value(), - Matchers.equalTo(lowest) - ); - } - - @Test - public void lowestCharTest() throws Exception { - final char lowest = 'A'; - final char highest = 'B'; - MatcherAssert.assertThat( - "Can't find the lowest char among many", - new LowestOf(() -> lowest, () -> highest).value(), - Matchers.equalTo(lowest) - ); - } -} From 3dd28a3a074095666eedf16920fb89d0a1112615 Mon Sep 17 00:00:00 2001 From: Eduard Balovnev Date: Fri, 26 Jan 2018 21:51:01 +0600 Subject: [PATCH 7/7] 460 fixing of findings --- .../java/org/cactoos/scalar/FoldedTest.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/cactoos/scalar/FoldedTest.java b/src/test/java/org/cactoos/scalar/FoldedTest.java index 96d7e098b1..0796e8efc0 100644 --- a/src/test/java/org/cactoos/scalar/FoldedTest.java +++ b/src/test/java/org/cactoos/scalar/FoldedTest.java @@ -36,13 +36,13 @@ * * @author Eduard Balovnev (bedward70@mail.ru) * @version $Id$ - * @since 0.10 + * @since 1.0 * @checkstyle JavadocMethodCheck (500 lines) */ public final class FoldedTest { @Test(expected = NoSuchElementException.class) - public void emptyListTest() throws Exception { + public void failsForEmptyIterable() throws Exception { new Folded<>( (first, last) -> first, Collections.emptyList() @@ -51,21 +51,20 @@ public void emptyListTest() throws Exception { } @Test - public void singleTest() throws Exception { + public void singleAtSingleIterable() throws Exception { final Integer single = 10; MatcherAssert.assertThat( "Can't find the single", new Folded<>( (first, last) -> first, - new IterableOf>((Scalar) () -> single) - ) - .value(), + new IterableOf>(() -> single) + ).value(), Matchers.equalTo(single) ); } @Test - public void firstTest() throws Exception { + public void firstAtIterable() throws Exception { final String one = "Apple"; final String two = "Banana"; final String three = "Orange"; @@ -74,18 +73,17 @@ public void firstTest() throws Exception { new Folded<>( (first, last) -> first, new IterableOf>( - (Scalar) () -> one, - (Scalar) () -> two, - (Scalar) () -> three + () -> one, + () -> two, + () -> three ) - ) - .value(), + ).value(), Matchers.equalTo(one) ); } @Test - public void lastTest() throws Exception { + public void lastAtIterable() throws Exception { final Character one = 'A'; final Character two = 'B'; final Character three = 'O'; @@ -94,12 +92,11 @@ public void lastTest() throws Exception { new Folded<>( (first, last) -> last, new IterableOf>( - (Scalar) () -> one, - (Scalar) () -> two, - (Scalar) () -> three + () -> one, + () -> two, + () -> three ) - ) - .value(), + ).value(), Matchers.equalTo(three) ); }