1 package net.matuschek.spider; 2 3 6 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 10 import net.matuschek.http.HttpConstants; 11 12 19 public class RobotTask 20 implements Comparable 21 { 22 23 31 public RobotTask(URL url, int maxDepth, String referer) { 32 setUrl(url); 33 this.maxDepth=maxDepth; 34 this.referer=referer; 35 } 36 37 45 public RobotTask(String urlString, int maxDepth, String referer) { 46 this.urlString = urlString; 47 this.maxDepth=maxDepth; 48 this.referer=referer; 49 } 50 51 public URL getUrl() { 52 try { 53 return new URL (urlString); 54 } catch (MalformedURLException e) { 55 e.printStackTrace(); 56 return null; 57 } 58 } 59 60 public void setUrl(URL url) { 61 urlString = url.toString(); 62 hashCode = 0; 63 } 64 65 public int getMaxDepth() { 66 return maxDepth; 67 } 68 69 public void setMaxDepth(int maxDepth) { 70 this.maxDepth = maxDepth; 71 } 72 73 public String getReferer() { 74 return referer; 75 } 76 77 public void setReferer(String referer) { 78 this.referer = referer; 79 } 80 81 public int getMethod() { 82 return method; 83 } 84 85 public void setMethod(int method) { 86 this.method = method; 87 hashCode = 0; 88 } 89 90 public String getParamString() { 91 return paramString; 92 } 93 94 public void setParamString(String paramString) { 95 this.paramString = paramString; 96 hashCode = 0; 97 } 98 99 100 101 102 106 public boolean equals(Object o) { 107 try { 108 return (compareTo(o) == 0); 109 } catch (ClassCastException e) { 110 return false; 111 } 112 } 113 114 115 123 public int compareTo(Object o) 124 throws ClassCastException 125 { 126 RobotTask r = (RobotTask)o; 127 128 if (r == null) { 129 throw new ClassCastException ("object to compare to is null"); 130 } 131 132 int diff = hashCode() - r.hashCode(); 133 if (diff == 0) { 134 String me = this.getInternalStringRepresentation(); 135 String it = r.getInternalStringRepresentation(); 136 diff = me.compareTo(it); 137 } 138 139 return diff; 140 } 141 142 143 149 public String toString() { 150 return urlString + " " + paramString + " Method " + method; 151 } 152 153 154 159 public int hashCode() { 160 if (hashCode != 0) { 161 return hashCode; 162 } 163 hashCode = getInternalStringRepresentation().hashCode(); 164 return hashCode; 165 } 166 167 178 public String getInternalStringRepresentation() { 179 return (paramString == null && method == HttpConstants.GET) 180 ? urlString 181 : urlString + paramString + method; 182 } 183 184 private int maxDepth; 186 private String referer; 187 protected int method=HttpConstants.GET; 188 protected String paramString=null; 189 190 protected int hashCode = 0; protected String urlString; protected int retries = 0; 194 197 public int retry() { return ++retries; } 198 199 } 200 | Popular Tags |