Skip to content

Commit 1212b84

Browse files
committed
Path segment codec APIs
1 parent 3369578 commit 1212b84

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
28+
package org.apache.hc.core5.net;
29+
30+
import java.nio.charset.Charset;
31+
import java.nio.charset.StandardCharsets;
32+
import java.util.Arrays;
33+
import java.util.List;
34+
35+
import org.apache.hc.core5.util.Args;
36+
37+
/**
38+
* URI path segment codec.
39+
*
40+
* @since 5.5
41+
*/
42+
public class PathSegmentCodec {
43+
44+
/**
45+
* Returns a list of URI path segments.
46+
*
47+
* @param s URI path component.
48+
* @param charset parameter charset.
49+
* @return list of segments.
50+
*/
51+
public static List<String> parsePathSegments(final CharSequence s, final Charset charset) {
52+
return URIBuilder.parsePath(s, charset);
53+
}
54+
55+
/**
56+
* Returns a list of URI path segments.
57+
*
58+
* @param s URI path component.
59+
* @return list of segments.
60+
*/
61+
public static List<String> parsePathSegments(final CharSequence s) {
62+
return parsePathSegments(s, StandardCharsets.UTF_8);
63+
}
64+
65+
/**
66+
* Returns a string consisting of joint encoded path segments.
67+
*
68+
* @param segments the segments.
69+
* @param charset parameter charset.
70+
* @return URI path component
71+
*/
72+
public static String formatSegments(final Iterable<String> segments, final Charset charset) {
73+
Args.notNull(segments, "Segments");
74+
final StringBuilder buf = new StringBuilder();
75+
URIBuilder.formatPath(buf, segments, false, charset);
76+
return buf.toString();
77+
}
78+
79+
/**
80+
* Returns a string consisting of joint encoded path segments.
81+
*
82+
* @param segments the segments.
83+
* @return URI path component
84+
*/
85+
public static String formatSegments(final String... segments) {
86+
return formatSegments(Arrays.asList(segments), StandardCharsets.UTF_8);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)