Created
March 14, 2011 16:30
-
-
Save contextfw/869408 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import scala.collection.mutable.MultiMap | |
import java.net.URLEncoder | |
import scala.collection.mutable.{HashMap, StringBuilder, HashSet} | |
import scala.collection.mutable.StringBuilder; | |
class LinkBuilder(path : String) { | |
private var attributes = new collection.mutable.HashMap[String, collection.mutable.Set[String]]() with collection.mutable.MultiMap[String, String]; | |
override def toString() : String = { | |
var url : StringBuilder = new StringBuilder(path); | |
var separator = "?"; | |
attributes.foreach { case (name,value) => | |
value.foreach { v => | |
url append separator append name append "=" append | |
URLEncoder.encode(v, "utf-8"); | |
separator = "&"; | |
} | |
} | |
url.toString | |
} | |
def +(name : String, value : String) : LinkBuilder = { | |
if (value != null) { | |
val entry = new HashSet[String] | |
entry += value; | |
if (attributes contains name) { | |
attributes(name).foreach { v => | |
entry += v; | |
} | |
} | |
attributes += name -> entry; | |
} | |
this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tein oman version tän inspaamana https://gist.github.com/869689. Noi mutable mapit on vähän mystisiä...