class PathElement {

   public static PathElement pathElement(String path) throws PatternSyntaxException
   {
      // logic based off resteasy for path templating
      String replacedCurlySegment = replaceEnclosedCurlyBraces(path);
      String[] split = TEMPLATE_PARAM_PATTERN.split(replacedCurlySegment);
      Matcher withPathParam = TEMPLATE_PARAM_PATTERN.matcher(replacedCurlySegment);

      int i = 0;
      StringBuilder buffer = new StringBuilder();
      if (i < split.length) buffer.append(split[i++]);

      int groupNumber = 1;
      List<Group> groups = new ArrayList<Group>();
      while (withPathParam.find())
      {
         String name = withPathParam.group(1);
         buffer.append("(");
         if (withPathParam.group(3) == null)
         {
            buffer.append(TEMPLATE_PARAM_DEFAULT_REGEX);
            groups.add(new Group(name, groupNumber++));
         }
         else
         {
            String expr = withPathParam.group(3);
            expr = recoverEnclosedCurlyBraces(expr);
            buffer.append(expr);
            groups.add(new Group(name, groupNumber++));
            groupNumber += groupCount(expr);
         }
         buffer.append(")");
         if (i < split.length) buffer.append(split[i++]);
      }

      return new PathElement(path, groups, buffer.toString());
   }

   public  PathElement(String value, List<Group> groups, String regex);

}