Skip to content

Instantly share code, notes, and snippets.

@contextfw
Created March 14, 2011 16:30
Show Gist options
  • Save contextfw/869408 to your computer and use it in GitHub Desktop.
Save contextfw/869408 to your computer and use it in GitHub Desktop.
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;
}
}
@ponzao
Copy link

ponzao commented Mar 14, 2011

Tein oman version tän inspaamana https://gist.github.com/869689. Noi mutable mapit on vähän mystisiä...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment