1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.sensorframework.dao.memory; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.Collections; |
9 | import java.util.HashMap; |
10 | |
11 | import de.uka.ipd.sdq.sensorframework.entities.State; |
12 | import de.uka.ipd.sdq.sensorframework.entities.StateSensor; |
13 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
14 | import de.uka.ipd.sdq.sensorframework.entities.dao.IStateDAO; |
15 | import de.uka.ipd.sdq.sensorframework.entities.impl.StateImpl; |
16 | |
17 | /** |
18 | * @author Steffen Becker |
19 | * |
20 | */ |
21 | public class MemoryStateDAO implements IStateDAO { |
22 | |
23 | private long nextID = 0; |
24 | private IDAOFactory myFactory; |
25 | private HashMap<Long,State> index = new HashMap<Long,State>(); |
26 | |
27 | public MemoryStateDAO(IDAOFactory myFactory){ |
28 | this.myFactory = myFactory; |
29 | } |
30 | |
31 | /* (non-Javadoc) |
32 | * @see de.uka.ipd.sdq.sensorfactory.entities.dao.IStateDAO#addState(java.lang.String) |
33 | */ |
34 | public synchronized State addState(String p_stateliteral) { |
35 | State result = new StateImpl(myFactory); |
36 | result.setStateID(nextID++); |
37 | result.setStateLiteral(p_stateliteral); |
38 | |
39 | index.put(result.getStateID(), result); |
40 | return result; |
41 | } |
42 | |
43 | public synchronized State get(long id) { |
44 | return index.get(id); |
45 | } |
46 | |
47 | public synchronized Collection<State> getStates() { |
48 | return Collections.unmodifiableCollection(index.values()); |
49 | } |
50 | |
51 | public synchronized Collection<State> findByStateLiteral(String searchKey) { |
52 | ArrayList<State> result = new ArrayList<State>(); |
53 | for (State e:this.index.values()){ |
54 | if (e.getStateLiteral().equals(searchKey)) |
55 | result.add(e); |
56 | } |
57 | return Collections.unmodifiableCollection(result); |
58 | } |
59 | |
60 | public void store(StateSensor stateSen) { |
61 | } |
62 | |
63 | public synchronized void removeState(State state, boolean doCascade) { |
64 | if (state == null) { |
65 | return; |
66 | } |
67 | |
68 | index.remove(state.getStateID()); |
69 | } |
70 | |
71 | public void store(State st) { |
72 | } |
73 | |
74 | public void storeAll() { |
75 | // Nothing to do here |
76 | } |
77 | |
78 | } |