From f806833aefb978def4213063de7f714ff5c488b5 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Nov 2017 11:16:38 +0200 Subject: [PATCH] #459 NumberOf --- .../java/org/cactoos/scalar/DoubleOf.java | 74 ----------------- src/main/java/org/cactoos/scalar/IntOf.java | 69 ---------------- src/main/java/org/cactoos/scalar/LongOf.java | 69 ---------------- .../scalar/{FloatOf.java => NumberOf.java} | 81 ++++++++++++++++--- .../java/org/cactoos/scalar/FloatOfTest.java | 55 ------------- .../java/org/cactoos/scalar/IntOfTest.java | 55 ------------- .../java/org/cactoos/scalar/LongOfTest.java | 55 ------------- .../{DoubleOfTest.java => NumberOfTest.java} | 57 +++++++++++-- 8 files changed, 121 insertions(+), 394 deletions(-) delete mode 100644 src/main/java/org/cactoos/scalar/DoubleOf.java delete mode 100644 src/main/java/org/cactoos/scalar/IntOf.java delete mode 100644 src/main/java/org/cactoos/scalar/LongOf.java rename src/main/java/org/cactoos/scalar/{FloatOf.java => NumberOf.java} (51%) delete mode 100644 src/test/java/org/cactoos/scalar/FloatOfTest.java delete mode 100644 src/test/java/org/cactoos/scalar/IntOfTest.java delete mode 100644 src/test/java/org/cactoos/scalar/LongOfTest.java rename src/test/java/org/cactoos/scalar/{DoubleOfTest.java => NumberOfTest.java} (51%) diff --git a/src/main/java/org/cactoos/scalar/DoubleOf.java b/src/main/java/org/cactoos/scalar/DoubleOf.java deleted file mode 100644 index a90fcd4325..0000000000 --- a/src/main/java/org/cactoos/scalar/DoubleOf.java +++ /dev/null @@ -1,74 +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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Double}. - * - *

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 Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class DoubleOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public DoubleOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public DoubleOf(final Text text) { - this.origin = text; - } - - @Override - public Double value() throws IOException { - return Double.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/IntOf.java b/src/main/java/org/cactoos/scalar/IntOf.java deleted file mode 100644 index 0264159b88..0000000000 --- a/src/main/java/org/cactoos/scalar/IntOf.java +++ /dev/null @@ -1,69 +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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Integer}. - * - *

There is no thread-safety guarantee. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class IntOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public IntOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public IntOf(final Text text) { - this.origin = text; - } - - @Override - public Integer value() throws IOException { - return Integer.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/LongOf.java b/src/main/java/org/cactoos/scalar/LongOf.java deleted file mode 100644 index bd071743de..0000000000 --- a/src/main/java/org/cactoos/scalar/LongOf.java +++ /dev/null @@ -1,69 +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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Long}. - * - *

There is no thread-safety guarantee. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class LongOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public LongOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public LongOf(final Text text) { - this.origin = text; - } - - @Override - public Long value() throws IOException { - return Long.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/FloatOf.java b/src/main/java/org/cactoos/scalar/NumberOf.java similarity index 51% rename from src/main/java/org/cactoos/scalar/FloatOf.java rename to src/main/java/org/cactoos/scalar/NumberOf.java index 64b9685fbd..18866b0597 100644 --- a/src/main/java/org/cactoos/scalar/FloatOf.java +++ b/src/main/java/org/cactoos/scalar/NumberOf.java @@ -23,7 +23,6 @@ */ package org.cactoos.scalar; -import java.io.IOException; import org.cactoos.Scalar; import org.cactoos.Text; import org.cactoos.text.TextOf; @@ -42,20 +41,40 @@ * @version $Id$ * @since 0.2 */ -public final class FloatOf implements Scalar { +public final class NumberOf extends Number implements Scalar { /** - * Source text. + * Serialization marker. */ - private final Text origin; + private static final long serialVersionUID = -1924406337256921883L; + + /** + * The LONG number. + */ + private final Scalar lnum; + + /** + * The INT number. + */ + private final Scalar inum; + + /** + * The FLOAT number. + */ + private final Scalar fnum; + + /** + * The DOUBLE number. + */ + private final Scalar dnum; /** * Ctor. * - * @param string Number-string + * @param txt Number-string */ - public FloatOf(final String string) { - this(new TextOf(string)); + public NumberOf(final String txt) { + this(new TextOf(txt)); } /** @@ -63,12 +82,52 @@ public FloatOf(final String string) { * * @param text Number-text */ - public FloatOf(final Text text) { - this.origin = text; + public NumberOf(final Text text) { + super(); + this.lnum = new SyncScalar<>( + new StickyScalar<>( + () -> Long.parseLong(text.asString()) + ) + ); + this.inum = new SyncScalar<>( + new StickyScalar<>( + () -> Integer.parseInt(text.asString()) + ) + ); + this.fnum = new SyncScalar<>( + new StickyScalar<>( + () -> Float.parseFloat(text.asString()) + ) + ); + this.dnum = new SyncScalar<>( + new StickyScalar<>( + () -> Double.parseDouble(text.asString()) + ) + ); + } + + @Override + public Number value() { + return this; + } + + @Override + public int intValue() { + return new UncheckedScalar<>(this.inum).value(); + } + + @Override + public long longValue() { + return new UncheckedScalar<>(this.lnum).value(); + } + + @Override + public float floatValue() { + return new UncheckedScalar<>(this.fnum).value(); } @Override - public Float value() throws IOException { - return Float.valueOf(this.origin.asString()); + public double doubleValue() { + return new UncheckedScalar<>(this.dnum).value(); } } diff --git a/src/test/java/org/cactoos/scalar/FloatOfTest.java b/src/test/java/org/cactoos/scalar/FloatOfTest.java deleted file mode 100644 index a3f381bfe7..0000000000 --- a/src/test/java/org/cactoos/scalar/FloatOfTest.java +++ /dev/null @@ -1,55 +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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link FloatOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class FloatOfTest { - - @Test - public strictfp void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse float number", - new FloatOf("1656.894").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(1656.894F) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentAFloat() throws IOException { - new FloatOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/IntOfTest.java b/src/test/java/org/cactoos/scalar/IntOfTest.java deleted file mode 100644 index ebda11991a..0000000000 --- a/src/test/java/org/cactoos/scalar/IntOfTest.java +++ /dev/null @@ -1,55 +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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link IntOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class IntOfTest { - - @Test - public void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse integer number", - new IntOf("1867892354").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(1867892354) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentAnInt() throws IOException { - new DoubleOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/LongOfTest.java b/src/test/java/org/cactoos/scalar/LongOfTest.java deleted file mode 100644 index a6b2497fb8..0000000000 --- a/src/test/java/org/cactoos/scalar/LongOfTest.java +++ /dev/null @@ -1,55 +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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link LongOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class LongOfTest { - - @Test - public void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse long number", - new LongOf("186789235425346").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(186789235425346L) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentALong() throws IOException { - new LongOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/DoubleOfTest.java b/src/test/java/org/cactoos/scalar/NumberOfTest.java similarity index 51% rename from src/test/java/org/cactoos/scalar/DoubleOfTest.java rename to src/test/java/org/cactoos/scalar/NumberOfTest.java index 74351fa5e1..e7dc8cb5f9 100644 --- a/src/test/java/org/cactoos/scalar/DoubleOfTest.java +++ b/src/test/java/org/cactoos/scalar/NumberOfTest.java @@ -29,27 +29,72 @@ import org.junit.Test; /** - * Test case for {@link DoubleOf}. + * Test case for {@link NumberOf}. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class DoubleOfTest { +public final class NumberOfTest { @Test - public strictfp void numberTest() throws IOException { + public void parsesFloat() throws IOException { + MatcherAssert.assertThat( + "Can't parse float number", + new NumberOf("1656.894").floatValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(1656.894F) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentAFloat() throws IOException { + new NumberOf("abcfds").floatValue(); + } + + @Test + public void parsesLong() throws IOException { + MatcherAssert.assertThat( + "Can't parse long number", + new NumberOf("186789235425346").longValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(186789235425346L) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentALong() throws IOException { + new NumberOf("abcddd").longValue(); + } + + @Test + public void parsesInteger() throws IOException { + MatcherAssert.assertThat( + "Can't parse integer number", + new NumberOf("1867892354").intValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(1867892354) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentAnInt() throws IOException { + new NumberOf("abc fdsf").intValue(); + } + + @Test + public void parsesDouble() throws IOException { MatcherAssert.assertThat( "Can't parse double number", - new DoubleOf("185.65156465123").value(), + new NumberOf("185.65156465123").doubleValue(), // @checkstyle MagicNumber (1 line) Matchers.equalTo(185.65156465123) ); } - @Test(expected = NumberFormatException.class) + @Test(expected = RuntimeException.class) public void failsIfTextDoesNotRepresentADouble() throws IOException { - new DoubleOf("abc").value(); + new NumberOf("abfdsc").doubleValue(); } }