View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.cache;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Map;
24  import java.util.Set;
25  
26  /**
27   * A <code>MapCache</code> is a {@link Cache Cache} implementation that uses a backing {@link Map} instance to store
28   * and retrieve cached data.
29   *
30   * @since 1.0
31   */
32  public class MapCache<K, V> implements Cache<K, V> {
33  
34      /**
35       * Backing instance.
36       */
37      private final Map<K, V> map;
38  
39      /**
40       * The name of this cache.
41       */
42      private final String name;
43  
44      public MapCache(String name, Map<K, V> backingMap) {
45          if (name == null) {
46              throw new IllegalArgumentException("Cache name cannot be null.");
47          }
48          if (backingMap == null) {
49              throw new IllegalArgumentException("Backing map cannot be null.");
50          }
51          this.name = name;
52          this.map = backingMap;
53      }
54  
55      public V get(K key) throws CacheException {
56          return map.get(key);
57      }
58  
59      public V put(K key, V value) throws CacheException {
60          return map.put(key, value);
61      }
62  
63      public V remove(K key) throws CacheException {
64          return map.remove(key);
65      }
66  
67      public void clear() throws CacheException {
68          map.clear();
69      }
70  
71      public int size() {
72          return map.size();
73      }
74  
75      public Set<K> keys() {
76          Set<K> keys = map.keySet();
77          if (!keys.isEmpty()) {
78              return Collections.unmodifiableSet(keys);
79          }
80          return Collections.emptySet();
81      }
82  
83      public Collection<V> values() {
84          Collection<V> values = map.values();
85          if (!map.isEmpty()) {
86              return Collections.unmodifiableCollection(values);
87          }
88          return Collections.emptySet();
89      }
90  
91      public String toString() {
92          return new StringBuilder("MapCache '")
93                  .append(name).append("' (")
94                  .append(map.size())
95                  .append(" entries)")
96                  .toString();
97      }
98  }