public PathPatternBuilder apply( Map<String, String> parameters, Map<String, String> staticParameterValues) { PathPatternBuilder result = new PathPatternBuilder(); for (PathSegmentBuilder pathSegment : pathSegmentBuilders) { result.append(pathSegment.apply(parameters, staticParameterValues)); } return result; }
private static void parse(PathPatternBuilder builder, String pattern) { boolean requireNameStart = false; StringBuilder nameBuilder = null; boolean allowSlashes = false; StringBuilder segmentBuilder = new StringBuilder(); for (int i = 0, len = pattern.length(); i < len; ++i) { char c = pattern.charAt(i); if (nameBuilder != null) { if (isValidNameChar(c)) { nameBuilder.append(c); } else { builder.addParameterName(segmentBuilder, nameBuilder, allowSlashes); segmentBuilder = new StringBuilder(); nameBuilder = null; --i; } } else if (c == PARAMETER_WITHOUT_SLASHES_PREFIX) { allowSlashes = false; requireNameStart = true; } else if (c == PARAMETER_WITH_SLASHES_PREFIX) { allowSlashes = true; requireNameStart = true; } else { segmentBuilder.append(c); } // peek ahead at the next name character and make sure there can be a valid name if (requireNameStart) { if (i == len - 1) { throw new IllegalArgumentException( "Invalid pattern: expecting name, found end of pattern"); } else { char d = pattern.charAt(i + 1); if (!isValidNameChar(d)) { throw new IllegalArgumentException( "Invalid pattern: expecting name, found '" + pattern.substring(i + 1) + "' at index " + i); } else { requireNameStart = false; nameBuilder = new StringBuilder(); nameBuilder.append(d); ++i; } } } } builder.addParameterName(segmentBuilder, nameBuilder, allowSlashes); }
@SuppressWarnings("unchecked") @Override public PathPatternBuilder clone() { try { PathPatternBuilder result = (PathPatternBuilder) super.clone(); result.pathSegmentBuilders = new ArrayList<PathSegmentBuilder>(pathSegmentBuilders.size()); for (PathSegmentBuilder parameter : pathSegmentBuilders) { result.pathSegmentBuilders.add(parameter.clone()); } result.parameterNames = (HashSet<String>) parameterNames.clone(); return result; } catch (CloneNotSupportedException ex) { throw new Error(ex); } }
/** Appends a pattern. */ public PathPatternBuilder append(String pattern) { PathPatternBuilder.parse(this, pattern); return this; }