Skip to content

Instantly share code, notes, and snippets.

@xrigau
Created May 28, 2015 14:47
Show Gist options
  • Save xrigau/f24dcd4323305f3dfd48 to your computer and use it in GitHub Desktop.
Save xrigau/f24dcd4323305f3dfd48 to your computer and use it in GitHub Desktop.
Custom Url implementation
package com.foo.bar;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
public class Url implements Serializable {
private final URL url;
public static Url from(String inputUrl) {
try {
URL foo = new URL(inputUrl);
return new Url(foo);
} catch (MalformedURLException e) {
throw new MalformedException(e, inputUrl);
}
}
Url(URL url) {
this.url = url;
}
@Override
public final String toString() {
return url.toString();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
Url otherUrl = (Url) other;
return url.toString().equals(otherUrl.url.toString());
}
@Override
public int hashCode() {
return url.toString().hashCode();
}
public static class MalformedException extends RuntimeException {
public MalformedException(Exception e, String url) {
super(url + " is malformed.", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment