View Javadoc
1   /*
2    *    Copyright 2009-2024 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.apache.ibatis.cache.decorators;
17  
18  import java.util.concurrent.locks.ReentrantLock;
19  
20  import org.apache.ibatis.cache.Cache;
21  
22  /**
23   * @author Clinton Begin
24   */
25  public class SynchronizedCache implements Cache {
26  
27    private final ReentrantLock lock = new ReentrantLock();
28    private final Cache delegate;
29  
30    public SynchronizedCache(Cache delegate) {
31      this.delegate = delegate;
32    }
33  
34    @Override
35    public String getId() {
36      return delegate.getId();
37    }
38  
39    @Override
40    public int getSize() {
41      lock.lock();
42      try {
43        return delegate.getSize();
44      } finally {
45        lock.unlock();
46      }
47    }
48  
49    @Override
50    public void putObject(Object key, Object object) {
51      lock.lock();
52      try {
53        delegate.putObject(key, object);
54      } finally {
55        lock.unlock();
56      }
57    }
58  
59    @Override
60    public Object getObject(Object key) {
61      lock.lock();
62      try {
63        return delegate.getObject(key);
64      } finally {
65        lock.unlock();
66      }
67    }
68  
69    @Override
70    public Object removeObject(Object key) {
71      lock.lock();
72      try {
73        return delegate.removeObject(key);
74      } finally {
75        lock.unlock();
76      }
77    }
78  
79    @Override
80    public void clear() {
81      lock.lock();
82      try {
83        delegate.clear();
84      } finally {
85        lock.unlock();
86      }
87    }
88  
89    @Override
90    public int hashCode() {
91      return delegate.hashCode();
92    }
93  
94    @Override
95    public boolean equals(Object obj) {
96      return delegate.equals(obj);
97    }
98  
99  }