1 package com.stateofflow.eclipse.metrics.calculators.cohesion;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 final class FieldSet implements Iterable<String> {
8 public interface Predicate {
9 boolean execute(FieldSet fieldSet);
10 }
11
12 private final Set<String> fields = new HashSet<String>();
13
14 public FieldSet() {
15 }
16
17 public FieldSet(final FieldSet set) {
18 addAll(set);
19 }
20
21 public void add(final String field) {
22 if (!fields.contains(field)) {
23 fields.add(field);
24 }
25 }
26
27 public void addAll(final FieldSet set) {
28 final Iterator<String> iter = set.fields.iterator();
29 while (iter.hasNext()) {
30 add(iter.next());
31 }
32 }
33
34 public boolean contains(final String field) {
35 return fields.contains(field);
36 }
37
38 @Override
39 public boolean equals(final Object obj) {
40 if (obj == this) {
41 return true;
42 }
43
44 if (obj == null || !obj.getClass().equals(getClass())) {
45 return false;
46 }
47
48 final FieldSet that = (FieldSet) obj;
49 return fields.equals(that.fields);
50 }
51
52 @Override
53 public int hashCode() {
54 return fields.hashCode();
55 }
56
57 public boolean intersects(final FieldSet set) {
58 final Iterator<String> fieldIterator = fields.iterator();
59 while (fieldIterator.hasNext()) {
60 if (set.contains(fieldIterator.next())) {
61 return true;
62 }
63 }
64
65 return false;
66 }
67
68 public Iterator<String> iterator() {
69 return fields.iterator();
70 }
71
72 public int size() {
73 return fields.size();
74 }
75
76 @Override
77 public String toString() {
78 return fields.toString();
79 }
80 }