1 | package de.uka.ipd.sdq.sensorframework.filter; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | import java.util.Properties; |
6 | |
7 | import de.uka.ipd.sdq.sensorframework.entities.Measurement; |
8 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
9 | |
10 | /** |
11 | * The class creates the filtered collection from measurements collection. |
12 | * |
13 | * @author Roman Andrej |
14 | */ |
15 | public class FilteredCollectionsManager { |
16 | |
17 | private ArrayList<IFilteredCollectionFactory> factories = new ArrayList<IFilteredCollectionFactory>(); |
18 | |
19 | IFilteredCollectionFactory factory; |
20 | |
21 | /** Filters count value. */ |
22 | private static int count = 0; |
23 | |
24 | /** Get the filtered measurements as new SensorAndMeasurements instance. */ |
25 | public Object getFilteredMeasurements(SensorAndMeasurements sam) { |
26 | return new SensorAndMeasurements(sam.getSensor(), |
27 | filteringTheMeasurements(sam.getMeasurements())); |
28 | } |
29 | |
30 | /** Set the filter sequence. */ |
31 | private Collection<Measurement> filteringTheMeasurements( |
32 | Collection<Measurement> measuremts) { |
33 | |
34 | // Exit constraint. |
35 | if (factories.isEmpty() || count == factories.size()) { |
36 | // set counter of 0 |
37 | resetCount(); |
38 | return measuremts; |
39 | } |
40 | factory = factories.get(count++); |
41 | |
42 | AbstractMeasurementsCollection filteredCollection; |
43 | |
44 | if (factory.canFilter(measuremts, |
45 | getProperty(factory.getProperties()))) { |
46 | filteredCollection = factory.getFilteredCollection(measuremts, |
47 | getProperty(factory.getProperties())); |
48 | } else { |
49 | // set the minimal value for filtering attribute |
50 | filteredCollection = factory.getFilteredCollection(measuremts, |
51 | factory.convertToType("0")); |
52 | } |
53 | |
54 | // call it self with another filtered collection |
55 | return filteringTheMeasurements(filteredCollection); |
56 | } |
57 | |
58 | /** Get the value of first property. */ |
59 | private Number getProperty(Properties properties) { |
60 | // get the attribute description |
61 | String desc = properties.propertyNames().nextElement().toString(); |
62 | // get attribute |
63 | return (Number) properties.get(desc); |
64 | } |
65 | |
66 | public void addFactory(IFilteredCollectionFactory factory) { |
67 | factories.add(factory); |
68 | } |
69 | |
70 | public void removeFactory(IFilteredCollectionFactory factory){ |
71 | factories.remove(factory); |
72 | } |
73 | |
74 | /** |
75 | * @return the factories |
76 | */ |
77 | public ArrayList<IFilteredCollectionFactory> getFactories() { |
78 | return factories; |
79 | } |
80 | |
81 | /** |
82 | * @param factories the factories to set |
83 | */ |
84 | public void setFactories(ArrayList<IFilteredCollectionFactory> factories) { |
85 | this.factories = factories; |
86 | } |
87 | |
88 | /** The method is used for control of the filtered collection sequence. */ |
89 | public void resetCount() { |
90 | count = 0; |
91 | } |
92 | } |