View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  /*
16   * Licensed under the Apache License, Version 2.0 (the "License");
17   * you may not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   */
28  
29  /*
30   *
31   *
32   * Licensed under the Apache License, Version 2.0 (the "License");
33   * you may not use this file except in compliance with the License.
34   * You may obtain a copy of the License at
35   *
36   * 	http://www.apache.org/licenses/LICENSE-2.0
37   *
38   * Unless required by applicable law or agreed to in writing, software
39   * distributed under the License is distributed on an "AS IS" BASIS,
40   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41   * See the License for the specific language governing permissions and
42   * limitations under the License.
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   * @author Johan Lindquist
54   * @version 1.0
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         // TODO: Must fix this synchronization
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             // It may be that the list is smaller than
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 }