1 | package de.uka.ipd.sdq.sensorframework.filter; |
2 | |
3 | import java.util.AbstractCollection; |
4 | import java.util.ArrayList; |
5 | import java.util.Collection; |
6 | import java.util.Iterator; |
7 | |
8 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
9 | |
10 | /** |
11 | * Superclass for all filtered collection in the SensorFramework. |
12 | * @author Roman Andrej |
13 | */ |
14 | public abstract class AbstractMeasurementsCollection extends |
15 | AbstractCollection<Measurement> { |
16 | |
17 | /** Original collection. */ |
18 | protected Collection<Measurement> originalMeasurements = null; |
19 | |
20 | /** Filtered collection. |
21 | * TODO clarify whether it contains items filtered AWAY |
22 | * rather than items left AFTER filtering. |
23 | * TODO why is this private, not protected? |
24 | */ |
25 | private ArrayList<Measurement> filteredItems = null;//why not a Collection, too? |
26 | |
27 | /** |
28 | * Initializes a new AbstractMeasurementsCollection. |
29 | */ |
30 | public AbstractMeasurementsCollection(){ |
31 | } |
32 | |
33 | /** |
34 | * Initializes a new AbstractMeasurementsCollection. |
35 | * |
36 | * @param originalMeasurements |
37 | * The associated measurements. |
38 | */ |
39 | public AbstractMeasurementsCollection( |
40 | Collection<Measurement> originalMeasurements) { |
41 | this.originalMeasurements = originalMeasurements; |
42 | } |
43 | |
44 | /** {@inheritDoc} |
45 | */ |
46 | @Override |
47 | public Iterator<Measurement> iterator() { |
48 | if (filteredItems == null) { |
49 | filteredItems = new ArrayList<Measurement>(); |
50 | applyFilter(filteredItems); |
51 | } |
52 | return new Iterator<Measurement>(){ |
53 | Iterator<Measurement> it = originalMeasurements.iterator(); |
54 | int currentOriginal = 0, alreadyReturned = 0; |
55 | |
56 | public boolean hasNext() { |
57 | return currentOriginal < originalMeasurements.size() && alreadyReturned < size(); |
58 | } |
59 | |
60 | public Measurement next() { |
61 | Measurement next; |
62 | while ( filteredItems.contains(next = it.next()) ){ |
63 | currentOriginal++; |
64 | } |
65 | currentOriginal++; alreadyReturned++; |
66 | return next; |
67 | } |
68 | |
69 | public void remove() {//TODO add logging here |
70 | |
71 | } |
72 | }; |
73 | } |
74 | |
75 | /** The Method filtered the measurements. |
76 | * TODO explain what filtering actually means. |
77 | * @param filteredItemsList seems to be used as return parameter, |
78 | * which contains the elements that are filtered away. |
79 | */ |
80 | protected abstract void applyFilter(Collection<Measurement> filteredItemsList); |
81 | |
82 | |
83 | /* (non-Javadoc) |
84 | * @see java.util.AbstractCollection#size() |
85 | */ |
86 | @Override |
87 | public int size() { |
88 | if (filteredItems == null) { |
89 | filteredItems = new ArrayList<Measurement>(); |
90 | applyFilter(filteredItems); |
91 | } |
92 | return originalMeasurements.size() - filteredItems.size();//TODO does it make sense? or does "filteredItems" contain items filtered AWAY, not items remaining AFTER filtering? |
93 | } |
94 | |
95 | /** |
96 | * Get the original collection. |
97 | */ |
98 | public Collection<Measurement> getOriginalMeasurements() |
99 | throws InstantiationException {//TODO is this really an instantiation problem? Or rather a NullPointerException? |
100 | if (originalMeasurements == null) |
101 | throw new InstantiationException("No original measurements saved!"); |
102 | return originalMeasurements; |
103 | } |
104 | |
105 | /** |
106 | * Resets internally stored filtered items to <code>null</code> |
107 | */ |
108 | public void resetFilteredItems(){ |
109 | filteredItems = null; |
110 | } |
111 | } |