001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.cache;
020
021import java.util.Collection;
022import java.util.Collections;
023import java.util.Map;
024import java.util.Set;
025
026/**
027 * A <code>MapCache</code> is a {@link Cache Cache} implementation that uses a backing {@link Map} instance to store
028 * and retrieve cached data.
029 *
030 * @since 1.0
031 */
032public class MapCache<K, V> implements Cache<K, V> {
033
034    /**
035     * Backing instance.
036     */
037    private final Map<K, V> map;
038
039    /**
040     * The name of this cache.
041     */
042    private final String name;
043
044    public MapCache(String name, Map<K, V> backingMap) {
045        if (name == null) {
046            throw new IllegalArgumentException("Cache name cannot be null.");
047        }
048        if (backingMap == null) {
049            throw new IllegalArgumentException("Backing map cannot be null.");
050        }
051        this.name = name;
052        this.map = backingMap;
053    }
054
055    public V get(K key) throws CacheException {
056        return map.get(key);
057    }
058
059    public V put(K key, V value) throws CacheException {
060        return map.put(key, value);
061    }
062
063    public V remove(K key) throws CacheException {
064        return map.remove(key);
065    }
066
067    public void clear() throws CacheException {
068        map.clear();
069    }
070
071    public int size() {
072        return map.size();
073    }
074
075    public Set<K> keys() {
076        Set<K> keys = map.keySet();
077        if (!keys.isEmpty()) {
078            return Collections.unmodifiableSet(keys);
079        }
080        return Collections.emptySet();
081    }
082
083    public Collection<V> values() {
084        Collection<V> values = map.values();
085        if (!values.isEmpty()) {
086            return Collections.unmodifiableCollection(values);
087        }
088        return Collections.emptyList();
089    }
090
091    public String toString() {
092        return new StringBuilder("MapCache '")
093                .append(name).append("' (")
094                .append(map.size())
095                .append(" entries)")
096                .toString();
097    }
098}