Skip to content

Instantly share code, notes, and snippets.

@luizamboni
Created March 8, 2016 01:49
Show Gist options
  • Save luizamboni/989082173d0a19e27120 to your computer and use it in GitHub Desktop.
Save luizamboni/989082173d0a19e27120 to your computer and use it in GitHub Desktop.
Eval Route class to Eval and contruct paths for Routes
import java.util.Arrays;
public class Routes {
public static void main(String[] args)
{
Route editRoute = new Route("admin/user/edit/:id").withMethod("GET").dispatch(ExampleController.class).withAction("edit");
System.out.println(editRoute);
System.out.println(editRoute.match("admin/user/edit/34"));
System.out.println(editRoute.match("admin/user/show/12"));
System.out.println(editRoute.mount("12"));
}
}
public class ExampleController{
public void edit(){
}
}
public class Route{
protected String path;
protected String method;
protected Class controller;
protected String action;
public Route(String path){
this.path = path;
}
public Route withMethod(String method){
this.method = method;
return this;
}
public Route dispatch(Class controller){
this.controller = controller;
return this;
}
public String mount(String...values){
String mountedPath = this.path;
for(String value : Arrays.asList(values)){
System.out.println(value);
mountedPath = mountedPath.replaceFirst(":.*/?",value);
}
return mountedPath;
}
public Route withAction(String actionName){
this.action = actionName;
return this;
}
@Override
public String toString(){
return method + " "+ path + " " +controller.toString()+ "#" + action;
}
public Boolean match(String path){
System.out.println(path);
System.out.println(this.path);
System.out.println(this.path.indexOf(":id"));
return this.path.regionMatches(0,path,0,this.path.indexOf(":id") );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment