diff options
Diffstat (limited to 'cli')
21 files changed, 183 insertions, 358 deletions
diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/OptionMapType.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/OptionMapType.scala index 92c91eb..7298905 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/OptionMapType.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/OptionMapType.scala @@ -3,75 +3,65 @@ package com.rockymadden.stringmetric.cli import scala.language.implicitConversions sealed abstract class OptionMapType[T](protected[this] val stringSelf: String) { - def get(): Option[T] + def parse: T } final case class OptionMapArray(arrayString: String) extends OptionMapType[Array[String]](arrayString) { - private[this] lazy val self = try { - if (stringSelf.length == 0) None - else Some(stringSelf.split(" ")) - } catch { case _: Throwable => None } + private[this] lazy val self = if (stringSelf.length == 0) Array.empty[String] else stringSelf.split(" ") - override def get() = self + override def parse = self } final case class OptionMapBigDecimal(bigDecimalString: String) extends OptionMapType[BigDecimal](bigDecimalString) { - private[this] lazy val self = try { Some(BigDecimal(stringSelf)) } catch { case _: Throwable => None } + private[this] lazy val self = BigDecimal(stringSelf) - override def get() = self + override def parse = self } final case class OptionMapBigInt(bigIntString: String) extends OptionMapType[BigInt](bigIntString) { - private[this] lazy val self = try { Some(BigInt(stringSelf)) } catch { case _: Throwable => None } + private[this] lazy val self = BigInt(stringSelf) - override def get() = self + override def parse = self } final case class OptionMapDouble(doubleString: String) extends OptionMapType[Double](doubleString) { - private[this] lazy val self = try { Some(stringSelf.toDouble) } catch { case _: Throwable => None } + private[this] lazy val self = stringSelf.toDouble - override def get() = self + override def parse = self } final case class OptionMapFloat(floatString: String) extends OptionMapType[Float](floatString) { - private[this] lazy val self = try { Some(stringSelf.toFloat) } catch { case _: Throwable => None } + private[this] lazy val self = stringSelf.toFloat - override def get() = self + override def parse = self } final case class OptionMapInt(intString: String) extends OptionMapType[Int](intString) { - private[this] lazy val self = try { Some(stringSelf.toInt) } catch { case _: Throwable => None } + private[this] lazy val self = stringSelf.toInt - override def get() = self + override def parse = self } final case class OptionMapList(listString: String) extends OptionMapType[List[String]](listString) { - private[this] lazy val self = try { - if (stringSelf.length == 0) None - else Some(stringSelf.split(" ").toList) - } catch { case _: Throwable => None } + private[this] lazy val self = if (stringSelf.length == 0) List.empty[String] else stringSelf.split(" ").toList - override def get() = self + override def parse = self } final case class OptionMapLong(longString: String) extends OptionMapType[Long](longString) { - private[this] lazy val self = try { Some(stringSelf.toLong) } catch { case _: Throwable => None } + private[this] lazy val self = stringSelf.toLong - override def get() = self + override def parse = self } final case class OptionMapShort(shortString: String) extends OptionMapType[Short](shortString) { - private[this] lazy val self = try { Some(stringSelf.toShort) } catch { case _: Throwable => None } + private[this] lazy val self = stringSelf.toShort - override def get() = self + override def parse = self } object OptionMapType { - implicit def OptionMapTypeToOptionT[T](optionMapType: OptionMapType[T]): Option[T] = optionMapType.get - - implicit def OptionMapTypeToT[T](optionMapType: OptionMapType[T]): T = - if(!optionMapType.isDefined) throw new IllegalArgumentException("Expected successful type conversion.") - else optionMapType.get.get + implicit def OptionMapTypeToT[T](optionMapType: OptionMapType[T]): T = optionMapType.parse implicit def StringToOptionMapArray(string: String): OptionMapArray = new OptionMapArray(string) diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneAlgorithm.scala index c2d18f9..8ef925b 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneAlgorithm.scala @@ -11,18 +11,15 @@ object metaphoneAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -42,9 +39,5 @@ object metaphoneAlgorithm extends Command { } override def execute(options: OptionMap): Unit = - println( - MetaphoneAlgorithm.compute( - options('dashless) - ).getOrElse("not computable") - ) + println(MetaphoneAlgorithm.compute(options('dashless)).getOrElse("not computable")) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneMetric.scala index c89c55e..d3cd7f0 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/metaphoneMetric.scala @@ -11,18 +11,15 @@ object metaphoneMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object metaphoneMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - MetaphoneMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(MetaphoneMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisAlgorithm.scala index df50986..33a9d82 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisAlgorithm.scala @@ -11,18 +11,15 @@ object nysiisAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -42,9 +39,5 @@ object nysiisAlgorithm extends Command { } override def execute(options: OptionMap): Unit = - println( - NysiisAlgorithm.compute( - options('dashless) - ).getOrElse("not computable") - ) + println(NysiisAlgorithm.compute(options('dashless)).getOrElse("not computable")) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisMetric.scala index 58157ca..f051bed 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisMetric.scala @@ -11,18 +11,15 @@ object nysiisMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object nysiisMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - NysiisMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(NysiisMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala index b11e369..a2a18e4 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala @@ -11,18 +11,15 @@ object refinedNysiisAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -42,9 +39,5 @@ object refinedNysiisAlgorithm extends Command { } override def execute(options: OptionMap): Unit = - println( - RefinedNysiisAlgorithm.compute( - options('dashless) - ).getOrElse("not computable") - ) + println(RefinedNysiisAlgorithm.compute(options('dashless)).getOrElse("not computable")) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisMetric.scala index 3b78565..00a078e 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedNysiisMetric.scala @@ -11,18 +11,15 @@ object refinedNysiisMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object refinedNysiisMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - RefinedNysiisMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(RefinedNysiisMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala index 2d9a8ad..3fe6ed3 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexAlgorithm.scala @@ -11,18 +11,15 @@ object refinedSoundexAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -42,9 +39,5 @@ object refinedSoundexAlgorithm extends Command { } override def execute(options: OptionMap): Unit = - println( - RefinedSoundexAlgorithm.compute( - options('dashless) - ).getOrElse("not computable") - ) + println(RefinedSoundexAlgorithm.compute(options('dashless)).getOrElse("not computable")) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexMetric.scala index a0101e8..2a0dbf5 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/refinedSoundexMetric.scala @@ -11,18 +11,15 @@ object refinedSoundexMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object refinedSoundexMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - RefinedSoundexMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(RefinedSoundexMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexAlgorithm.scala index ecbea71..45e78d3 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexAlgorithm.scala @@ -11,18 +11,15 @@ object soundexAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -42,9 +39,5 @@ object soundexAlgorithm extends Command { } override def execute(options: OptionMap): Unit = - println( - SoundexAlgorithm.compute( - options('dashless) - ).getOrElse("not computable") - ) + println(SoundexAlgorithm.compute(options('dashless)).getOrElse("not computable")) } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexMetric.scala index 77f49fd..9e6c21d 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexMetric.scala @@ -11,18 +11,15 @@ object soundexMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object soundexMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - SoundexMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(SoundexMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala index c2a21b5..02b4b09 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/diceSorensenMetric.scala @@ -11,20 +11,17 @@ object diceSorensenMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2 - && options.contains('n) && (options('n): OptionMapInt).isDefined - ) { + && options.contains('n) && (options('n): OptionMapInt) >= 1) { + execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -49,11 +46,6 @@ object diceSorensenMetric extends Command { val strings: OptionMapArray = options('dashless) val n: OptionMapInt = options('n) - println( - DiceSorensenMetric.compare( - strings(0), - strings(1) - )(n).getOrElse("not comparable") - ) + println(DiceSorensenMetric.compare(strings(0), strings(1))(n).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/hammingMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/hammingMetric.scala index ec9e159..c5373d2 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/hammingMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/hammingMetric.scala @@ -11,18 +11,15 @@ object hammingMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object hammingMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - HammingMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(HammingMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroMetric.scala index 2f43bf9..7e54c61 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroMetric.scala @@ -8,18 +8,15 @@ object jaroMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -41,11 +38,6 @@ object jaroMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - JaroMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(JaroMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroWinklerMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroWinklerMetric.scala index 4c6dd60..287e6f6 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroWinklerMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/jaroWinklerMetric.scala @@ -11,18 +11,15 @@ object jaroWinklerMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object jaroWinklerMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - JaroWinklerMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(JaroWinklerMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinMetric.scala index e48511a..c8e65a5 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinMetric.scala @@ -11,18 +11,15 @@ object levenshteinMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -45,11 +42,6 @@ object levenshteinMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - LevenshteinMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(LevenshteinMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramAlgorithm.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramAlgorithm.scala index 668fa8a..f684514 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramAlgorithm.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramAlgorithm.scala @@ -11,20 +11,17 @@ object nGramAlgorithm extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1 - && options.contains('n) && (options('n): OptionMapInt).isDefined - ) { + && options.contains('n) && (options('n): OptionMapInt) >= 1) { + execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -48,8 +45,6 @@ object nGramAlgorithm extends Command { override def execute(options: OptionMap): Unit = { val n: OptionMapInt = options('n) - println( - NGramAlgorithm.compute(options('dashless))(n).map(_.mkString("|")).getOrElse("not computable") - ) + println(NGramAlgorithm.compute(options('dashless))(n).map(_.mkString("|")).getOrElse("not computable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramMetric.scala index 23428be..5236dc3 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/nGramMetric.scala @@ -11,20 +11,17 @@ object nGramMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2 - && options.contains('n) && (options('n): OptionMapInt).isDefined - ) { + && options.contains('n) && (options('n): OptionMapInt) >= 1) { + execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -49,11 +46,6 @@ object nGramMetric extends Command { val strings: OptionMapArray = options('dashless) val n: OptionMapInt = options('n) - println( - NGramMetric.compare( - strings(0), - strings(1) - )(n).getOrElse("not comparable") - ) + println(NGramMetric.compare(strings(0), strings(1))(n).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala index fa31794..c477122 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala @@ -11,18 +11,15 @@ object ratcliffObershelpMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -44,11 +41,6 @@ object ratcliffObershelpMetric extends Command { override def execute(options: OptionMap): Unit = { val strings: OptionMapArray = options('dashless) - println( - RatcliffObershelpMetric.compare( - strings(0), - strings(1) - ).getOrElse("not comparable") - ) + println(RatcliffObershelpMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) } } diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/weightedLevenshteinMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/weightedLevenshteinMetric.scala index c76e2b4..09f83ff 100755 --- a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/weightedLevenshteinMetric.scala +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/weightedLevenshteinMetric.scala @@ -13,22 +13,19 @@ object weightedLevenshteinMetric extends Command { override def main(args: Array[String]): Unit = { val options = OptionMap(args) - try { - // Help. + try if (options.contains('h) || options.contains('help)) { help() exit(options) - // Execute. } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2 - && options.contains('deleteWeight) && (options('deleteWeight): OptionMapDouble).isDefined - && options.contains('insertWeight) && (options('insertWeight): OptionMapDouble).isDefined - && options.contains('substituteWeight) && (options('substituteWeight): OptionMapDouble).isDefined - ) { + && options.contains('deleteWeight) && (options('deleteWeight): OptionMapDouble) >= 0 + && options.contains('insertWeight) && (options('insertWeight): OptionMapDouble) >= 0 + && options.contains('substituteWeight) && (options('substituteWeight): OptionMapDouble) >= 0) { + execute(options) exit(options) - // Invalid syntax. } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - } catch { + catch { case e: Throwable => error(e, options) } } @@ -62,11 +59,6 @@ object weightedLevenshteinMetric extends Command { (options('substituteWeight): OptionMapBigDecimal) ) - println( - WeightedLevenshteinMetric.compare( - strings(0), - strings(1) - )(weights).getOrElse("not comparable") - ) + println(WeightedLevenshteinMetric.compare(strings(0), strings(1))(weights).getOrElse("not comparable")) } } diff --git a/cli/source/test/scala/com/rockymadden/stringmetric/cli/OptionMapTypeSpec.scala b/cli/source/test/scala/com/rockymadden/stringmetric/cli/OptionMapTypeSpec.scala index 178a825..48510c3 100755 --- a/cli/source/test/scala/com/rockymadden/stringmetric/cli/OptionMapTypeSpec.scala +++ b/cli/source/test/scala/com/rockymadden/stringmetric/cli/OptionMapTypeSpec.scala @@ -7,26 +7,19 @@ import org.scalatest.junit.JUnitRunner @RunWith(classOf[JUnitRunner]) final class OptionMapTypeSpec extends ScalaTest { "OptionMapArray" should provide { - "get method" when passed { + "parse method" when passed { "invalid argument" should returns { - "None" in { - OptionMapArray("").get.isDefined should be (false) + "empty Array" in { + OptionMapArray("").parse should equal (Array.empty[String]) - (OptionMapArray(""): Option[Array[String]]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { - evaluating { - OptionMapArray(""): Array[String] - } should produce[IllegalArgumentException] + (OptionMapArray(""): Array[String]) should equal (Array.empty[String]) } } "valid argument" should returns { "Array" in { - OptionMapArray("1").get.get should equal (Array("1")) + OptionMapArray("1").parse should equal (Array("1")) - OptionMapArray("1 2 3").get.get should equal (Array("1", "2", "3")) + OptionMapArray("1 2 3").parse should equal (Array("1", "2", "3")) (OptionMapArray("1 2 3"): Array[String]) should equal (Array("1", "2", "3")) } @@ -34,26 +27,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapBigDecimal" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapBigDecimal("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapBigDecimal("").parse + } should produce[NumberFormatException] - OptionMapBigDecimal("one").get.isDefined should be (false) + evaluating { + OptionMapBigDecimal("one").parse + } should produce[NumberFormatException] - (OptionMapBigDecimal("one"): Option[BigDecimal]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapBigDecimal("one"): BigDecimal - } should produce[IllegalArgumentException] + (OptionMapBigDecimal("one"): BigDecimal) + } should produce[NumberFormatException] } } "valid argument" should returns { "BigDecimal" in { - OptionMapBigDecimal("1").get.get should equal (1: BigDecimal) + OptionMapBigDecimal("1").parse should equal (1: BigDecimal) (OptionMapBigDecimal("1"): BigDecimal) should equal (1: BigDecimal) } @@ -61,26 +53,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapBigInt" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapBigInt("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapBigInt("").parse + } should produce[NumberFormatException] - OptionMapBigInt("one").get.isDefined should be (false) + evaluating { + OptionMapBigInt("one").parse + } should produce[NumberFormatException] - (OptionMapBigInt("one"): Option[BigInt]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapBigInt("one"): BigInt - } should produce[IllegalArgumentException] + (OptionMapBigInt("one"): BigInt) + } should produce[NumberFormatException] } } "valid argument" should returns { "BigInt" in { - OptionMapBigInt("1").get.get should equal (1: BigInt) + OptionMapBigInt("1").parse should equal (1: BigInt) (OptionMapBigInt("1"): BigInt) should equal (1: BigInt) } @@ -88,26 +79,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapDouble" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapDouble("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapDouble("").parse + } should produce[NumberFormatException] - OptionMapDouble("one").get.isDefined should be (false) + evaluating { + OptionMapDouble("one").parse + } should produce[NumberFormatException] - (OptionMapDouble("one"): Option[Double]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapDouble("one"): Double - } should produce[IllegalArgumentException] + (OptionMapDouble("one"): Double) + } should produce[NumberFormatException] } } "valid argument" should returns { "Double" in { - OptionMapDouble("1").get.get should equal (1d) + OptionMapDouble("1").parse should equal (1d) (OptionMapDouble("1"): Double) should equal (1d) } @@ -115,26 +105,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapFloat" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapFloat("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapFloat("").parse + } should produce[NumberFormatException] - OptionMapFloat("one").get.isDefined should be (false) + evaluating { + OptionMapFloat("one").parse + } should produce[NumberFormatException] - (OptionMapFloat("one"): Option[Float]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapFloat("one"): Float - } should produce[IllegalArgumentException] + (OptionMapFloat("one"): Float) + } should produce[NumberFormatException] } } "valid argument" should returns { "Float" in { - OptionMapFloat("1").get.get should equal (1f) + OptionMapFloat("1").parse should equal (1f) (OptionMapFloat("1"): Float) should equal (1f) } @@ -142,26 +131,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapInt" should provide { - "get method" when passed { + "parse method" when passed { "invalid argument" should returns { - "None" in { - OptionMapInt("").get.isDefined should be (false) + "NumberFormatException" in { + evaluating { + OptionMapInt("").parse + } should produce[NumberFormatException] - OptionMapInt("one").get.isDefined should be (false) + evaluating { + OptionMapInt("one").parse + } should produce[NumberFormatException] - (OptionMapInt("one"): Option[Int]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapInt("one"): Int - } should produce[IllegalArgumentException] + (OptionMapInt("one"): Int) + } should produce[NumberFormatException] } } "valid argument" should returns { "Int" in { - OptionMapInt("1").get.get should equal (1) + OptionMapInt("1").parse should equal (1) (OptionMapInt("1"): Int) should equal (1) } @@ -169,26 +157,19 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapList" should provide { - "get method" when passed { + "parse method" when passed { "invalid argument" should returns { - "None" in { - OptionMapList("").get.isDefined should be (false) + "empty List" in { + OptionMapList("").parse should equal (List.empty[String]) - (OptionMapList(""): Option[List[String]]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { - evaluating { - OptionMapList(""): List[String] - } should produce[IllegalArgumentException] + (OptionMapList(""): List[String]) should equal (List.empty[String]) } } "valid argument" should returns { "List" in { - OptionMapList("1").get.get should equal (List("1")) + OptionMapList("1").parse should equal (List("1")) - OptionMapList("1 2 3").get.get should equal (List("1", "2", "3")) + OptionMapList("1 2 3").parse should equal (List("1", "2", "3")) (OptionMapList("1 2 3"): List[String]) should equal (List("1", "2", "3")) } @@ -196,26 +177,25 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapLong" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapLong("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapLong("").parse + } should produce[NumberFormatException] - OptionMapLong("one").get.isDefined should be (false) + evaluating { + OptionMapLong("one").parse + } should produce[NumberFormatException] - (OptionMapLong("one"): Option[Long]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapLong("one"): Long - } should produce[IllegalArgumentException] + (OptionMapLong("one"): Long) + } should produce[NumberFormatException] } } "valid argument" should returns { "Long" in { - OptionMapLong("1").get.get should equal (1l) + OptionMapLong("1").parse should equal (1l) (OptionMapLong("1"): Long) should equal (1l) } @@ -223,30 +203,29 @@ final class OptionMapTypeSpec extends ScalaTest { } } "OptionMapShort" should provide { - "get method" when passed { - "invalid argument" should returns { - "None" in { - OptionMapShort("").get.isDefined should be (false) + "parse method" when passed { + "invalid argument" should throws { + "NumberFormatException" in { + evaluating { + OptionMapShort("").parse + } should produce[NumberFormatException] - OptionMapShort("one").get.isDefined should be (false) + evaluating { + OptionMapShort("one").parse + } should produce[NumberFormatException] - (OptionMapShort("one"): Option[Short]).isDefined should be (false) - } - } - "invalid argument accessed via implicit to type" should throws { - "IllegalArgumentException" in { evaluating { - OptionMapShort("one"): Short - } should produce[IllegalArgumentException] + (OptionMapShort("one"): Short) + } should produce[NumberFormatException] } } "valid argument" should returns { "Short" in { - OptionMapShort("1").get.get should equal (1: Short) + OptionMapShort("1").parse should equal (1: Short) (OptionMapShort("1"): Short) should equal (1: Short) } } } } -}
\ No newline at end of file +} |