/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

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

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class PathUtils {
    
    private PathUtils() {
        /* utility class */
    }
    
    public static Path fromURL(URL url) {
        try {
            return Paths.get(url.toURI());
        } catch (URISyntaxException cause) {
            throw new IllegalArgumentException(cause);
        }
    }
    
    public static String getExtension(String path) {
        return getExtension(Paths.get(path));
    }
    
    public static String getExtension(Path path) {
        String name = path.getFileName().toString();
        int index = name.lastIndexOf(".");
        return (-1==index) ? "" : name.substring(index);
    }
    
    public static Path changeExtension(Path path, String extension) {
        return changeExtension(path.toString(), extension);
    }
    
    public static Path changeExtension(String path, String extension) {
        int index = path.lastIndexOf(".");
        if(-1==index) {
            return Paths.get(path + extension);
        } else {
            return Paths.get(path.substring(0,index) + extension);
        }
    }
    
    public static String getSimpleName(String path) {
        return getSimpleName(Paths.get(path));
    }
    
    public static String getSimpleName(Path path) {
        String name = path.getFileName().toString();
        int index = name.lastIndexOf(".");
        return index<0 ? name : name.substring(0, index);
    }
}
