1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27 public abstract class AbstractHazelcastCache implements Cache {
28
29
30 protected final ReadWriteLock readWriteLock = new DummyReadWriteLock();
31
32
33 protected final String id;
34
35
36 protected final IMap<Object, Object> cacheMap;
37
38
39
40
41
42
43
44
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 }