Created
October 12, 2011 08:34
-
-
Save amir343/1280657 to your computer and use it in GitHub Desktop.
Source that is used for Injectors and Extractros in Scala blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Firstname(name:String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] = { | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object IPAddresses { | |
def unapplySeq(ips:String):Option[Seq[String]] = { | |
Some(ips split ",") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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