Created
May 28, 2015 14:47
-
-
Save xrigau/f24dcd4323305f3dfd48 to your computer and use it in GitHub Desktop.
Custom Url implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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