View Javadoc
1   /*
2    *    Copyright 2010-2022 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    *       https://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.mybatis.caches.hazelcast;
17  
18  import com.hazelcast.map.IMap;
19  
20  import java.util.concurrent.locks.ReadWriteLock;
21  
22  import org.apache.ibatis.cache.Cache;
23  
24  /**
25   * The Class AbstractHazelcastCache.
26   */
27  public abstract class AbstractHazelcastCache implements Cache {
28  
29    /** The {@code ReadWriteLock}. */
30    protected final ReadWriteLock readWriteLock = new DummyReadWriteLock();
31  
32    /** The cache id. */
33    protected final String id;
34  
35    /** The cache map reference. */
36    protected final IMap<Object, Object> cacheMap;
37  
38    /**
39     * Instantiates a new abstract hazelcast cache.
40     *
41     * @param id
42     *          the id
43     * @param imap
44     *          the i map
45     */
46    protected AbstractHazelcastCache(String id, IMap<Object, Object> imap) {
47      if (id == null) {
48        throw new IllegalArgumentException("Cache instances require an id");
49      }
50      if (imap == null) {
51        throw new IllegalArgumentException("Cache instances require a cacheMap");
52      }
53      this.id = id;
54      this.cacheMap = imap;
55    }
56  
57    @Override
58    public void clear() {
59      this.cacheMap.clear();
60    }
61  
62    @Override
63    public String getId() {
64      return this.id;
65    }
66  
67    @Override
68    public Object getObject(Object key) {
69      return this.cacheMap.get(key);
70    }
71  
72    @Override
73    public ReadWriteLock getReadWriteLock() {
74      return this.readWriteLock;
75    }
76  
77    @Override
78    public int getSize() {
79      return this.cacheMap.size();
80    }
81  
82    @Override
83    public void putObject(Object key, Object value) {
84      if (value != null) {
85        this.cacheMap.set(key, value);
86      } else {
87        if (this.cacheMap.containsKey(key)) {
88          this.cacheMap.remove(key);
89        }
90      }
91    }
92  
93    @Override
94    public Object removeObject(Object key) {
95      return this.cacheMap.remove(key);
96    }
97  
98    @Override
99    public boolean equals(Object obj) {
100     if (this == obj) {
101       return true;
102     }
103     if (obj == null) {
104       return false;
105     }
106     if (!(obj instanceof Cache)) {
107       return false;
108     }
109 
110     Cache otherCache = (Cache) obj;
111     return this.id.equals(otherCache.getId());
112   }
113 
114   @Override
115   public int hashCode() {
116     return this.id.hashCode();
117   }
118 
119   @Override
120   public String toString() {
121     return "Hazelcast {" + this.id + "}";
122   }
123 
124 }