View Javadoc

1   /*
2    * $Id: SimpleMemoryObjectStore.java 22514 2011-07-22 04:51:45Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.store;
12  
13  import org.mule.api.store.ListableObjectStore;
14  import org.mule.api.store.ObjectStoreException;
15  import org.mule.config.i18n.CoreMessages;
16  
17  import java.io.Serializable;
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  public class SimpleMemoryObjectStore<T extends Serializable> extends AbstractObjectStore<T>
25      implements ListableObjectStore<T>
26  {
27      private Map<Serializable, T> map = Collections.synchronizedMap(new HashMap<Serializable, T>());
28  
29      @Override
30      public boolean isPersistent()
31      {
32          return false;
33      }
34  
35      @Override
36      protected boolean doContains(Serializable key)
37      {
38          return map.containsKey(key);
39      }
40  
41      @Override
42      protected void doStore(Serializable key, T value) throws ObjectStoreException
43      {
44          if (value == null)
45          {
46              throw new ObjectStoreException(CoreMessages.objectIsNull("value"));
47          }
48  
49          map.put(key, value);
50      }
51  
52      @Override
53      protected T doRetrieve(Serializable key)
54      {
55          return map.get(key);
56      }
57  
58      @Override
59      protected T doRemove(Serializable key)
60      {
61          return map.remove(key);
62      }
63  
64      @Override
65      public void open() throws ObjectStoreException
66      {
67          // this is a no-op
68      }
69  
70      @Override
71      public void close() throws ObjectStoreException
72      {
73          // this is a no-op
74      }
75  
76      @Override
77      public List<Serializable> allKeys() throws ObjectStoreException
78      {
79          return new ArrayList<Serializable>(map.keySet());
80      }
81  }