All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.io.KiwiPaths Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.io;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Resources;
import lombok.experimental.UtilityClass;
import org.kiwiproject.net.UncheckedURISyntaxException;

import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Static utilities related to {@link Path} instances.
 */
@UtilityClass
public class KiwiPaths {

    /**
     * This wraps Guava's {@link Resources#getResource(String)} and converts it to a {@link Path}
     *
     * @param resourceName the resource name
     * @return the {@link Path} of the given resource name
     * @see Resources#getResource(String)
     */
    @SuppressWarnings("UnstableApiUsage") // because Guava's Resources has been around for a long time
    public static Path pathFromResourceName(String resourceName) {
        try {
            return Paths.get(Resources.getResource(resourceName).toURI());
        } catch (URISyntaxException e) {
            throw newUncheckedException(resourceName, e);
        }
    }

    @VisibleForTesting
    static UncheckedURISyntaxException newUncheckedException(String resourceName, URISyntaxException e) {
        var message = "Cannot convert to URI: " + resourceName;
        return new UncheckedURISyntaxException(message, e);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy