GlideUrl.java revision c95999a11b7c380a6711395482e16362039e5b31
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        if (url == null) {
20            throw new IllegalArgumentException("URL must not be null!");
21        }
22        this.url = url;
23        stringUrl = null;
24    }
25
26    public GlideUrl(String url) {
27        if (TextUtils.isEmpty(url)) {
28            throw new IllegalArgumentException("String url must not be empty or null: " + url);
29        }
30        this.stringUrl = url;
31        this.url = null;
32    }
33
34    public URL toURL() throws MalformedURLException {
35        if (url == null) {
36            url = new URL(stringUrl);
37        }
38        return url;
39    }
40
41    @Override
42    public String toString() {
43        if (TextUtils.isEmpty(stringUrl)) {
44            stringUrl = url.toString();
45        }
46        return stringUrl;
47    }
48
49    @Override
50    public boolean equals(Object o) {
51        if (this == o) {
52            return true;
53        }
54        if (o == null || getClass() != o.getClass()) {
55            return false;
56        }
57
58        GlideUrl glideUrl = (GlideUrl) o;
59        if (stringUrl != null) {
60            if (glideUrl.stringUrl != null) {
61                return stringUrl.equals(glideUrl.stringUrl);
62            } else {
63                return stringUrl.equals(glideUrl.url.toString());
64            }
65        } else {
66            if (glideUrl.stringUrl != null) {
67                return url.toString().equals(glideUrl.stringUrl);
68            } else {
69                return url.equals(glideUrl.url);
70            }
71        }
72    }
73
74    @Override
75    public int hashCode() {
76        if (stringUrl != null) {
77            return stringUrl.hashCode();
78        } else {
79            return url.toString().hashCode();
80        }
81    }
82}
83