--- /dev/null
+/*\r
+ * This is the MIT license, see also http://www.opensource.org/licenses/mit-license.html\r
+ *\r
+ * Copyright (c) 2001 Brian Pitcher\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a\r
+ * copy of this software and associated documentation files (the "Software"),\r
+ * to deal in the Software without restriction, including without limitation\r
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
+ * and/or sell copies of the Software, and to permit persons to whom the\r
+ * Software is furnished to do so, subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in\r
+ * all copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ * SOFTWARE.\r
+ */\r
+\r
+// $Header: /project/jiss/smhuang/leap/weblech/src/spider/DownloadQueue.java,v 1.1 2010/06/30 15:45:27 smhuang Exp $\r
+\r
+package spider;\r
+\r
+import java.util.*;\r
+import java.net.URL;\r
+import java.io.Serializable;\r
+\r
+public class DownloadQueue implements Serializable\r
+{\r
+ private SpiderConfig config;\r
+\r
+ private List interestingURLsToDownload;\r
+ private List averageURLsToDownload;\r
+ private List boringURLsToDownload;\r
+ private Set urlsInQueue;\r
+\r
+ public DownloadQueue(SpiderConfig config)\r
+ {\r
+ this.config = config;\r
+ interestingURLsToDownload = new ArrayList();\r
+ averageURLsToDownload = new ArrayList();\r
+ boringURLsToDownload = new ArrayList();\r
+ urlsInQueue = new HashSet();\r
+ }\r
+\r
+ public void queueURL(URLToDownload url)\r
+ {\r
+ URL u = url.getURL();\r
+ if(urlsInQueue.contains(u))\r
+ {\r
+ return;\r
+ }\r
+\r
+ if(config.isInteresting(u))\r
+ {\r
+ if(config.isDepthFirstSearch())\r
+ {\r
+ interestingURLsToDownload.add(0, url);\r
+ }\r
+ else\r
+ {\r
+ interestingURLsToDownload.add(url);\r
+ }\r
+ }\r
+ else if(config.isBoring(u))\r
+ {\r
+ if(config.isDepthFirstSearch())\r
+ {\r
+ boringURLsToDownload.add(0, url);\r
+ }\r
+ else\r
+ {\r
+ boringURLsToDownload.add(url);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if(config.isDepthFirstSearch())\r
+ {\r
+ averageURLsToDownload.add(0, url);\r
+ }\r
+ else\r
+ {\r
+ averageURLsToDownload.add(url);\r
+ }\r
+ }\r
+\r
+ urlsInQueue.add(u);\r
+ }\r
+\r
+ public void queueURLs(Collection urls)\r
+ {\r
+ for(Iterator i = urls.iterator(); i.hasNext(); )\r
+ {\r
+ URLToDownload u2d = (URLToDownload) i.next();\r
+ queueURL(u2d);\r
+ }\r
+ }\r
+\r
+ public URLToDownload getNextInQueue()\r
+ {\r
+ if(interestingURLsToDownload.size() > 0)\r
+ {\r
+ return returnURLFrom(interestingURLsToDownload);\r
+ }\r
+ else if(averageURLsToDownload.size() > 0)\r
+ {\r
+ return returnURLFrom(averageURLsToDownload);\r
+ }\r
+ else if(boringURLsToDownload.size() > 0)\r
+ {\r
+ return returnURLFrom(boringURLsToDownload);\r
+ }\r
+ else\r
+ {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ private URLToDownload returnURLFrom(List urlList)\r
+ {\r
+ URLToDownload u2d = (URLToDownload) urlList.get(0);\r
+ urlList.remove(0);\r
+ urlsInQueue.remove(u2d.getURL());\r
+ return u2d;\r
+ }\r
+\r
+ public int size()\r
+ {\r
+ return interestingURLsToDownload.size() + averageURLsToDownload.size() + boringURLsToDownload.size();\r
+ }\r
+\r
+ public String toString()\r
+ {\r
+ return size() + " URLs";\r
+ }\r
+\r
+} // End class DownloadQueue\r