Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/state/StateEngine.java @ 343:542caebea773
Add functionality to get 'oldest' artifact of a collection.'
artifacts/trunk@3037 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 19 Oct 2011 15:55:33 +0000 |
parents | 2ce31a9414ff |
children | 16ab243507e0 |
rev | line source |
---|---|
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.artifactdatabase.state; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
2 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
3 import java.util.HashMap; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
4 import java.util.List; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
5 import java.util.Map; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
6 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
7 import org.apache.log4j.Logger; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 /** |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
10 * The StateEngine stores all states for each Artifact. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
11 * |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
12 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 */ |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 public class StateEngine { |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
15 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
16 /** The logger used in this class. */ |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
17 private static Logger logger = Logger.getLogger(StateEngine.class); |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
18 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 /** |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
20 * A map that contains the states of the artifacts. The key of this map is |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
21 * the name of an artifact, its value is a list of all states the artifact |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 * can reach. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 */ |
272
2ce31a9414ff
Use generics more precisely
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
108
diff
changeset
|
24 protected Map<String, List<State>> states; |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
27 /** |
108
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
28 * A map that contains all existing states. The key of this map is the ID of |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
29 * the state, its value is the state itself. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
30 */ |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
31 protected Map<String, State> allStates; |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
32 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
33 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
34 /** |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
35 * The default constructor. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 */ |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
37 public StateEngine() { |
272
2ce31a9414ff
Use generics more precisely
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
108
diff
changeset
|
38 states = new HashMap<String, List<State>>(); |
108
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
39 allStates = new HashMap<String, State>(); |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
40 } |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
41 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
42 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
43 /** |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
44 * This method adds a state into the map <i>allStates</i>. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
45 * |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
46 * @param state The state to add. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
47 */ |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
48 protected void addState(State state) { |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
49 allStates.put(state.getID(), state); |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
50 } |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
51 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
52 |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
53 /** |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
54 * Returns the state based on its ID. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
55 * |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
56 * @param stateId The ID of the desired state. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
57 * |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
58 * @return the state. |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
59 */ |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
60 public State getState(String stateId) { |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
61 return allStates.get(stateId); |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
62 } |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
63 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
64 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
65 /** |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
66 * Add new states for a specific artifact. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
67 * |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
68 * @param artifact The name of the artifact. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
69 * @param states A list of states that the artifact can reach. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
70 * |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
71 * @return true, if the states were added, otherwise false. |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
72 */ |
108
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
73 public boolean addStates(String artifact, List<State> states) { |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
74 List tmp = this.states.get(artifact); |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
75 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
76 if (tmp != null) { |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
77 logger.info( |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
78 "States for the artifact '" + artifact + "' already stored."); |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
79 |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
80 return false; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
81 } |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
82 |
108
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
83 // add the state to the map with all existing states |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
84 for (State s: states) { |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
85 addState(s); |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
86 } |
9ece61d918b1
Improved the StateEngine and the TransitionEngine to retrieve the reachable states of a current state.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
106
diff
changeset
|
87 |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
88 logger.debug("Add new states for the artifact '" + artifact + "'"); |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
89 return this.states.put(artifact, states) != null; |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
90 } |
106
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
91 |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
92 |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
93 /** |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
94 * Returns the state list of an artifact specified by its name. |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
95 * |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
96 * @param artifact The name of the artifact. |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
97 * |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
98 * @return the list of states of this artifact or <i>null</i> if no states |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
99 * are existing for this <i>artifact</i>. |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
100 */ |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
101 public List<State> getStates(String artifact) { |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
102 return states.get(artifact); |
ece0fdb07975
Implementations to initialize and retrieve states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
104
diff
changeset
|
103 } |
104
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
104 } |
26bfff409dd3
Added interfaces and engines used in concrete artifact packages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |