From 9ab650e2a396be20d521029f0ef244eee47455df Mon Sep 17 00:00:00 2001 From: g4s8 Date: Wed, 31 May 2017 21:32:31 +0400 Subject: [PATCH] string as number --- README.md | 4 + .../java/org/cactoos/text/StringAsNumber.java | 71 +++++++++++++++ .../org/cactoos/text/StringAsNumberTest.java | 86 +++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 src/main/java/org/cactoos/text/StringAsNumber.java create mode 100644 src/test/java/org/cactoos/text/StringAsNumberTest.java diff --git a/README.md b/README.md index fdf5f78798..0641777c16 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,10 @@ mvn clean install -Pqulice Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static code analyze tool with [checks list](http://checkstyle.sourceforge.net/checks.html) in GitHub precommits. +## Contributors + + - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com + ## License (MIT) Copyright (c) 2017 Yegor Bugayenko diff --git a/src/main/java/org/cactoos/text/StringAsNumber.java b/src/main/java/org/cactoos/text/StringAsNumber.java new file mode 100644 index 0000000000..69461bc017 --- /dev/null +++ b/src/main/java/org/cactoos/text/StringAsNumber.java @@ -0,0 +1,71 @@ +/** + * 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.text; + +/** + * String as {@link Number}. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class StringAsNumber extends Number { + + private static final long serialVersionUID = 27917942951259451L; + + /** + * Source string. + */ + private final String string; + + /** + * Ctor. + * + * @param string Source string + */ + public StringAsNumber(final String string) { + super(); + this.string = string; + } + + @Override + public int intValue() { + return Integer.valueOf(this.string); + } + + @Override + public long longValue() { + return Long.valueOf(this.string); + } + + @Override + public float floatValue() { + return Float.valueOf(this.string); + } + + @Override + public double doubleValue() { + return Double.valueOf(this.string); + } +} diff --git a/src/test/java/org/cactoos/text/StringAsNumberTest.java b/src/test/java/org/cactoos/text/StringAsNumberTest.java new file mode 100644 index 0000000000..d7bf403a9a --- /dev/null +++ b/src/test/java/org/cactoos/text/StringAsNumberTest.java @@ -0,0 +1,86 @@ +/** + * 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.text; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StringAsNumber}. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class StringAsNumberTest { + + /** + * Test {@link Integer}. + */ + @Test + public void intTest() { + MatcherAssert.assertThat( + new StringAsNumber("-198815458").intValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(-198815458) + ); + } + + /** + * Test {@link Long}. + */ + @Test + public void longTest() { + MatcherAssert.assertThat( + new StringAsNumber("9145723601264654").longValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(9145723601264654L) + ); + } + + /** + * Test {@link Float}. + */ + @Test + public strictfp void floatTest() { + MatcherAssert.assertThat( + new StringAsNumber("765923.24").floatValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(765923.24F) + ); + } + + /** + * Test {@link Double}. + */ + @Test + public strictfp void doubleTest() { + MatcherAssert.assertThat( + new StringAsNumber("0.25323464151").doubleValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(0.25323464151) + ); + } +}