1 package net.matuschek.spider; 2 3 6 7 import java.util.HashSet ; 8 import java.util.LinkedList ; 9 10 19 public class HashedMemoryTaskList implements TaskList { 20 21 public static int DEFAULT_CAPACITY = 50000; 22 23 29 private LinkedList list = new LinkedList (); 31 32 40 private HashSet hashset = null; 41 42 43 51 private boolean sequentialAccess; 52 53 54 58 public HashedMemoryTaskList() { 59 this(true, DEFAULT_CAPACITY); 60 } 61 62 63 71 public HashedMemoryTaskList(boolean sequentialAccess) { 72 this(sequentialAccess, DEFAULT_CAPACITY); 73 } 74 75 84 public HashedMemoryTaskList(boolean sequentialAccess, int initialCapacity) { 85 this.sequentialAccess = sequentialAccess; 86 this.hashset = new HashSet (initialCapacity); 87 } 88 89 90 91 96 public synchronized void add(RobotTask task) { 97 if (this.sequentialAccess) { 98 list.add(task); 99 } 100 hashset.add(task); 101 } 102 103 104 108 public synchronized void addAtStart(RobotTask task) { 109 if (this.sequentialAccess) { 110 list.add(0,task); 111 } 112 hashset.add(task); 115 } 116 117 118 121 public synchronized void clear() { 122 list.clear(); 123 hashset.clear(); 124 } 125 126 127 130 public synchronized boolean contains(RobotTask task) { 131 return hashset.contains(task); 132 } 133 134 135 138 public synchronized boolean remove(RobotTask task) { 139 hashset.remove(task); 140 if (this.sequentialAccess) { 141 return list.remove(task); 142 } else { 143 return true; 144 } 145 } 146 147 148 156 public synchronized RobotTask removeFirst() 157 throws ArrayIndexOutOfBoundsException 158 { 159 if (! this.sequentialAccess) { 160 throw 161 new ArrayIndexOutOfBoundsException ("sequential access not allowed"); 162 } 163 RobotTask task = (RobotTask) list.removeFirst(); 166 hashset.remove(task); 167 return task; 168 } 169 170 171 174 public synchronized int size() { 175 return hashset.size(); 176 } 177 178 179 186 public synchronized RobotTask elementAt(int position) 187 throws ArrayIndexOutOfBoundsException 188 { 189 if (! this.sequentialAccess) { 190 throw 191 new ArrayIndexOutOfBoundsException ("sequential access not allowed"); 192 } 193 return (RobotTask)(list.get(position)); 195 } 196 197 } 198 | Popular Tags |