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 org.apache.shiro.util.CollectionUtils;
022
023import java.util.Collection;
024import java.util.Collections;
025import java.util.Map;
026import java.util.Set;
027
028/**
029 * A <code>MapCache</code> is a {@link Cache Cache} implementation that uses a backing {@link Map} instance to store
030 * and retrieve cached data.
031 *
032 * @since 1.0
033 */
034public class MapCache<K, V> implements Cache<K, V> {
035
036    /**
037     * Backing instance.
038     */
039    private final Map<K, V> map;
040
041    /**
042     * The name of this cache.
043     */
044    private final String name;
045
046    public MapCache(String name, Map<K, V> backingMap) {
047        if (name == null) {
048            throw new IllegalArgumentException("Cache name cannot be null.");
049        }
050        if (backingMap == null) {
051            throw new IllegalArgumentException("Backing map cannot be null.");
052        }
053        this.name = name;
054        this.map = backingMap;
055    }
056
057    public V get(K key) throws CacheException {
058        return map.get(key);
059    }
060
061    public V put(K key, V value) throws CacheException {
062        return map.put(key, value);
063    }
064
065    public V remove(K key) throws CacheException {
066        return map.remove(key);
067    }
068
069    public void clear() throws CacheException {
070        map.clear();
071    }
072
073    public int size() {
074        return map.size();
075    }
076
077    public Set<K> keys() {
078        Set<K> keys = map.keySet();
079        if (!keys.isEmpty()) {
080            return Collections.unmodifiableSet(keys);
081        }
082        return Collections.emptySet();
083    }
084
085    public Collection<V> values() {
086        Collection<V> values = map.values();
087        if (!CollectionUtils.isEmpty(values)) {
088            return Collections.unmodifiableCollection(values);
089        }
090        return Collections.emptySet();
091    }
092
093    public String toString() {
094        return new StringBuilder("MapCache '")
095                .append(name).append("' (")
096                .append(map.size())
097                .append(" entries)")
098                .toString();
099    }
100}