| 1 | package de.uka.ipd.sdq.sensorframework.dao.memory; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | |
| 8 | import de.uka.ipd.sdq.sensorframework.entities.ScalabilitySensor; |
| 9 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 10 | import de.uka.ipd.sdq.sensorframework.entities.State; |
| 11 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
| 12 | import de.uka.ipd.sdq.sensorframework.entities.TimeSpanSensor; |
| 13 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
| 14 | import de.uka.ipd.sdq.sensorframework.entities.dao.ISensorDAO; |
| 15 | import de.uka.ipd.sdq.sensorframework.entities.impl.ScalabilitySensorImpl; |
| 16 | import de.uka.ipd.sdq.sensorframework.entities.impl.StateSensorImpl; |
| 17 | import de.uka.ipd.sdq.sensorframework.entities.impl.TimeSpanSensorImpl; |
| 18 | |
| 19 | /** |
| 20 | * TODO |
| 21 | * |
| 22 | */ |
| 23 | public class MemorySensorDAO implements ISensorDAO { |
| 24 | |
| 25 | private IDAOFactory myFactory; |
| 26 | private long nextID = 0; |
| 27 | private HashMap<Long,Sensor> index = new HashMap<Long,Sensor>(); |
| 28 | |
| 29 | public MemorySensorDAO(IDAOFactory memoryDAOFactory) { |
| 30 | this.myFactory = memoryDAOFactory; |
| 31 | } |
| 32 | |
| 33 | public synchronized StateSensor addStateSensor(State p_initialstate, String p_sensorname) { |
| 34 | StateSensor result = new StateSensorImpl(myFactory); |
| 35 | result.setSensorID(nextID++); |
| 36 | result.setInitialState(p_initialstate); |
| 37 | result.setSensorName(p_sensorname); |
| 38 | |
| 39 | index.put(result.getSensorID(), result); |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | public synchronized TimeSpanSensor addTimeSpanSensor(String p_sensorname) { |
| 44 | TimeSpanSensor result = new TimeSpanSensorImpl(myFactory); |
| 45 | result.setSensorID(nextID++); |
| 46 | result.setSensorName(p_sensorname); |
| 47 | |
| 48 | index.put(result.getSensorID(), result); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | public synchronized ScalabilitySensor addScalabilitySensor(String p_sensorname) { |
| 53 | ScalabilitySensor result = new ScalabilitySensorImpl(myFactory); |
| 54 | result.setSensorID(nextID++); |
| 55 | result.setSensorName(p_sensorname); |
| 56 | |
| 57 | index.put(result.getSensorID(), result); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | public synchronized Sensor get(long id) { |
| 62 | if (!index.containsKey(id)) |
| 63 | throw new RuntimeException("Attempt to retrieve non-existing sensor."); |
| 64 | return index.get(id); |
| 65 | } |
| 66 | |
| 67 | public synchronized Collection<Sensor> getSensors() { |
| 68 | return Collections.unmodifiableCollection(index.values()); |
| 69 | } |
| 70 | |
| 71 | public synchronized Collection<Sensor> findBySensorName(String searchKey) { |
| 72 | ArrayList<Sensor> result = new ArrayList<Sensor>(); |
| 73 | for (Sensor e:this.index.values()){ |
| 74 | if (e.getSensorName().equals(searchKey)) |
| 75 | result.add(e); |
| 76 | } |
| 77 | return Collections.unmodifiableCollection(result); |
| 78 | } |
| 79 | |
| 80 | public synchronized void removeSensor(Sensor sensor, boolean doCascade) { |
| 81 | if (sensor == null) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if ( doCascade == true ) { |
| 86 | if (sensor instanceof StateSensor) { |
| 87 | //remove the states |
| 88 | for (State state: ((StateSensor)sensor).getSensorStates()) { |
| 89 | myFactory.createStateDAO().removeState(state, true); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | index.remove(sensor.getSensorID()); |
| 95 | } |
| 96 | |
| 97 | public void store(Sensor s) { |
| 98 | } |
| 99 | |
| 100 | public void storeAll() { |
| 101 | // Nothing to do here |
| 102 | } |
| 103 | |
| 104 | } |