1
2
3
4
5
6
7
8
9 package bexee.dao;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.apache.commons.id.serial.AlphanumericGenerator;
15
16 import bexee.core.ProcessContext;
17
18 /***
19 * This class uses memory rather than persistent storage to store and load
20 * <code>ProcessContext</code> objects.
21 *
22 * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:09 $
23 * @author Patric Fornasier
24 * @author Pawel Kowalski
25 */
26 public class MemoryProcessContextDAO implements ProcessContextDAO {
27
28 protected static Map map;
29
30 private static AlphanumericGenerator generator;
31
32 /***
33 * Creates a new <code>MemoryProcessContextDAO</code> that uses a static
34 * <code>Map</code> to manage the stored <code>ProcessContext</code>
35 * objects.
36 */
37 public MemoryProcessContextDAO() {
38 if (map == null) {
39 map = new HashMap();
40 }
41 if (generator == null) {
42 generator = generator = new AlphanumericGenerator(false, 10);
43 }
44 }
45
46 public void delete(String id) {
47 map.remove(id);
48 }
49
50 public ProcessContext find(String id) {
51 return (ProcessContext) map.get(id);
52 }
53
54 public String insert(ProcessContext ctx) {
55
56
57 String id = generator.nextStringIdentifier();
58 ctx.setId(id);
59
60
61 map.put(id, ctx);
62 return id;
63 }
64
65 public void update(ProcessContext ctx) throws DAOException {
66
67
68 String id = ctx.getId();
69 ProcessContext oldCtx = find(id);
70 if (oldCtx == null) {
71 throw new DAOException("No object with id " + id + "exists");
72 }
73
74
75 map.put(id, ctx);
76 }
77 }