001/*
002 * (C) Copyright 2014 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Maxime Hilaire
016 *
017 */
018package org.nuxeo.ecm.core.cache;
019
020import java.io.Serializable;
021
022/**
023 * Class to implement mandatory check attributes before calling implementation of cache This enable to have the same
024 * behavior for any use of cache for all implementation of cache
025 *
026 * @since 6.0
027 */
028public class CacheAttributesChecker extends AbstractCache {
029
030    protected Cache cache;
031
032    protected CacheAttributesChecker(CacheDescriptor desc) {
033        super(desc);
034    }
035
036    void setCache(Cache cache) {
037        this.cache = cache;
038    }
039
040    public Cache getCache() {
041        return cache;
042    }
043
044    @Override
045    public Serializable get(String key) {
046        if (key == null) {
047            return null;
048        }
049        return cache.get(key);
050    }
051
052    @Override
053    public void invalidate(String key) {
054        if (key == null) {
055            throw new IllegalArgumentException(String.format("Can't invalidate a null key for the cache '%s'!", name));
056        }
057        cache.invalidate(key);
058    }
059
060    @Override
061    public void invalidateAll() {
062        cache.invalidateAll();
063    }
064
065    @Override
066    public void put(String key, Serializable value) {
067        if (key == null) {
068            throw new IllegalArgumentException(String.format("Can't put a null key for the cache '%s'!", name));
069        }
070        if (value == null) {
071            throw new IllegalArgumentException(String.format("Can't put a null value for the cache '%s'!", name));
072        }
073        cache.put(key, value);
074    }
075
076    @Override
077    public boolean hasEntry(String key) {
078        if (key == null) {
079            return false;
080        }
081        return cache.hasEntry(key);
082    }
083
084}