1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package net.sourceforge.basher.impl;
45
46 import java.util.*;
47
48 import net.sourceforge.basher.EntityRegistry;
49 import net.sourceforge.basher.internal.Randomizer;
50 import org.apache.commons.logging.Log;
51
52
53
54
55
56 public class InMemoryEntityRegistry implements EntityRegistry
57 {
58 private Log _logger;
59 private Map _registry = new HashMap();
60 private Randomizer _randomizer;
61
62 public void setLog(final Log logger)
63 {
64 _logger = logger;
65 }
66
67 public void setRandomizer(final Randomizer randomizer)
68 {
69 _randomizer = randomizer;
70 }
71
72
73 public void unregister(final String entityIdentifier, final Object entity)
74 {
75 if (entityIdentifier == null)
76 {
77 throw new NullPointerException("entityIdentifier");
78 }
79 if (entity == null)
80 {
81 throw new NullPointerException("entityID");
82 }
83
84 synchronized (_registry)
85 {
86 final List list = (List) _registry.get(entityIdentifier);
87 if (list == null || !list.contains(entity))
88 {
89 _logger.warn("Warning, entity identifier " + entity + " does not exists for type " + entityIdentifier);
90 return;
91 }
92 list.remove(entity);
93 }
94 }
95
96 public void register(final String entityIdentifier, final Object entity)
97 {
98 if (entityIdentifier == null)
99 {
100 throw new NullPointerException("entityIdentifier");
101 }
102 if (entity == null)
103 {
104 throw new NullPointerException("entityID");
105 }
106
107 synchronized (_registry)
108 {
109 List list = (List) _registry.get(entityIdentifier);
110 if (list == null)
111 {
112 list = new ArrayList();
113 _registry.put(entityIdentifier, list);
114 }
115 list.add(entity);
116 }
117 }
118
119 public Collection getAllRegistered(final String entityIdentifier)
120 {
121 if (entityIdentifier == null)
122 {
123 throw new NullPointerException("entityIdentifier");
124 }
125 return (Collection) _registry.get(entityIdentifier);
126 }
127
128 public long getNumRegistered(final String entityIdentifier)
129 {
130 if (entityIdentifier == null)
131 {
132 throw new NullPointerException("entityIdentifier");
133 }
134 final List list = (List) _registry.get(entityIdentifier);
135 return (list == null ? 0 : list.size());
136 }
137
138 public Collection getRandomSelection(final String entityIdentifier, final int size)
139 {
140
141 synchronized (_registry)
142 {
143 if (entityIdentifier == null)
144 {
145 throw new NullPointerException("entityIdentifier");
146 }
147 if (size <= 0)
148 {
149 throw new IllegalArgumentException("size <= 0");
150 }
151
152
153 final List list = (List) _registry.get(entityIdentifier);
154
155 if (list == null || list.isEmpty())
156 {
157 return Collections.EMPTY_LIST;
158 }
159
160
161 int realNumberToSelect = size;
162
163 if (list.size() < size)
164 {
165 realNumberToSelect = list.size();
166 }
167
168 final List selection = new ArrayList();
169
170 for (int i = 0; i < realNumberToSelect; i++)
171 {
172 final int nextIDSlot = _randomizer.getRandomInt(list.size());
173 final Object nextID = list.get(nextIDSlot);
174 selection.add(nextID);
175 }
176 return selection;
177 }
178 }
179
180 public Object getRandom(final String entityIdentifier)
181 {
182 List list = (List) getRandomSelection(entityIdentifier, 1);
183 if (list.isEmpty())
184 {
185 return null;
186 }
187 return list.get(0);
188 }
189
190 }