]>
Commit | Line | Data |
---|---|---|
e0fe6563 | 1 | package no.uio.ifi.refaktor.prefix; |
4ecbd2c6 EK |
2 | |
3 | import java.util.HashSet; | |
4 | import java.util.Iterator; | |
5 | import java.util.LinkedList; | |
6 | import java.util.Set; | |
7 | ||
7a978020 EK |
8 | /** |
9 | * A set for collecting prefixes (see {@link Prefix}}). | |
10 | * | |
11 | * It is based on a regular Set<Prefix>, but is used to | |
12 | * add some more functionality to the basic set, like | |
13 | * incrementing a counter in a prefix each time an equal | |
14 | * prefix is registered with the set. In addition | |
15 | * it can produce the set that is this set, minus the | |
16 | * prefixes from another set that are enclosed by the | |
17 | * prefixes in this set. | |
18 | */ | |
4ecbd2c6 EK |
19 | public class PrefixSet implements Iterable<Prefix> { |
20 | ||
21 | private final Set<Prefix> prefixes = new HashSet<Prefix>(); | |
22 | ||
dda9d0a5 | 23 | public PrefixSet() { |
062df6c3 EK |
24 | } |
25 | ||
dda9d0a5 EK |
26 | private PrefixSet(PrefixSet initialElements) { |
27 | prefixes.addAll(initialElements.prefixes); | |
062df6c3 EK |
28 | } |
29 | ||
30 | public boolean add(Prefix prefix) { | |
31 | return prefixes.add(prefix); | |
32 | } | |
33 | ||
7a978020 | 34 | private void remove(Prefix prefix) { |
062df6c3 | 35 | prefixes.remove(prefix); |
4ecbd2c6 EK |
36 | } |
37 | ||
38 | public boolean contains(Prefix prefix) { | |
39 | return prefixes.contains(prefix); | |
40 | } | |
41 | ||
42 | @Override | |
43 | public Iterator<Prefix> iterator() { | |
44 | return prefixes.iterator(); | |
45 | } | |
46 | ||
47 | public boolean isEmpty() { | |
48 | return prefixes.isEmpty(); | |
49 | } | |
50 | ||
4ecbd2c6 EK |
51 | public LinkedList<Prefix> toList() { |
52 | return new LinkedList<Prefix>(prefixes); | |
53 | } | |
54 | ||
bfbc0306 EK |
55 | public void register(Prefix prefix) { |
56 | if (contains(prefix)) { | |
57 | incrementCountFor(prefix); | |
58 | } else { | |
59 | add(prefix); | |
60 | } | |
61 | } | |
62 | ||
63 | private void incrementCountFor(Prefix prefix) { | |
64 | get(prefix).incrementCount(); | |
65 | } | |
66 | ||
67 | public Prefix get(Prefix prefix) { | |
68 | for (Prefix p: this) { | |
69 | if (p.equals(prefix)) | |
70 | return p; | |
71 | } | |
72 | return null; | |
73 | } | |
74 | ||
7a978020 | 75 | public void registerAllSubPrefixesOf(Prefix prefix) { |
bfbc0306 EK |
76 | for (Prefix p: prefix.getSubPrefixes()) |
77 | register(p); | |
78 | } | |
79 | ||
80 | @Override | |
81 | public String toString() { | |
82 | String str = ""; | |
83 | for (Prefix p: this) { | |
84 | str += p.toString() + " (" + p.getCount() + ")\n"; | |
85 | } | |
86 | return str; | |
87 | } | |
88 | ||
7a978020 EK |
89 | /** |
90 | * Creates a set of prefixes that are the prefixes of this set, | |
91 | * minus the prefixes that are enclosing the prefixes of the | |
74581229 EK |
92 | * other set. A prefix is enclosing another if the other prefix |
93 | * is a sub-prefix of the first prefix. | |
7a978020 EK |
94 | * |
95 | * @param other The set of prefixes that are to be checked against this one. | |
74581229 | 96 | * @return The set of prefixes that are not enclosing the ones in the other set. |
7a978020 EK |
97 | */ |
98 | public PrefixSet minusEnclosedPrefixesFrom(PrefixSet other) { | |
062df6c3 | 99 | PrefixSet prefixSet = shallowCopy(); |
dda9d0a5 EK |
100 | removeEnclosingPrefixesFromPrefixSet(prefixSet, other); |
101 | return prefixSet; | |
102 | } | |
103 | ||
104 | private PrefixSet shallowCopy() { | |
105 | return new PrefixSet(this); | |
106 | } | |
107 | ||
108 | private void removeEnclosingPrefixesFromPrefixSet(PrefixSet prefixSet, PrefixSet other) { | |
74581229 | 109 | for (Prefix p: other) { |
062df6c3 | 110 | for (Prefix prefix: this) { |
74581229 | 111 | if (prefix.hasSubPrefix(p)) |
062df6c3 EK |
112 | prefixSet.remove(prefix); |
113 | } | |
114 | } | |
062df6c3 | 115 | } |
ee6a0b5b EK |
116 | |
117 | public void clear() { | |
118 | prefixes.clear(); | |
119 | } | |
4ecbd2c6 | 120 | } |