scala.util.Sorting Scala Examples
The following examples show how to use scala.util.Sorting.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Request.scala From peregrine with Apache License 2.0 | 5 votes |
package io.peregrine import com.google.common.base.Splitter import com.twitter.finagle.http.{Request => FinagleRequest, RequestProxy} import scala.collection.JavaConversions._ import scala.collection.mutable import scala.util.Sorting object Request { def apply() = new Request(FinagleRequest("/")) def apply(path: String) = new Request(FinagleRequest(path)) } class Request(val request: FinagleRequest) extends RequestProxy { var multiParams: mutable.Map[String, MultipartItem] = mutable.Map.empty val routeParams: mutable.Map[String, String] = mutable.Map.empty var error : Option[Throwable] = None def accepts: Seq[ContentType] = { val accept = this.headers().get("Accept") if (accept != null) { val acceptParts = Splitter.on(',').split(accept).toArray Sorting.quickSort(acceptParts)(AcceptOrdering) val seq = acceptParts.map { xs => val part = Splitter.on(";q=").split(xs).toArray.head ContentType(part).getOrElse(new ContentType.All) }.toSeq seq } else { Seq.empty[ContentType] } } def param(key: String): Option[String] = { routeParams.get(key) orElse params.get(key) } } object AcceptOrdering extends Ordering[String] { def getWeight(str: String): Double = { val parts = Splitter.on(';').split(str).toArray if (parts.length < 2) { 1.0 } else { try { Splitter.on("q=").split(parts(1)).toArray.last.toFloat } catch { case e: java.lang.NumberFormatException => 1.0 } } } def compare(a: String, b: String): Int = { getWeight(b) compare getWeight(a) } }
Example 2
Source File: RequestSpec.scala From peregrine with Apache License 2.0 | 5 votes |
package io.peregrine import com.google.common.base.Splitter import com.twitter.finagle.http.{Request => FinagleRequest, Version} import org.jboss.netty.buffer.ChannelBuffers import org.jboss.netty.handler.codec.http.{DefaultHttpRequest, HttpMethod} import scala.collection.JavaConversions._ import scala.util.Sorting class RequestSpec extends ShouldSpec { "AcceptOrdering" should "understand accept header ordering" in { val accept = "application/xhtml+xml;q=2,application/xml;q=0.9,**;q=0.8") parts(1) should equal("application/xml;q=0.9") parts(0) should equal("application/xhtml+xml;q=2") } "Url encoded params" should "work with PUT even with query strings" in { val nettyRequest = new DefaultHttpRequest(Version.Http11, HttpMethod.PUT, "/params?q=hi") nettyRequest.setContent(ChannelBuffers.wrappedBuffer("test=foo".getBytes)) nettyRequest.headers().add("Content-Type", "application/x-www-form-urlencoded") val finagleRequest = FinagleRequest(nettyRequest) val request = new Request(finagleRequest) request.params.get("test") should equal(Some("foo")) request.params.get("q") should equal(Some("hi")) } }