bigbluebutton-Github/akka-bbb-apps/scala/Test2.sc

77 lines
3.8 KiB
Python
Raw Normal View History

2015-10-05 23:23:06 +08:00
import scala.collection.immutable.StringOps
2015-10-29 01:05:40 +08:00
import java.net.URLEncoder
import scala.collection._
2017-04-29 05:20:46 +08:00
2015-10-05 23:23:06 +08:00
object Test2 {
2017-04-29 05:20:46 +08:00
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
2015-11-17 15:11:14 +08:00
2017-04-29 05:20:46 +08:00
val userId = new StringOps("abc_12") //> userId : scala.collection.immutable.StringOps = abc_12
val s2 = userId.split('_') //> s2 : Array[String] = Array(abc, 12)
val s1 = if (s2.length == 2) s2(0) else userId //> s1 : Comparable[String] = abc
2015-10-29 01:05:40 +08:00
def sortParam(params: mutable.Map[String, String]):SortedSet[String] = {
collection.immutable.SortedSet[String]() ++ params.keySet
} //> sortParam: (params: scala.collection.mutable.Map[String,String])scala.collec
2017-04-29 05:20:46 +08:00
//| tion.SortedSet[String]
2015-10-29 01:05:40 +08:00
def createBaseString(params: mutable.Map[String, String]): String = {
val csbuf = new StringBuffer()
var keys = sortParam(params)
2017-04-29 05:20:46 +08:00
2015-10-29 01:05:40 +08:00
var first = true;
for (key <- keys) {
for (value <- params.get(key)) {
if (first) {
first = false;
} else {
csbuf.append("&");
}
2017-04-29 05:20:46 +08:00
2015-10-29 01:05:40 +08:00
csbuf.append(key);
csbuf.append("=");
csbuf.append(value);
}
}
2017-04-29 05:20:46 +08:00
2015-10-29 01:05:40 +08:00
return csbuf.toString();
} //> createBaseString: (params: scala.collection.mutable.Map[String,String])Strin
2017-04-29 05:20:46 +08:00
//| g
2015-10-29 01:05:40 +08:00
def urlEncode(s: String): String = {
URLEncoder.encode(s, "UTF-8");
2017-04-29 05:20:46 +08:00
} //> urlEncode: (s: String)String
2015-10-29 01:05:40 +08:00
val baseString = "fullName=User+4621018&isBreakout=true&meetingID=random-1853792&password=mp&redirect=true"
//> baseString : String = fullName=User+4621018&isBreakout=true&meetingID=rand
2017-04-29 05:20:46 +08:00
//| om-1853792&password=mp&redirect=true
2015-10-29 01:05:40 +08:00
val params = new collection.mutable.HashMap[String, String]
2017-04-29 05:20:46 +08:00
//> params : scala.collection.mutable.HashMap[String,String] = Map()
2015-10-29 01:05:40 +08:00
params += "fullName" -> urlEncode("User 4621018")
2017-04-29 05:20:46 +08:00
//> res0: Test2.params.type = Map(fullName -> User+4621018)
2015-10-29 01:05:40 +08:00
params += "isBreakout" -> urlEncode("true") //> res1: Test2.params.type = Map(fullName -> User+4621018, isBreakout -> true)
2017-04-29 05:20:46 +08:00
//|
2015-10-29 01:05:40 +08:00
params += "meetingID" -> urlEncode("random-1853792")
//> res2: Test2.params.type = Map(fullName -> User+4621018, isBreakout -> true,
2017-04-29 05:20:46 +08:00
//| meetingID -> random-1853792)
2015-10-29 01:05:40 +08:00
params += "password" -> urlEncode("mp") //> res3: Test2.params.type = Map(fullName -> User+4621018, isBreakout -> true,
2017-04-29 05:20:46 +08:00
//| meetingID -> random-1853792, password -> mp)
2015-10-29 01:05:40 +08:00
params += "redirect" -> urlEncode("true") //> res4: Test2.params.type = Map(fullName -> User+4621018, isBreakout -> true,
2017-04-29 05:20:46 +08:00
//| meetingID -> random-1853792, redirect -> true, password -> mp)
2015-10-29 01:05:40 +08:00
val keys = sortParam(params) //> keys : scala.collection.SortedSet[String] = TreeSet(fullName, isBreakout,
2017-04-29 05:20:46 +08:00
//| meetingID, password, redirect)
2015-10-29 01:05:40 +08:00
val result = createBaseString(params) //> result : String = fullName=User+4621018&isBreakout=true&meetingID=random-1
2017-04-29 05:20:46 +08:00
//| 853792&password=mp&redirect=true
val between = Set("xab", "bc")
val u2 = Set("bc", "xab")
val u3 = u2 + "zxc"
val foo = between subsetOf(u2)
val id = between.toSeq.sorted.mkString("-")
2015-10-05 23:23:06 +08:00
}