View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.cache.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.concurrent.locks.ReadWriteLock;
21  
22  import org.apache.ibatis.cache.Cache;
23  import org.apache.ibatis.cache.CacheException;
24  
25  /**
26   * @author Clinton Begin
27   */
28  public class PerpetualCache implements Cache {
29  
30    private String id;
31  
32    private Map<Object, Object> cache = new HashMap<Object, Object>();
33  
34    public PerpetualCache(String id) {
35      this.id = id;
36    }
37  
38    @Override
39    public String getId() {
40      return id;
41    }
42  
43    @Override
44    public int getSize() {
45      return cache.size();
46    }
47  
48    @Override
49    public void putObject(Object key, Object value) {
50      cache.put(key, value);
51    }
52  
53    @Override
54    public Object getObject(Object key) {
55      return cache.get(key);
56    }
57  
58    @Override
59    public Object removeObject(Object key) {
60      return cache.remove(key);
61    }
62  
63    @Override
64    public void clear() {
65      cache.clear();
66    }
67  
68    @Override
69    public ReadWriteLock getReadWriteLock() {
70      return null;
71    }
72  
73    @Override
74    public boolean equals(Object o) {
75      if (getId() == null) {
76        throw new CacheException("Cache instances require an ID.");
77      }
78      if (this == o) {
79        return true;
80      }
81      if (!(o instanceof Cache)) {
82        return false;
83      }
84  
85      Cache otherCache = (Cache) o;
86      return getId().equals(otherCache.getId());
87    }
88  
89    @Override
90    public int hashCode() {
91      if (getId() == null) {
92        throw new CacheException("Cache instances require an ID.");
93      }
94      return getId().hashCode();
95    }
96  
97  }