com.github.dreamhead.moco.util.URLs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
package com.github.dreamhead.moco.util;
import com.google.common.base.Strings;
import java.net.MalformedURLException;
import java.net.URL;
import static com.github.dreamhead.moco.util.Preconditions.checkNotNullOrEmpty;
public final class URLs {
private static final String SEPARATOR = "/";
public static String toBase(final String baseUri) {
if (baseUri.endsWith(SEPARATOR)) {
return baseUri;
}
return baseUri + SEPARATOR;
}
public static String join(final String base, final String path) {
String joinPath = toJoinPath(path);
if (base.endsWith(SEPARATOR)) {
return base + joinPath;
}
if (joinPath.isEmpty()) {
return base;
}
return base + SEPARATOR + joinPath;
}
private static String toJoinPath(final String path) {
if (Strings.isNullOrEmpty(path)) {
return "";
}
if (path.startsWith(SEPARATOR)) {
return path.substring(1);
}
return path;
}
public static URL toUrl(final String url) {
try {
return new URL(checkNotNullOrEmpty(url, "URL should not be null"));
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
private URLs() {}
}