1 24 25 27 package weblech.spider; 28 29 import java.util.*; 30 import java.net.URL ; 31 import java.io.Serializable ; 32 33 public class DownloadQueue implements Serializable 34 { 35 private SpiderConfig config; 36 37 private List interestingURLsToDownload; 38 private List averageURLsToDownload; 39 private List boringURLsToDownload; 40 private Set urlsInQueue; 41 42 public DownloadQueue(SpiderConfig config) 43 { 44 this.config = config; 45 interestingURLsToDownload = new ArrayList(); 46 averageURLsToDownload = new ArrayList(); 47 boringURLsToDownload = new ArrayList(); 48 urlsInQueue = new HashSet(); 49 } 50 51 public void queueURL(URLToDownload url) 52 { 53 URL u = url.getURL(); 54 if(urlsInQueue.contains(u)) 55 { 56 return; 57 } 58 59 if(config.isInteresting(u)) 60 { 61 if(config.isDepthFirstSearch()) 62 { 63 interestingURLsToDownload.add(0, url); 64 } 65 else 66 { 67 interestingURLsToDownload.add(url); 68 } 69 } 70 else if(config.isBoring(u)) 71 { 72 if(config.isDepthFirstSearch()) 73 { 74 boringURLsToDownload.add(0, url); 75 } 76 else 77 { 78 boringURLsToDownload.add(url); 79 } 80 } 81 else 82 { 83 if(config.isDepthFirstSearch()) 84 { 85 averageURLsToDownload.add(0, url); 86 } 87 else 88 { 89 averageURLsToDownload.add(url); 90 } 91 } 92 93 urlsInQueue.add(u); 94 } 95 96 public void queueURLs(Collection urls) 97 { 98 for(Iterator i = urls.iterator(); i.hasNext(); ) 99 { 100 URLToDownload u2d = (URLToDownload) i.next(); 101 queueURL(u2d); 102 } 103 } 104 105 public URLToDownload getNextInQueue() 106 { 107 if(interestingURLsToDownload.size() > 0) 108 { 109 return returnURLFrom(interestingURLsToDownload); 110 } 111 else if(averageURLsToDownload.size() > 0) 112 { 113 return returnURLFrom(averageURLsToDownload); 114 } 115 else if(boringURLsToDownload.size() > 0) 116 { 117 return returnURLFrom(boringURLsToDownload); 118 } 119 else 120 { 121 return null; 122 } 123 } 124 125 private URLToDownload returnURLFrom(List urlList) 126 { 127 URLToDownload u2d = (URLToDownload) urlList.get(0); 128 urlList.remove(0); 129 urlsInQueue.remove(u2d.getURL()); 130 return u2d; 131 } 132 133 public int size() 134 { 135 return interestingURLsToDownload.size() + averageURLsToDownload.size() + boringURLsToDownload.size(); 136 } 137 138 public String toString() 139 { 140 return size() + " URLs"; 141 } 142 143 } | Popular Tags |