Class URIReferenceBuilder
- java.lang.Object
-
- org.czeal.urireference.URIReferenceBuilder
-
public class URIReferenceBuilder extends Object
A builder class for constructing URI references.This class provides a fluent API to build a
URIReferenceobject incrementally by setting its various components such asscheme,userinfo,host,port,path,query, andfragment.This builder supports both absolute and relative URI references.
Examples:
//--------------------------------------------------------------------------- // Build a URI reference. //--------------------------------------------------------------------------- URIReference uriRef = new URIReferenceBuilder() .setScheme("http") .setHost("example.com") .setPath("/a/b/c") .appendQueryParam("k1", "v1") .build(); System.out.println(uriRef.toString()); // "http://example.com/a/b/c?k1=v1" System.out.println(uriRef.getScheme()); // "http" System.out.println(uriRef.getAuthority()); // "example.com" System.out.println(uriRef.getUserinfo()); // null System.out.println(uriRef.getAuthority().getHost().getType()); // "REGNAME" System.out.println(uriRef.getAuthority().getHost().getValue()); // "example.com" System.out.println(uriRef.getPort()); // -1 System.out.println(uriRef.getPath()); // "/a/b/c" System.out.println(uriRef.getQuery()); // "k1=v1" System.out.println(uriRef.getFragment()); // null //--------------------------------------------------------------------------- // Build a URI reference from another URI reference. //--------------------------------------------------------------------------- URIReference uriRef = URIReferenceBuilder .fromURIReference("http://example.com/a/b/c?k1=v1") .appendPath("d", "e", "f") .appendQueryParam("k2", "v2") .build(); System.out.println(uriRef.toString()); // "http://example.com/a/b/c/d/e/f?k1=v1&k2=v2" System.out.println(uriRef.getScheme()); // "http" System.out.println(uriRef.getAuthority().toString()); // "example.com" System.out.println(uriRef.getUserinfo()); // null System.out.println(uriRef.getAuthority().getHost().getType()); // "REGNAME" System.out.println(uriRef.getAuthority().getHost().getValue()); // "example.com" System.out.println(uriRef.getPort()); // -1 System.out.println(uriRef.getPath()); // "/a/b/c/d/e/f" System.out.println(uriRef.getQuery()); // "k1=v1&k2=&v2" System.out.println(uriRef.getFragment()); // null- Author:
- Hideki Ikeda
- See Also:
- RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
-
-
Constructor Summary
Constructors Constructor Description URIReferenceBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description URIReferenceBuilderappendPathSegments(String... segment)Appends a path segment.URIReferenceBuilderappendQueryParam(String key, String value)Appends a new query parameter to the resultant URI reference.URIReferencebuild()Builds a URI reference.static URIReferenceBuilderfromURIReference(String uriRef)Creates aURIReferenceBuilderinstance with a given string representing a URI reference.static URIReferenceBuilderfromURIReference(URIReference uriRef)Creates aURIReferenceBuilderinstance with a given string representing a URI reference.URIReferenceBuilderremoveQueryParam(String key)Removes the query parameter(s) specified by a key.URIReferenceBuilderreplaceQueryParam(String key, String value)Replaces the value(s) of the query parameter(s) specified by the key with a new value.URIReferenceBuildersetAuthorityRequired(boolean authorityRequired)Determines whether or not the authority is required in the resultant URI reference.URIReferenceBuildersetCharset(Charset charset)Sets the charset used in the resultant URI reference.URIReferenceBuildersetFragment(String fragment)Sets the fragment.URIReferenceBuildersetHost(String host)Sets the host.URIReferenceBuildersetPath(String path)Sets the path.URIReferenceBuildersetPort(int port)Sets the port.URIReferenceBuildersetQuery(String query)Sets the query.URIReferenceBuildersetScheme(String scheme)Sets the scheme.URIReferenceBuildersetUserinfo(String userinfo)Sets the userinfo.URIReferenceBuilderuriRef(URIReference uriRef)Sets information about a givenURIReferenceBuilderinstance.
-
-
-
Method Detail
-
fromURIReference
public static URIReferenceBuilder fromURIReference(String uriRef)
Creates aURIReferenceBuilderinstance with a given string representing a URI reference. This method copies the following information to the created instance:- charset
- scheme
- userinfo
- host
- port
- query
- fragment
Note that this method works as if invoking it were equivalent to evaluating the following expression:
fromURIReference(URIReference.parse(uriRef))- Parameters:
uriRef- A string representing a URI reference.- Returns:
- A
URIReferenceBuilderinstance initialized with the given URI reference information.
-
fromURIReference
public static URIReferenceBuilder fromURIReference(URIReference uriRef)
Creates aURIReferenceBuilderinstance with a given string representing a URI reference. This method copies the following information to the created instance:- charset
- scheme
- userinfo
- host
- port
- query
- fragment
Note that this method works as if invoking it were equivalent to evaluating the following expression:
new URIReferenceBuilder().uriRef(uriRef)- Parameters:
uriRef- AURIReferenceinstance.- Returns:
- A
URIReferenceBuilderinstance initialized with the given URI reference information.
-
uriRef
public URIReferenceBuilder uriRef(URIReference uriRef)
Sets information about a givenURIReferenceBuilderinstance. Specifically, it copies the following information from the givenURIReferenceBuilderinstance to this instance.- charset
- scheme
- userinfo
- host
- port
- query
- fragment
- Parameters:
uriRef- AURIReferenceinstance.- Returns:
thisobject.
-
setCharset
public URIReferenceBuilder setCharset(Charset charset)
Sets the charset used in the resultant URI reference.This method replaces existing charset value with the given value.
- Parameters:
charset- The charset.- Returns:
thisobject.
-
setScheme
public URIReferenceBuilder setScheme(String scheme)
Sets the scheme.This method replaces existing
schemevalue with the given value.- Parameters:
scheme- The scheme of the resultant URI reference. Specifyingnullfor this property unsets the scheme.- Returns:
thisobject.
-
setUserinfo
public URIReferenceBuilder setUserinfo(String userinfo)
Sets the userinfo.This method replaces existing
userinfovalue with the given value.- Parameters:
userinfo- The userinfo of the resultant URI reference. Specifyingnullfor this property unsets the userinfo.- Returns:
thisobject.
-
setHost
public URIReferenceBuilder setHost(String host)
Sets the host.This method replaces existing
hostvalue with the given value.- Parameters:
host- The host of the resultant URI reference. Specifyingnullfor this property unsets the host.- Returns:
thisobject.
-
setPort
public URIReferenceBuilder setPort(int port)
Sets the port.This method replaces existing
portvalue with the given value.- Parameters:
port- The port of the resultant URI reference. Specifyingnullfor this property unsets the port.- Returns:
thisobject.
-
setPath
public URIReferenceBuilder setPath(String path)
Sets the path.This method replaces existing
pathvalue with the given value.- Parameters:
path- The path of the resultant URI reference. Specifyingnullfor this property unsets the path.- Returns:
thisobject.
-
setQuery
public URIReferenceBuilder setQuery(String query)
Sets the query.This method replaces the existing
queryvalue with the given value.- Parameters:
query- The query of the resultant URI reference. Specifyingnullfor this property unsets the query.- Returns:
thisobject.
-
setFragment
public URIReferenceBuilder setFragment(String fragment)
Sets the fragment.This method replaces existing
fragmentvalue with the given value.- Parameters:
fragment- The fragment of the resultant URI reference. Specifyingnullfor this property unsets the fragment.- Returns:
thisobject.
-
setAuthorityRequired
public URIReferenceBuilder setAuthorityRequired(boolean authorityRequired)
Determines whether or not the authority is required in the resultant URI reference.- Parameters:
authorityRequired- Iftrue, the authority part is present in the resultant URI reference; otherwise, it will be omitted.- Returns:
thisobject.
-
appendQueryParam
public URIReferenceBuilder appendQueryParam(String key, String value)
Appends a new query parameter to the resultant URI reference.- Parameters:
key- The key of the new query parameter.value- The value of the new query parameter.- Returns:
thisobject.
-
replaceQueryParam
public URIReferenceBuilder replaceQueryParam(String key, String value)
Replaces the value(s) of the query parameter(s) specified by the key with a new value.- Parameters:
key- The key of the query parameter(s) whose value(s) is to be replaced.value- A new value of the new query parameter.- Returns:
thisobject.
-
removeQueryParam
public URIReferenceBuilder removeQueryParam(String key)
Removes the query parameter(s) specified by a key.- Parameters:
key- The key of the query parameter(s) to be removed.- Returns:
thisobject.
-
appendPathSegments
public URIReferenceBuilder appendPathSegments(String... segment)
Appends a path segment.- Parameters:
segment- A path segment.- Returns:
thisobject.
-
build
public URIReference build()
Builds a URI reference.- Returns:
- An
URIReferenceobject representing the resultant URI reference.
-
-