1package com.bumptech.glide.load.model; 2 3import android.text.TextUtils; 4 5import java.net.MalformedURLException; 6import java.net.URL; 7 8/** 9 * This is a simple wrapper for strings representing http/https urls. new URL() is an excessively expensive operation 10 * that may be unnecessary if the class loading the image from the url doesn't actually require a URL object. 11 * 12 * Users wishing to replace the class for handling urls must register a factory using GlideUrl. 13 */ 14public class GlideUrl { 15 private String stringUrl; 16 private URL url; 17 18 public GlideUrl(URL url) { 19 this.url = url; 20 stringUrl = null; 21 } 22 23 public GlideUrl(String url) { 24 this.stringUrl = url; 25 this.url = null; 26 } 27 28 public URL toURL() throws MalformedURLException { 29 if (url == null) { 30 url = new URL(stringUrl); 31 } 32 return url; 33 } 34 35 @Override 36 public String toString() { 37 if (TextUtils.isEmpty(stringUrl)) { 38 stringUrl = url.toString(); 39 } 40 return stringUrl; 41 } 42 43 @Override 44 public boolean equals(Object o) { 45 if (this == o) { 46 return true; 47 } 48 if (o == null || getClass() != o.getClass()) { 49 return false; 50 } 51 52 GlideUrl glideUrl = (GlideUrl) o; 53 if (stringUrl != null) { 54 if (glideUrl.stringUrl != null) { 55 return stringUrl.equals(glideUrl.stringUrl); 56 } else { 57 return stringUrl.equals(glideUrl.url.toString()); 58 } 59 } else { 60 if (glideUrl.stringUrl != null) { 61 return url.toString().equals(glideUrl.stringUrl); 62 } else { 63 return url.equals(glideUrl.url); 64 } 65 } 66 } 67 68 @Override 69 public int hashCode() { 70 if (stringUrl != null) { 71 return stringUrl.hashCode(); 72 } else { 73 return url.toString().hashCode(); 74 } 75 } 76} 77