final <R extends Route> R add(R route) throws MalformedRouteException { if (route == null) { throw new NullPointerException("No null route accepted"); } if (((Route) route).parent != null) { throw new IllegalArgumentException("No route with an existing parent can be accepted"); } // LinkedList<Param> ancestorParams = new LinkedList<Param>(); findAncestorOrSelfParams(ancestorParams); LinkedList<Param> descendantParams = new LinkedList<Param>(); for (Param param : ancestorParams) { ((Route) route).findDescendantOrSelfParams(param.name, descendantParams); if (descendantParams.size() > 0) { throw new MalformedRouteException("Duplicate parameter " + param.name); } } // if (route instanceof PatternRoute || route instanceof SegmentRoute) { children = Tools.appendTo(children, route); terminal = false; ((Route) route).parent = this; } else { throw new IllegalArgumentException("Only accept segment or pattern routes"); } // return route; }
private void findAncestorOrSelfParams(List<Param> params) { findParams(params); if (parent != null) { parent.findAncestorOrSelfParams(params); } }