Skip to content

Java Builders: URL builder with zero runtime dependencies

License

Notifications You must be signed in to change notification settings

mikaelhg/urlbuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jul 28, 2023
7395ba0 · Jul 28, 2023
Jul 28, 2023
Jul 28, 2023
Jul 28, 2023
Dec 6, 2022
May 5, 2017
Oct 13, 2013
Oct 12, 2015
Jul 28, 2023
May 7, 2017
Jul 28, 2023
Dec 6, 2022
Dec 6, 2022
Dec 6, 2022
Jul 28, 2023

Repository files navigation

Java URL builder

Build Status Coverage Status Maven Central

Create and modify URLs and URL parameters easily, with a builder class.

Builder instances are immutable, thread-safe and reusable. Every change creates a new instance.

UrlBuilder.fromString("http://www.google.com/")
    .addParameter("q", "charlie brown")
    .toString() == "http://www.google.com/?q=charlie+brown"

UrlBuilder.fromString("http://foo/h%F6pl%E4", "ISO-8859-1")
    .encodeAs("UTF-8")
    .toString() == "http://foo/h%C3%B6pl%C3%A4"

final UrlBuilder ub1 = UrlBuilder.empty()
    .withScheme("http")
    .withHost("www.example.com")
    .withPath("/")
    .addParameter("foo", "bar");

final java.net.URI uri1 = ub1.toUri();

try {
    final java.net.URI uri2 = ub1.toUriWithException();
} catch (java.net.URISyntaxException ex) {
    // handle the exception
}

final java.net.URL url1 = ub1.toUrl();

try {
    final java.net.URL url2 = ub1.toUrlWithException();
} catch (java.net.MalformedURLException ex) {
    // handle the exception
}

Todo:

  • More unit tests for corner cases. Please send in pull requests, your help is needed.

Use with Gradle:

implementation("io.mikael:urlbuilder:2.0.9")
implementation "io.mikael:urlbuilder:2.0.9"

Use with Maven:

<dependencies>
    <dependency>
        <groupId>io.mikael</groupId>
        <artifactId>urlbuilder</artifactId>
        <version>2.0.9</version>
    </dependency>
</dependencies>