View Javadoc

1   package org.varienaja.util.coverart;
2   
3   import java.io.Serializable;
4   import java.net.URI;
5   import java.net.URISyntaxException;
6   
7   public class CoverArtSearchResult implements Comparable<CoverArtSearchResult>, Serializable {
8   
9   	private static final long serialVersionUID = 2007120801L;
10  	
11  	private URI _uri;
12  	private int _width;
13  	private int _height;
14  	private int _size;
15  	
16  	public CoverArtSearchResult(URI uri, int w, int h, int size) {
17  		_uri = uri;
18  		_width = w;
19  		_height = h;
20  		_size = size;
21  	}
22  	
23  	public CoverArtSearchResult(String uri, int w, int h, int size) throws URISyntaxException {
24  		_uri = new URI(uri);
25  		_width = w;
26  		_height = h;
27  		_size = size;
28  	}
29  	
30  	public int getWidth() {
31  		return _width;
32  	}
33  	
34  	public int getHeight() {
35  		return _height;
36  	}
37  	
38  	public int getSize() {
39  		return _size;
40  	}
41  	
42  	public URI getURI() {
43  		return _uri;
44  	}
45  	
46  	/**
47  	 * Setter for the URI.
48  	 */
49  	public void setURI(URI uri) {
50  		_uri = uri;
51  	}
52  	
53  	public String toString() {
54  		return _width+"x"+_height+" ("+_size+") "+_uri.toString();
55  	}
56  	
57  	public boolean equals(Object o) {
58  		if (o instanceof CoverArtSearchResult) {
59  			CoverArtSearchResult test = (CoverArtSearchResult) o;
60  			
61  			return getWidth()==test.getWidth() &&
62  					getHeight()==test.getHeight() &&
63  					getSize()==test.getSize() &&
64  					(getURI()==null ? test.getURI()==null : getURI().equals(test.getURI()));
65  		} else {
66  			return false;
67  		}
68  	}
69  	
70  	public int hashCode() {
71  		return 31*(1+getWidth())*(1+getHeight())*(1+getSize());
72  	}
73  	
74  	public int compareTo(CoverArtSearchResult test) {
75  		//if (this>test) return -1 (reverse-sorting)
76  		
77  		//Special cases if getHeight()==0, which would result in NaN-outcomes in the division below.
78  		if (getHeight()==0) {
79  			if (test.getHeight()>0) {
80  				return 1;
81  			} else {
82  				return 0;
83  			}
84  		} else {
85  			if (test.getHeight()==0) {
86  				if (getHeight()>0) {
87  					return -1;
88  				} else {
89  					return 0;
90  				}
91  			}
92  		}
93  		
94  		double myRatio = Math.abs(1.0 - (double) getWidth()/(double) getHeight());
95  		double otherRatio = Math.abs(1.0 - (double)test.getWidth()/(double)test.getHeight());
96  		
97  		if (Math.abs(myRatio-otherRatio)<0.05) { //Ratio comparable, make decision on size
98  			int mySize = getWidth()*getHeight();
99  			int otherSize = test.getWidth()*test.getHeight();
100 			if (mySize>otherSize) {
101 				return -1;
102 			} else {
103 				if (mySize<otherSize) {
104 					return 1;
105 				} else { //Make decision on filesize
106 					if (getSize()>test.getSize()) {
107 						return -1;
108 					} else {
109 						if (getSize()<test.getSize()) {
110 							return 1;
111 						} else { //Everything equal
112 							return 0;
113 						}
114 					}
115 				}
116 			}
117 		} else { //Make decision on ratio: most square image wins
118 			if (myRatio<otherRatio) { //I win
119 				return -1;
120 			} else { //Other wins
121 				return 1;
122 			}
123 		}
124 	}
125 
126 }