Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- My:
- import scala.collection.mutable.ListBuffer
- class Router {
- class Info(Route: String, Http: String, fun: ()=>String)
- {
- var route = Route
- var http = Http
- var message=fun()
- override def equals(obj: Any): Boolean ={
- obj match{
- case obj: Info => route.equals(obj.route) && http.equals(obj.http)
- case _ => false
- }
- }
- override def hashCode: Int = {
- var result = 1
- result = 31 * result + (if (route == null) 0 else route.hashCode)
- result = 31 * result + (if (http == null) 0 else http.hashCode)
- result
- }
- override def toString: String = message
- }
- var list = new ListBuffer[Info]
- def bind(route: String, http: String, fun: ()=>String):Unit=
- {
- if(list.contains(new Info(route, http, ()=>"")))
- {
- list.update(list.indexOf(new Info(route, http, fun)), new Info(route, http, fun))
- }
- else
- list.append(new Info(route, http, fun))
- }
- def runRequest(route:String, http: String):String={
- if(list.contains(new Info(route, http, ()=>""))){
- list.filter(x=>x.equals(new Info(route, http, ()=>""))).head.toString
- }
- else
- "Error 404: Not Found"
- }
- }
- Other:
- class Router {
- private val routes = collection.mutable.Map[(String, String), () => String]()
- def bind(url: String, method: String, action: () => String): Unit =
- routes.update((url, method), action)
- def runRequest(url: String, method: String): String =
- routes.getOrElse((url, method), () => "Error 404: Not Found")()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement