]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/prefix/PrefixSet.java
0d2c1da110294896dae25faed4e1dc0748f59cda
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / prefix / PrefixSet.java
1 package no.uio.ifi.refaktor.prefix;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.LinkedList;
6 import java.util.Set;
7
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  */
19 public class PrefixSet implements Iterable<Prefix> {
20
21         private final Set<Prefix> prefixes = new HashSet<Prefix>();
22
23         public PrefixSet() {
24         }
25
26         private PrefixSet(PrefixSet initialElements) {
27                 prefixes.addAll(initialElements.prefixes);
28         }
29
30         public boolean add(Prefix prefix) {
31                 return prefixes.add(prefix);
32         }
33
34         private void remove(Prefix prefix) {
35                 prefixes.remove(prefix);
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
51         public LinkedList<Prefix> toList() {
52                 return new LinkedList<Prefix>(prefixes);
53         }
54
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
75         public void registerAllSubPrefixesOf(Prefix prefix) {
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
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 
92          * other set. A prefix is enclosing another if the other prefix
93          * is a sub-prefix of the first prefix.
94          * 
95          * @param other The set of prefixes that are to be checked against this one.
96          * @return The set of prefixes that are not enclosing the ones in the other set.
97          */
98         public PrefixSet minusEnclosedPrefixesFrom(PrefixSet other) {
99                 PrefixSet prefixSet = shallowCopy();
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) {
109                 for (Prefix p: other) {
110                         for (Prefix prefix: this) {
111                                 if (prefix.hasSubPrefix(p))
112                                         prefixSet.remove(prefix);
113                         }
114                 }
115         }
116
117         public void clear() {
118                 prefixes.clear();
119         }
120 }