diff options
author | Rocky Madden <git@rockymadden.com> | 2013-03-07 13:23:31 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-03-07 13:23:31 -0700 |
commit | d3ee1e3fa282de1a6696ca54a3bb4c89c10a28ce (patch) | |
tree | ecd9b646bc1e71ff492098195faae7ba666204e2 /core/source | |
parent | 6195ae90c35ba8d79c77ed3a1ac205b70609fdd0 (diff) | |
download | stringmetric-d3ee1e3fa282de1a6696ca54a3bb4c89c10a28ce.tar.gz stringmetric-d3ee1e3fa282de1a6696ca54a3bb4c89c10a28ce.tar.bz2 stringmetric-d3ee1e3fa282de1a6696ca54a3bb4c89c10a28ce.zip |
Nested case objects within Alphabet object.
Diffstat (limited to 'core/source')
12 files changed, 67 insertions, 54 deletions
diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala index 5c69035..089b01b 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/Alphabet.scala @@ -2,58 +2,60 @@ package com.rockymadden.stringmetric.phonetic import scala.collection.immutable.Set -sealed abstract class Alphabet { - val Chars: Set[Char] - - def isSuperset(char: Char): Boolean = Chars.contains(char) - - def isSuperset(charArray: Array[Char]): Boolean = - charArray.length > 0 && charArray.takeWhile(Chars.contains(_)).length == charArray.length - - def isSuperset(string: String): Boolean = isSuperset(string.toCharArray) - - def startsWith(char: Char): Boolean = Chars.contains(char) - - def startsWith(charArray: Array[Char]): Boolean = charArray.length > 0 && Chars.contains(charArray.head) - - def startsWith(string: String): Boolean = startsWith(string.toCharArray) -} - -case object LowercaseConsonant extends Alphabet { - override final val Chars = - Set('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x' ,'z') -} -case object UppercaseConsonant extends Alphabet { - override final val Chars = - Set('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X' ,'Z') -} -case object Consonant extends Alphabet { - override final val Chars = LowercaseConsonant.Chars ++ UppercaseConsonant.Chars -} -case object LowercaseVowel extends Alphabet { - override final val Chars = Set('a', 'e', 'i', 'o', 'u') -} -case object UppercaseVowel extends Alphabet { - override final val Chars = Set('A', 'E', 'I', 'O', 'U') -} -case object Vowel extends Alphabet { - override final val Chars = LowercaseVowel.Chars ++ UppercaseVowel.Chars -} -case object LowercaseY extends Alphabet { - override final val Chars = Set('y') -} -case object UppercaseY extends Alphabet { - override final val Chars = Set('Y') -} -case object Y extends Alphabet { - override final val Chars = LowercaseY.Chars ++ UppercaseY.Chars -} -case object LowercaseAlpha extends Alphabet { - override final val Chars = LowercaseConsonant.Chars ++ LowercaseVowel.Chars ++ LowercaseY.Chars -} -case object UppercaseAlpha extends Alphabet { - override final val Chars = UppercaseConsonant.Chars ++ UppercaseVowel.Chars ++ UppercaseY.Chars -} -case object Alpha extends Alphabet { - override final val Chars = LowercaseAlpha.Chars ++ UppercaseAlpha.Chars +object Alphabet { + protected sealed abstract class AlphabetSet { + protected[Alphabet] val Chars: Set[Char] + + def isSuperset(char: Char): Boolean = Chars.contains(char) + + def isSuperset(charArray: Array[Char]): Boolean = + charArray.length > 0 && charArray.takeWhile(Chars.contains(_)).length == charArray.length + + def isSuperset(string: String): Boolean = isSuperset(string.toCharArray) + + def startsWith(char: Char): Boolean = Chars.contains(char) + + def startsWith(charArray: Array[Char]): Boolean = charArray.length > 0 && Chars.contains(charArray.head) + + def startsWith(string: String): Boolean = startsWith(string.toCharArray) + } + + case object LowercaseConsonant extends AlphabetSet { + override protected[Alphabet] final val Chars = + Set('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x' ,'z') + } + case object UppercaseConsonant extends AlphabetSet { + override protected[Alphabet] final val Chars = + Set('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X' ,'Z') + } + case object Consonant extends AlphabetSet { + override protected[Alphabet] final val Chars = LowercaseConsonant.Chars ++ UppercaseConsonant.Chars + } + case object LowercaseVowel extends AlphabetSet { + override protected[Alphabet] final val Chars = Set('a', 'e', 'i', 'o', 'u') + } + case object UppercaseVowel extends AlphabetSet { + override protected[Alphabet] final val Chars = Set('A', 'E', 'I', 'O', 'U') + } + case object Vowel extends AlphabetSet { + override protected[Alphabet] final val Chars = LowercaseVowel.Chars ++ UppercaseVowel.Chars + } + case object LowercaseY extends AlphabetSet { + override protected[Alphabet] final val Chars = Set('y') + } + case object UppercaseY extends AlphabetSet { + override protected[Alphabet] final val Chars = Set('Y') + } + case object Y extends AlphabetSet { + override protected[Alphabet] final val Chars = LowercaseY.Chars ++ UppercaseY.Chars + } + case object LowercaseAlpha extends AlphabetSet { + override protected[Alphabet] final val Chars = LowercaseConsonant.Chars ++ LowercaseVowel.Chars ++ LowercaseY.Chars + } + case object UppercaseAlpha extends AlphabetSet { + override protected[Alphabet] final val Chars = UppercaseConsonant.Chars ++ UppercaseVowel.Chars ++ UppercaseY.Chars + } + case object Alpha extends AlphabetSet { + override protected[Alphabet] final val Chars = LowercaseAlpha.Chars ++ UppercaseAlpha.Chars + } } diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala index 1949375..518213b 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet.{ Alpha, LowercaseVowel } import scala.annotation.{ switch, tailrec } /** An implementation of the Metaphone algorithm. */ diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala index a694d5f..cce3df9 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/MetaphoneMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha /** A implementation of the Metaphone metric. */ class MetaphoneMetric extends StringMetric[Boolean] { diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala index 3f2d029..cd07a71 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet.{ Alpha, LowercaseVowel } import scala.annotation.{ switch, tailrec } /** An implementation of the NYSIIS algorithm. */ diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala index 6560ad7..9df9b16 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/NysiisMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha /** An implementation of the NYSIIS metric. */ class NysiisMetric extends StringMetric[Boolean] { diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala index e779296..ef8a134 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet.{ Alpha, LowercaseVowel } import scala.annotation.{ switch, tailrec } /** An implementation of the refined NYSIIS algorithm. */ diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala index 16ac11a..7be84af 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedNysiisMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha /** An implementation of the refined NYSIIS metric. */ class RefinedNysiisMetric extends StringMetric[Boolean] { diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala index cda0e7e..1f460fb 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha import scala.annotation.{ switch, tailrec } /** An implementation of the refined Soundex algorithm. */ diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala index 3e78e34..d1bd6bb 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha /** An implementation of the refined Soundex metric. */ class RefinedSoundexMetric extends StringMetric[Boolean] { diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala index 04f5525..c7adb18 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexAlgorithm.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringAlgorithm, StringFilter } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha import scala.annotation.{ switch, tailrec } /** An implementation of the Soundex algorithm. */ diff --git a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala index a9d7784..928066b 100755 --- a/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala +++ b/core/source/core/scala/com/rockymadden/stringmetric/phonetic/SoundexMetric.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.{ StringFilter, StringMetric } +import com.rockymadden.stringmetric.phonetic.Alphabet.Alpha /** An implementation of the Soundex metric. */ class SoundexMetric extends StringMetric[Boolean] { diff --git a/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala b/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala index 8329b57..8074ca8 100755 --- a/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala +++ b/core/source/test/scala/com/rockymadden/stringmetric/phonetic/AlphabetSpec.scala @@ -1,6 +1,7 @@ package com.rockymadden.stringmetric.phonetic import com.rockymadden.stringmetric.ScalaTest +import com.rockymadden.stringmetric.phonetic.Alphabet.{ Alpha, Vowel } import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner |