Skip to content

Commit

Permalink
string as number
Browse files Browse the repository at this point in the history
  • Loading branch information
g4s8 committed May 31, 2017
1 parent a2170a8 commit 9ab650e
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) [email protected]

## License (MIT)

Copyright (c) 2017 Yegor Bugayenko
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/cactoos/text/StringAsNumber.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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);
}
}
86 changes: 86 additions & 0 deletions src/test/java/org/cactoos/text/StringAsNumberTest.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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)
);
}
}

0 comments on commit 9ab650e

Please sign in to comment.