1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.archive.crawler.scope;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.archive.crawler.datamodel.CrawlURI;
31 import org.archive.net.UURI;
32
33 /***
34 * A CrawlScope that caches its seed list for the
35 * convenience of scope-tests that are based on the
36 * seeds.
37 *
38 * @author gojomo
39 *
40 */
41 public class SeedCachingScope extends ClassicScope {
42
43 private static final long serialVersionUID = 300230673616424926L;
44
45
46
47 List<UURI> seeds;
48
49 public SeedCachingScope(String name) {
50 super(name);
51 }
52
53
54
55
56 public boolean addSeed(CrawlURI curi) {
57 if (super.addSeed(curi) == false) {
58
59 return false;
60 }
61
62 List<UURI> newSeeds = new ArrayList<UURI>(seeds);
63 newSeeds.add(curi.getUURI());
64 seeds = newSeeds;
65 return true;
66 }
67
68
69
70
71 public synchronized void refreshSeeds() {
72 super.refreshSeeds();
73 seeds = null;
74 fillSeedsCache();
75 }
76
77
78
79
80 public Iterator<UURI> seedsIterator() {
81 fillSeedsCache();
82 return seeds.iterator();
83 }
84
85 /***
86 * Ensure seeds cache is created/filled
87 */
88 protected synchronized void fillSeedsCache() {
89 if (seeds==null) {
90 seeds = new ArrayList<UURI>();
91 Iterator<UURI> iter = super.seedsIterator();
92 while(iter.hasNext()) {
93 seeds.add(iter.next());
94 }
95 }
96 }
97 }