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 org.apache.shiro.util.CollectionUtils;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Map;
26  import java.util.Set;
27  
28  /**
29   * A <code>MapCache</code> is a {@link Cache Cache} implementation that uses a backing {@link Map} instance to store
30   * and retrieve cached data.
31   *
32   * @since 1.0
33   */
34  public class MapCache<K, V> implements Cache<K, V> {
35  
36      /**
37       * Backing instance.
38       */
39      private final Map<K, V> map;
40  
41      /**
42       * The name of this cache.
43       */
44      private final String name;
45  
46      public MapCache(String name, Map<K, V> backingMap) {
47          if (name == null) {
48              throw new IllegalArgumentException("Cache name cannot be null.");
49          }
50          if (backingMap == null) {
51              throw new IllegalArgumentException("Backing map cannot be null.");
52          }
53          this.name = name;
54          this.map = backingMap;
55      }
56  
57      public V get(K key) throws CacheException {
58          return map.get(key);
59      }
60  
61      public V put(K key, V value) throws CacheException {
62          return map.put(key, value);
63      }
64  
65      public V remove(K key) throws CacheException {
66          return map.remove(key);
67      }
68  
69      public void clear() throws CacheException {
70          map.clear();
71      }
72  
73      public int size() {
74          return map.size();
75      }
76  
77      public Set<K> keys() {
78          Set<K> keys = map.keySet();
79          if (!keys.isEmpty()) {
80              return Collections.unmodifiableSet(keys);
81          }
82          return Collections.emptySet();
83      }
84  
85      public Collection<V> values() {
86          Collection<V> values = map.values();
87          if (!CollectionUtils.isEmpty(values)) {
88              return Collections.unmodifiableCollection(values);
89          }
90          return Collections.emptySet();
91      }
92  
93      public String toString() {
94          return new StringBuilder("MapCache '")
95                  .append(name).append("' (")
96                  .append(map.size())
97                  .append(" entries)")
98                  .toString();
99      }
100 }