From 523921afc92474e5daec1532cc8ecb17f88fc5d5 Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Tue, 6 Nov 2012 11:29:54 -0700 Subject: Created parseBigInt method. --- .../scala/org/hashtree/stringmetric/cli/ParseUtility.scala | 4 +++- .../org/hashtree/stringmetric/cli/ParseUtilitySpec.scala | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'cli/source') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala index f2975d9..092db3e 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala @@ -1,11 +1,13 @@ package org.hashtree.stringmetric.cli -import scala.math.BigDecimal +import scala.math.{ BigDecimal, BigInt } /** Utility standalone for parse based operations. */ object ParseUtility { def parseBigDecimal(string: String): Option[BigDecimal] = try { Some(BigDecimal(string)) } catch { case _ => None } + def parseBigInt(string: String): Option[BigInt] = try { Some(BigInt(string)) } catch { case _ => None } + def parseDouble(string: String): Option[Double] = try { Some(string.toDouble) } catch { case _ => None } def parseFloat(string: String): Option[Float] = try { Some(string.toFloat) } catch { case _ => None } diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala index f265c54..a7b4adc 100755 --- a/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala @@ -3,7 +3,7 @@ package org.hashtree.stringmetric.cli import org.hashtree.stringmetric.ScalaTest import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner -import scala.math.BigDecimal +import scala.math.{ BigDecimal, BigInt } @RunWith(classOf[JUnitRunner]) final class ParseUtilitySpec extends ScalaTest { @@ -20,6 +20,18 @@ final class ParseUtilitySpec extends ScalaTest { } } } + "parseBigInt method" when passed { + "invalid argument" should returns { + "None" in { + ParseUtility.parseBigInt("one").isDefined should be (false) + } + } + "valid argument" should returns { + "Some(BigInt)" in { + ParseUtility.parseBigInt("1").get should equal (1: BigInt) + } + } + } "parseDouble method" when passed { "invalid argument" should returns { "None" in { -- cgit v1.2.3