Skip to content

Instantly share code, notes, and snippets.

@amir343
Created October 12, 2011 08:34
Show Gist options
  • Save amir343/1280657 to your computer and use it in GitHub Desktop.
Save amir343/1280657 to your computer and use it in GitHub Desktop.
Source that is used for Injectors and Extractros in Scala blog post
case class Firstname(name:String)
object IPAddress {
def apply(a:String, b:String, c:String, d:String):String = a + "." + b + "." + c + "." + d
def unapply(ip:String):Option[(String, String, String, String)] = {
...
}
}
object IPAddress {
def unapply(ip:String):Option[(String, String, String, String)] = {
val tokens = ip split "\\."
if (tokens.length == 4 && isValid(tokens)) Some(tokens(0), tokens(1), tokens(2), tokens(3)) else None
}
private def isValid (tokens:Array[String]):Boolean = {
tokens forall { elem =>
try {
val intValue = elem.toInt
intValue >= 0 && intValue <= 255
} catch {
case _ => false
}
}
}
}
object IPAddresses {
def unapplySeq(ips:String):Option[Seq[String]] = {
Some(ips split ",")
}
}
def unapply(ip:String):Boolean = {
val tokens = ip split "\\."
if (tokens.length == 4 && isValid(tokens)) true else false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment