1
2
3
4
5
6
7 package org.asyrinx.joey.om.agent.impl;
8
9 import java.io.Serializable;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.Set;
13
14 import org.apache.commons.collections.Closure;
15 import org.asyrinx.brownie.core.collection.MapTree;
16 import org.asyrinx.brownie.core.collection.MapTreeVisitor;
17 import org.asyrinx.joey.om.agent.AgentAssociation;
18
19 /***
20 * @author akima
21 */
22 public class AgentAssociationImpl implements AgentAssociation {
23
24 /***
25 *
26 */
27 public AgentAssociationImpl() {
28 super();
29 }
30
31 private final Set usingAgent = new HashSet();
32
33
34
35
36 public void addUsingAgent(Class agentClass) {
37 usingAgent.add(agentClass);
38 }
39
40
41
42
43 public boolean canUseAgent(Object agent) {
44 for (Iterator iter = usingAgent.iterator(); iter.hasNext();) {
45 final Class class1 = (Class) iter.next();
46 if (class1.isInstance(agent))
47 return true;
48 }
49 return false;
50 }
51
52 private MapTree loadedObjects = new MapTree();
53
54
55
56
57 public void addLoaded(Serializable key, Object entity) {
58 loadedObjects.put(new Object[] { entity.getClass(), key }, entity);
59 }
60
61
62
63
64 public boolean isLoaded(final Object entity) {
65 if (entity == null)
66 return false;
67 final CheckContains checkContains = new CheckContains(entity);
68 new MapTreeVisitor(loadedObjects.getRoot(), checkContains).execute();
69 return checkContains.contains;
70 }
71
72
73
74
75 public boolean isLoaded(Serializable key, Class entityClass) {
76 return loadedObjects.get(new Object[] { entityClass, key }) != null;
77 }
78
79
80
81
82 public Object getLoaded(Serializable key, Class entityClass) {
83 return loadedObjects.get(new Object[] { entityClass, key });
84 }
85
86
87
88
89 public void clearLoaded() {
90 loadedObjects.getRoot().clear();
91 }
92
93 }
94 class CheckContains implements Closure {
95 public CheckContains(Object target) {
96 this.target = target;
97 }
98 private final Object target;
99 public boolean contains = false;
100 public void execute(Object object) {
101 if (target == object)
102 contains = true;
103 }
104
105 }