1 package org.sapia.magnet.domain; 2 3 import java.net.URL ; 6 import java.net.MalformedURLException ; 7 import java.util.Comparator ; 8 9 19 public class Resource implements Comparable { 20 21 25 26 private String _theURL; 27 28 29 private int _theIndex; 30 31 35 41 public Resource(String aURL, int anIndex) { 42 _theURL = aURL; 43 _theIndex = anIndex; 44 } 45 46 50 55 public String getURL() { 56 return _theURL; 57 } 58 59 64 public int getIndex() { 65 return _theIndex; 66 } 67 68 72 79 public URL toURL() throws MalformedURLException { 80 return new URL (_theURL); 81 } 82 83 87 93 public boolean equals(Object anObject) { 94 if (anObject instanceof Resource) { 95 return _theURL.equals(((Resource) anObject)._theURL); 96 } else { 97 return false; 98 } 99 } 100 101 106 public int hashCode() { 107 return _theURL.hashCode(); 108 } 109 110 115 public String toString() { 116 StringBuffer aBuffer = new StringBuffer (super.toString()); 117 aBuffer.append("[url=").append(_theURL). 118 append(" index=").append(_theIndex). 119 append("]\n"); 120 121 return aBuffer.toString(); 122 } 123 124 128 139 public int compareTo(Object anObject) { 140 return _theIndex - ((Resource) anObject)._theIndex; 141 } 142 143 144 148 public static class AscendingComparator implements Comparator { 149 public int compare(Object anObject, Object anotherObject) { 150 return ((Resource) anObject)._theURL.compareTo(((Resource) anotherObject)._theURL); 151 } 152 } 153 154 public static class DescendingComparator implements Comparator { 155 public int compare(Object anObject, Object anotherObject) { 156 return ((Resource) anObject)._theURL.compareTo(((Resource) anotherObject)._theURL) * -1; 157 } 158 } 159 } 160 | Popular Tags |