Package com.github.mizool.core
Class UrlRef
- java.lang.Object
-
- com.github.mizool.core.UrlRef
-
public final class UrlRef extends Object
Safely stores a reference to a URL and allows conversion toURIandURLat any time.
Features-
Unlike
URL, this class ensures that invocations ofequals()andhashCode()never trigger network requests, making this class suitable for collections. For details, see the SonarQube rule "URL.hashCode" and "URL.equals" should be avoided. -
When constructing a
UrlRefinstance, it is ensured that it later can be converted to both aURIand aURL. -
Creating a
UrlRefnever throws checked exceptions. -
Unlike
URI, anUrlRefinstance can be used to build a child URI - just likeURLwith itstwo-arg constructor.
-
Unlike
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)inthashCode()UrlRefresolve(@NonNull String spec)Creates a newUrlRefby combining the URL referenced by this instance (the "context") with the given spec.StringtoString()URItoUri()URLtoUrl()
-
-
-
Constructor Detail
-
UrlRef
public UrlRef(@NonNull @NonNull URI uri)- Throws:
NullPointerException- ifuriisnullIllegalArgumentException- if the resultingUrlRefwould cause exceptions when converted toURL
-
UrlRef
public UrlRef(@NonNull @NonNull URL url)- Throws:
NullPointerException- ifurlisnullIllegalArgumentException- if the resultingUrlRefwould cause exceptions when converted toURI
-
UrlRef
public UrlRef(@NonNull @NonNull String spec)- Throws:
NullPointerException- ifspecisnullIllegalArgumentException- if the resultingUrlRefwould cause exceptions when converted toURIorURL
-
-
Method Detail
-
resolve
public UrlRef resolve(@NonNull @NonNull String spec)
Creates a newUrlRefby combining the URL referenced by this instance (the "context") with the given spec.
The new URL is constructed as if usingURL(URL, String). Note that the result differs based on whether the context has a trailing slash, the spec has a leading slash, both, or none.- Throws:
NullPointerException- ifspecisnullIllegalArgumentException- if the resultingUrlRefwould cause exceptions when converted toURIorURL
-
toUri
public URI toUri()
-
toUrl
public URL toUrl()
-
-