From d68098d74f1235132f7fc4cf69e615f24030dc81 Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Fri, 30 Nov 2012 20:22:00 -0700 Subject: Refactored out utilities more a more elegant solution. --- .../org/hashtree/stringmetric/cli/OptionMap.scala | 40 ++++++ .../hashtree/stringmetric/cli/OptionMapType.scala | 69 ++++++++++ .../stringmetric/cli/OptionMapUtility.scala | 43 ------ .../hashtree/stringmetric/cli/ParseUtility.scala | 20 --- .../cli/phonetic/metaphoneAlgorithm.scala | 2 +- .../cli/phonetic/metaphoneMetric.scala | 2 +- .../cli/phonetic/nysiisAlgorithm.scala | 2 +- .../stringmetric/cli/phonetic/nysiisMetric.scala | 2 +- .../cli/phonetic/refinedNysiisAlgorithm.scala | 2 +- .../cli/phonetic/refinedNysiisMetric.scala | 2 +- .../cli/phonetic/refinedSoundexAlgorithm.scala | 2 +- .../cli/phonetic/refinedSoundexMetric.scala | 2 +- .../cli/phonetic/soundexAlgorithm.scala | 2 +- .../stringmetric/cli/phonetic/soundexMetric.scala | 2 +- .../cli/similarity/diceSorensenMetric.scala | 8 +- .../cli/similarity/hammingMetric.scala | 2 +- .../stringmetric/cli/similarity/jaroMetric.scala | 2 +- .../cli/similarity/jaroWinklerMetric.scala | 2 +- .../cli/similarity/levenshteinMetric.scala | 2 +- .../cli/similarity/nGramAlgorithm.scala | 8 +- .../stringmetric/cli/similarity/nGramMetric.scala | 8 +- .../cli/similarity/weightedLevenshteinMetric.scala | 16 +-- .../hashtree/stringmetric/cli/OptionMapSpec.scala | 84 ++++++++++++ .../stringmetric/cli/OptionMapTypeSpec.scala | 149 ++++++++++++++++++++ .../stringmetric/cli/OptionMapUtilitySpec.scala | 151 --------------------- .../stringmetric/cli/ParseUtilitySpec.scala | 96 ------------- 26 files changed, 376 insertions(+), 344 deletions(-) create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMap.scala create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapType.scala delete mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapUtility.scala delete mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapSpec.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapTypeSpec.scala delete mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapUtilitySpec.scala delete mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala (limited to 'cli') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMap.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMap.scala new file mode 100755 index 0000000..eba447f --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMap.scala @@ -0,0 +1,40 @@ +package org.hashtree.stringmetric.cli + +import scala.annotation.tailrec + +object OptionMap { + def apply(args: Array[String]): OptionMap = { + @tailrec + def next(om: OptionMap, a: List[String]): OptionMap = { + val double = """^(--[a-zA-Z0-9]+)(\=[a-zA-Z0-9\.\-\_]+)?""".r + val single = """^(-[a-zA-Z0-9]+)(\=[a-zA-Z0-9\.\-\_]+)?""".r + val less = """([a-zA-Z0-9\/\-\_\$\.]+)""".r + + a match { + // Empty, return. + case Nil => om + // Double dash options, without value. + case double(k, null) :: t => next(om + (Symbol(k.tail.tail) -> ""), t) + // Double dash options, with value. + case double(k, v) :: t => next(om + (Symbol(k.tail.tail) -> v.tail), t) + // Single dash options, without value. + case single(k, null) :: t => next(om + (Symbol(k.tail) -> ""), t) + // Single dash options, with value. Value is discarded. + case single(k, v) :: t => next(om + (Symbol(k.tail) -> ""), t) + // Dashless options. + case less(v) :: t if v.head != '-' => + if (om.contains('dashless)) { + val dashless = om('dashless) + " " + v.trim + + next((om - 'dashless) + ('dashless -> dashless), t) + } else next(om + ('dashless -> v.trim), t) + // Invalid option, ignore. + case _ :: t => next(om, t) + } + } + + next(Map.empty[Symbol, String], args.toList) + } + + def apply(strings: String*): OptionMap = apply(strings.toArray) +} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapType.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapType.scala new file mode 100755 index 0000000..43f1210 --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapType.scala @@ -0,0 +1,69 @@ +package org.hashtree.stringmetric.cli + +sealed abstract class OptionMapType[T](protected[this] val stringSelf: String) { + def get(): Option[T] +} + +final case class OptionMapBigDecimal(bigDecimalString: String) extends OptionMapType[BigDecimal](bigDecimalString) { + private[this] lazy val self = try { Some(BigDecimal(stringSelf)) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapBigInt(bigIntString: String) extends OptionMapType[BigInt](bigIntString) { + private[this] lazy val self = try { Some(BigInt(stringSelf)) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapDouble(doubleString: String) extends OptionMapType[Double](doubleString) { + private[this] lazy val self = try { Some(stringSelf.toDouble) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapFloat(floatString: String) extends OptionMapType[Float](floatString) { + private[this] lazy val self = try { Some(stringSelf.toFloat) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapInt(intString: String) extends OptionMapType[Int](intString) { + private[this] lazy val self = try { Some(stringSelf.toInt) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapLong(longString: String) extends OptionMapType[Long](longString) { + private[this] lazy val self = try { Some(stringSelf.toLong) } catch { case _ => None } + + override def get() = self +} + +final case class OptionMapShort(shortString: String) extends OptionMapType[Short](shortString) { + private[this] lazy val self = try { Some(stringSelf.toShort) } catch { case _ => None } + + override def get() = self +} + +object OptionMapType { + implicit def OptionMapTypeToString[T](optionMapType: OptionMapType[T]): String = optionMapType.get.map(_.toString).get + + implicit def OptionMapTypeToOptionT[T](optionMapType: OptionMapType[T]): Option[T] = optionMapType.get + + implicit def OptionMapTypeToT[T](optionMapType: OptionMapType[T]): T = optionMapType.get.get + + implicit def StringToOptionMapBigDecimal(string: String): OptionMapBigDecimal = new OptionMapBigDecimal(string) + + implicit def StringToOptionMapBigInt(string: String): OptionMapBigInt = new OptionMapBigInt(string) + + implicit def StringToOptionMapDouble(string: String): OptionMapDouble = new OptionMapDouble(string) + + implicit def StringToOptionMapFloat(string: String): OptionMapFloat = new OptionMapFloat(string) + + implicit def StringToOptionMapInt(string: String): OptionMapInt = new OptionMapInt(string) + + implicit def StringToOptionMapLong(string: String): OptionMapLong = new OptionMapLong(string) + + implicit def StringToOptionMapShort(string: String): OptionMapShort = new OptionMapShort(string) +} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapUtility.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapUtility.scala deleted file mode 100755 index 4b5d5fa..0000000 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/OptionMapUtility.scala +++ /dev/null @@ -1,43 +0,0 @@ -package org.hashtree.stringmetric.cli - -import scala.annotation.tailrec - -/** Utility standalone for OptionMap based operations. */ -object OptionMapUtility { - def toOptionMap(arguments: Array[String]): OptionMap = toOptionMap(arguments.toList) - - def toOptionMap(arguments: List[String]): OptionMap = next(Map.empty[Symbol, String], arguments) - - @tailrec - private[this] def next(optionMap: OptionMap, arguments: List[String]): OptionMap = { - val double = """^(--[a-zA-Z0-9]+)(\=[a-zA-Z0-9\.\-\_]+)?""".r - val single = """^(-[a-zA-Z0-9]+)(\=[a-zA-Z0-9\.\-\_]+)?""".r - val less = """([a-zA-Z0-9\/\-\_\$\.]+)""".r - - arguments match { - // Empty List, return OptionMap. - case Nil => optionMap - // Double dash options, without value. - case double(name, null) :: tail => - next(optionMap + (Symbol(name.tail.tail) -> ""), tail) - // Double dash options, with value. - case double(name, value) :: tail => - next(optionMap + (Symbol(name.tail.tail) -> value.tail), tail) - // Single dash options, without value. - case single(name, null) :: tail => - next(optionMap + (Symbol(name.tail) -> ""), tail) - // Single dash options, with value. Value is discarded. - case single(name, value) :: tail => - next(optionMap + (Symbol(name.tail) -> ""), tail) - // Dashless options. - case less(value) :: tail if value.head != '-' => - if (optionMap.contains('dashless)) { - val dashless = optionMap('dashless) + " " + value.trim - - next((optionMap - 'dashless) + ('dashless -> dashless), tail) - } else next(optionMap + ('dashless -> value.trim), tail) - // Invalid option, ignore. - case _ :: tail => next(optionMap, tail) - } - } -} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala deleted file mode 100755 index 092db3e..0000000 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/ParseUtility.scala +++ /dev/null @@ -1,20 +0,0 @@ -package org.hashtree.stringmetric.cli - -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 } - - def parseInt(string: String): Option[Int] = try { Some(string.toInt) } catch { case _ => None } - - def parseLong(string: String): Option[Long] = try { Some(string.toLong) } catch { case _ => None } - - def parseShort(string: String): Option[Short] = try { Some(string.toShort) } catch { case _ => None } -} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneAlgorithm.scala index c318a55..aa8fa25 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneAlgorithm.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.MetaphoneAlgorithm */ object metaphoneAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneMetric.scala index 22227dc..c4d31c3 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/metaphoneMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.MetaphoneMetric */ object metaphoneMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisAlgorithm.scala index 994ee1d..940a3c5 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisAlgorithm.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.NysiisAlgorithm */ object nysiisAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisMetric.scala index 2dca78e..ea0180c 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/nysiisMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.NysiisMetric */ object nysiisMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala index 156fa07..947fe14 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.RefinedNysiisAlgorithm */ object refinedNysiisAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala index 12e0ecd..01dfeaf 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.RefinedNysiisMetric */ object refinedNysiisMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala index 7071175..b088158 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.RefinedSoundexAlgorithm */ object refinedSoundexAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexMetric.scala index 950367e..35344cf 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedSoundexMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.RefinedSoundexMetric */ object refinedSoundexMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexAlgorithm.scala index 63ae5cb..d970b6c 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexAlgorithm.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.SoundexAlgorithm */ object soundexAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexMetric.scala index 4353be1..4ccc8be 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/soundexMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.phonetic.SoundexMetric */ object soundexMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/diceSorensenMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/diceSorensenMetric.scala index 395136b..2c88022 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/diceSorensenMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/diceSorensenMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.similarity.DiceSorensenMetric */ object diceSorensenMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. @@ -17,8 +17,8 @@ object diceSorensenMetric extends Command { help() exit(options) // Execute. - } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 && - options.contains('n) && ParseUtility.parseInt(options('n)).isDefined + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 + && options.contains('n) && OptionMapInt(options('n)).isDefined ) { execute(options) exit(options) @@ -47,7 +47,7 @@ object diceSorensenMetric extends Command { override def execute(options: OptionMap): Unit = { val strings = options('dashless).split(" ") - val n = ParseUtility.parseInt(options('n)).get + val n = OptionMapInt(options('n)) println( DiceSorensenMetric.compare( diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/hammingMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/hammingMetric.scala index 4b12009..6cf7b33 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/hammingMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/hammingMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.similarity.HammingMetric */ object hammingMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroMetric.scala index 3a81050..9d54257 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroMetric.scala @@ -6,7 +6,7 @@ import org.hashtree.stringmetric.similarity.JaroMetric /** The jaroMetric [[org.hashtree.stringmetric.cli.Command]]. Compares two strings to calculate the Jaro distance. */ object jaroMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroWinklerMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroWinklerMetric.scala index f49596c..1822e39 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroWinklerMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/jaroWinklerMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.similarity.JaroWinklerMetric */ object jaroWinklerMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/levenshteinMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/levenshteinMetric.scala index fde64fb..0aecf0f 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/levenshteinMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/levenshteinMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.similarity.LevenshteinMetric */ object levenshteinMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala index 82da50f..8dffafe 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramAlgorithm.scala @@ -8,7 +8,7 @@ import org.hashtree.stringmetric.similarity.NGramAlgorithm */ object nGramAlgorithm extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. @@ -16,8 +16,8 @@ object nGramAlgorithm extends Command { help() exit(options) // Execute. - } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 0 && - options.contains('n) && ParseUtility.parseInt(options('n)).isDefined + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 0 + && options.contains('n) && OptionMapInt(options('n)).isDefined ) { execute(options) exit(options) @@ -45,7 +45,7 @@ object nGramAlgorithm extends Command { } override def execute(options: OptionMap): Unit = { - val n = ParseUtility.parseInt(options('n)).get + val n = OptionMapInt(options('n)) println( NGramAlgorithm.compute(options('dashless))(n).map(_.mkString("|")).getOrElse("not computable") diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala index 07e4db6..d28a432 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/nGramMetric.scala @@ -9,7 +9,7 @@ import org.hashtree.stringmetric.similarity.NGramMetric */ object nGramMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. @@ -17,8 +17,8 @@ object nGramMetric extends Command { help() exit(options) // Execute. - } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 && - options.contains('n) && ParseUtility.parseInt(options('n)).isDefined + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 + && options.contains('n) && OptionMapInt(options('n)).isDefined ) { execute(options) exit(options) @@ -47,7 +47,7 @@ object nGramMetric extends Command { override def execute(options: OptionMap): Unit = { val strings = options('dashless).split(" ") - val n = ParseUtility.parseInt(options('n)).get + val n = OptionMapInt(options('n)) println( NGramMetric.compare( diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/weightedLevenshteinMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/weightedLevenshteinMetric.scala index f6e4771..d785c98 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/weightedLevenshteinMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/similarity/weightedLevenshteinMetric.scala @@ -11,7 +11,7 @@ import scala.math.BigDecimal */ object weightedLevenshteinMetric extends Command { override def main(args: Array[String]): Unit = { - val options = OptionMapUtility.toOptionMap(args) + val options = OptionMap(args) try { // Help. @@ -19,10 +19,10 @@ object weightedLevenshteinMetric extends Command { help() exit(options) // Execute. - } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 && - options.contains('deleteWeight) && ParseUtility.parseDouble(options('deleteWeight)).isDefined && - options.contains('insertWeight) && ParseUtility.parseDouble(options('insertWeight)).isDefined && - options.contains('substituteWeight) && ParseUtility.parseDouble(options('substituteWeight)).isDefined + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1 + && options.contains('deleteWeight) && OptionMapDouble(options('deleteWeight)).isDefined + && options.contains('insertWeight) && OptionMapDouble(options('insertWeight)).isDefined + && options.contains('substituteWeight) && OptionMapDouble(options('substituteWeight)).isDefined ) { execute(options) exit(options) @@ -57,9 +57,9 @@ object weightedLevenshteinMetric extends Command { override def execute(options: OptionMap): Unit = { val strings = options('dashless).split(" ") val weights = Tuple3[BigDecimal, BigDecimal, BigDecimal]( - ParseUtility.parseBigDecimal(options('deleteWeight)).get, - ParseUtility.parseBigDecimal(options('insertWeight)).get, - ParseUtility.parseBigDecimal(options('substituteWeight)).get + OptionMapBigDecimal(options('deleteWeight)), + OptionMapBigDecimal(options('insertWeight)), + OptionMapBigDecimal(options('substituteWeight)) ) println( diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapSpec.scala new file mode 100755 index 0000000..23821c6 --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapSpec.scala @@ -0,0 +1,84 @@ +package org.hashtree.stringmetric.cli + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class OptionMapSpec extends ScalaTest { + "OptionMap" should provide { + "apply method" when passed { + "single valid double dashed option" should returns { + "populated Map" in { + val options = OptionMap("--help") + + options('help) should equal ("") + } + } + "multiple valid double dashed options" should returns { + "populated Map" in { + val options = OptionMap("--help", "--test=test") + + options('help) should equal ("") + options('test) should equal ("test") + } + } + "invalid double dashed options" should returns { + "empty Map" in { + val options = OptionMap("--help#", "--test%=test") + + options.keysIterator.length should be (0) + } + } + "single valid single dashed option" should returns { + "populated Map" in { + val options = OptionMap("-h") + + options('h) should equal ("") + } + } + "multiple valid single dashed options" should returns { + "populated Map" in { + val options = OptionMap("-h", "-i") + + options('h) should equal ("") + options('i) should equal ("") + } + } + "invalid single dashed options" should returns { + "empty Map" in { + val options = OptionMap("-h-i", "-i#gloo") + + options.keysIterator.length should be (0) + } + } + "single nameless option" should returns { + "single key populated Map" in { + val options = OptionMap("filename0") + + options('dashless).count(_ == ' ') should be (0) + } + } + "multiple single nameless options" should returns { + "single key populated Map" in { + val options = OptionMap("filename0", "filename1", "filename2") + + options('dashless).count(_ == ' ') should be (2) + } + } + "mixed options" should returns { + "populated Map" in { + val options = OptionMap( + "-q", "--help", "--test=test", "-go", "filename0", "filename1", "filename2" + ) + + options('q) should equal ("") + options('help) should equal ("") + options('test) should equal ("test") + options('go) should equal ("") + options('dashless).count(_ == ' ') should be (2) + } + } + } + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapTypeSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapTypeSpec.scala new file mode 100755 index 0000000..143d64b --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapTypeSpec.scala @@ -0,0 +1,149 @@ +package org.hashtree.stringmetric.cli + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class OptionMapTypeSpec extends ScalaTest { + "OptionMapBigDecimal" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapBigDecimal("one").get.isDefined should be (false) + + (OptionMapBigDecimal("one"): Option[BigDecimal]).isDefined should be (false) + } + } + "valid argument" should returns { + "BigDecimal" in { + OptionMapBigDecimal("1").get.get should equal (1: BigDecimal) + + (OptionMapBigDecimal("1"): BigDecimal) should equal (1: BigDecimal) + + (OptionMapBigDecimal("1"): String) should equal ("1") + } + } + } + } + "OptionMapBigInt" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapBigInt("one").get.isDefined should be (false) + + (OptionMapBigInt("one"): Option[BigInt]).isDefined should be (false) + } + } + "valid argument" should returns { + "BigInt" in { + OptionMapBigInt("1").get.get should equal (1: BigInt) + + (OptionMapBigInt("1"): BigInt) should equal (1: BigInt) + + (OptionMapBigInt("1"): String) should equal ("1") + } + } + } + } + "OptionMapDouble" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapDouble("one").get.isDefined should be (false) + + (OptionMapDouble("one"): Option[Double]).isDefined should be (false) + } + } + "valid argument" should returns { + "Double" in { + OptionMapDouble("1").get.get should equal (1d) + + (OptionMapDouble("1"): Double) should equal (1d) + + (OptionMapDouble("1"): String) should equal ("1.0") + } + } + } + } + "OptionMapFloat" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapFloat("one").get.isDefined should be (false) + + (OptionMapFloat("one"): Option[Float]).isDefined should be (false) + } + } + "valid argument" should returns { + "Float" in { + OptionMapFloat("1").get.get should equal (1f) + + (OptionMapFloat("1"): Float) should equal (1f) + + (OptionMapFloat("1"): String) should equal ("1.0") + } + } + } + } + "OptionMapInt" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapInt("one").get.isDefined should be (false) + + (OptionMapInt("one"): Option[Int]).isDefined should be (false) + } + } + "valid argument" should returns { + "Int" in { + OptionMapInt("1").get.get should equal (1) + + (OptionMapInt("1"): Int) should equal (1) + + (OptionMapInt("1"): String) should equal ("1") + } + } + } + } + "OptionMapLong" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapLong("one").get.isDefined should be (false) + + (OptionMapLong("one"): Option[Long]).isDefined should be (false) + } + } + "valid argument" should returns { + "Long" in { + OptionMapLong("1").get.get should equal (1l) + + (OptionMapLong("1"): Long) should equal (1l) + + (OptionMapLong("1"): String) should equal ("1") + } + } + } + } + "OptionMapShort" should provide { + "get method" when passed { + "invalid argument" should returns { + "None" in { + OptionMapShort("one").get.isDefined should be (false) + + (OptionMapShort("one"): Option[Short]).isDefined should be (false) + } + } + "valid argument" should returns { + "Short" in { + OptionMapShort("1").get.get should equal (1: Short) + + (OptionMapShort("1"): Short) should equal (1: Short) + + (OptionMapShort("1"): String) should equal ("1") + } + } + } + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapUtilitySpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapUtilitySpec.scala deleted file mode 100755 index a5a8eb1..0000000 --- a/cli/source/test/scala/org/hashtree/stringmetric/cli/OptionMapUtilitySpec.scala +++ /dev/null @@ -1,151 +0,0 @@ -package org.hashtree.stringmetric.cli - -import org.hashtree.stringmetric.ScalaTest -import org.junit.runner.RunWith -import org.scalatest.junit.JUnitRunner - -@RunWith(classOf[JUnitRunner]) -final class OptionMapUtilitySpec extends ScalaTest { - "OptionMapUtility" should provide { - "overloaded toOptionMap method" when passed { - "Array with a single valid double dashed option" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("--help")) - - options('help) should equal ("") - } - } - "List with a single valid double dashed option" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(List("--help")) - - options('help) should equal ("") - } - } - "Array with a multiple valid double dashed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("--help", "--test=test")) - - options('help) should equal ("") - options('test) should equal ("test") - } - } - "List with a multiple valid double dashed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(List("--help", "--test=test")) - - options('help) should equal ("") - options('test) should equal ("test") - } - } - "Array with invalid double dashed options" should returns { - "empty Map" in { - val options = OptionMapUtility.toOptionMap(Array("--help#", "--test%=test")) - - options.keysIterator.length should be (0) - } - } - "List with invalid double dashed options" should returns { - "empty Map" in { - val options = OptionMapUtility.toOptionMap(List("--help#", "--test%=test")) - - options.keysIterator.length should be (0) - } - } - "Array with a single valid single dashed option" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("-h")) - - options('h) should equal ("") - } - } - "List with a single valid single dashed option" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(List("-h")) - - options('h) should equal ("") - } - } - "Array with multiple valid single dashed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("-h", "-i")) - - options('h) should equal ("") - options('i) should equal ("") - } - } - "List with multiple valid single dashed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(List("-h", "-i")) - - options('h) should equal ("") - options('i) should equal ("") - } - } - "Array with an invalid single dashed options" should returns { - "empty Map" in { - val options = OptionMapUtility.toOptionMap(Array("-h-i", "-i#gloo")) - - options.keysIterator.length should be (0) - } - } - "List with an invalid single dashed options" should returns { - "empty Map" in { - val options = OptionMapUtility.toOptionMap(List("-h-i", "-i#gloo")) - - options.keysIterator.length should be (0) - } - } - "Array with a single nameless option" should returns { - "single key populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("filename0")) - - options('dashless).count(_ == ' ') should be (0) - } - } - "List with a single nameless option" should returns { - "single key populated Map" in { - val options = OptionMapUtility.toOptionMap(List("filename0")) - - options('dashless).count(_ == ' ') should be (0) - } - } - "Array with multiple single nameless options" should returns { - "single key populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("filename0", "filename1", "filename2")) - - options('dashless).count(_ == ' ') should be (2) - } - } - "List with multiple single nameless options" should returns { - "single key populated Map" in { - val options = OptionMapUtility.toOptionMap(List("filename0", "filename1", "filename2")) - - options('dashless).count(_ == ' ') should be (2) - } - } - "Array with mixed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(Array("-q", "--help", "--test=test", "-go", "filename0", "filename1", "filename2")) - - options('q) should equal ("") - options('help) should equal ("") - options('test) should equal ("test") - options('go) should equal ("") - options('dashless).count(_ == ' ') should be (2) - } - } - "List with mixed options" should returns { - "populated Map" in { - val options = OptionMapUtility.toOptionMap(List("-q", "--help", "--test=test", "-go", "filename0", "filename1", "filename2")) - - options('q) should equal ("") - options('help) should equal ("") - options('test) should equal ("test") - options('go) should equal ("") - options('dashless).count(_ == ' ') should be (2) - } - } - } - } -} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala deleted file mode 100755 index a7b4adc..0000000 --- a/cli/source/test/scala/org/hashtree/stringmetric/cli/ParseUtilitySpec.scala +++ /dev/null @@ -1,96 +0,0 @@ -package org.hashtree.stringmetric.cli - -import org.hashtree.stringmetric.ScalaTest -import org.junit.runner.RunWith -import org.scalatest.junit.JUnitRunner -import scala.math.{ BigDecimal, BigInt } - -@RunWith(classOf[JUnitRunner]) -final class ParseUtilitySpec extends ScalaTest { - "ParseUtility" should provide { - "parseBigDecimal method" when passed { - "invalid argument" should returns { - "None" in { - ParseUtility.parseBigDecimal("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(BigDecimal)" in { - ParseUtility.parseBigDecimal("1").get should equal (BigDecimal(1)) - } - } - } - "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 { - ParseUtility.parseDouble("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(Double)" in { - ParseUtility.parseDouble("1").get should be (1d) - } - } - } - "parseFloat method" when passed { - "invalid argument" should returns { - "None" in { - ParseUtility.parseFloat("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(Float)" in { - ParseUtility.parseFloat("1").get should be (1f) - } - } - } - "parseInt method" when passed { - "invalid argument" should returns { - "None" in { - ParseUtility.parseInt("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(Int)" in { - ParseUtility.parseInt("1").get should be (1) - } - } - } - "parseLong method" when passed { - "invalid argument" should returns { - "None" in { - ParseUtility.parseLong("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(Long)" in { - ParseUtility.parseLong("1").get should be (1l) - } - } - } - "parseShort method" when passed { - "invalid argument" should returns { - "None" in { - ParseUtility.parseShort("one").isDefined should be (false) - } - } - "valid argument" should returns { - "Some(Short)" in { - ParseUtility.parseShort("1").get should equal (1: Short) - } - } - } - } -} \ No newline at end of file -- cgit v1.2.3