private String buildPath(boolean fromEncoded) {
StringBuilder sb = new StringBuilder();
Iterator<PathSegment> iter = paths.iterator();
while (iter.hasNext()) {
PathSegment ps = iter.next();
String p = ps.getPath();
if (p.length() != 0 || !iter.hasNext()) {
p = fromEncoded ? new URITemplate(p).encodeLiteralCharacters() : p;
if (sb.length() == 0 && leadingSlash) {
sb.append('/');
} else if (!p.startsWith("/") && sb.length() > 0) {
sb.append('/');
}
sb.append(p);
if (iter.hasNext()) {
buildMatrix(sb, ps.getMatrixParameters(), fromEncoded);
}
}
}
buildMatrix(sb, matrix, fromEncoded);
return sb.toString();