001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Maxime Hilaire
018 *
019 */
020package org.nuxeo.ecm.core.cache;
021
022import java.io.Serializable;
023import java.util.Set;
024
025/**
026 * Class to implement mandatory check attributes before calling implementation of cache This enable to have the same
027 * behavior for any use of cache for all implementation of cache
028 *
029 * @since 6.0
030 */
031public class CacheAttributesChecker extends AbstractCache {
032
033    protected Cache cache;
034
035    protected CacheAttributesChecker(CacheDescriptor desc) {
036        super(desc);
037    }
038
039    void setCache(Cache cache) {
040        this.cache = cache;
041    }
042
043    public Cache getCache() {
044        return cache;
045    }
046
047    @Override
048    public Serializable get(String key) {
049        if (key == null) {
050            return null;
051        }
052        return cache.get(key);
053    }
054
055    @Override
056    public Set<String> keySet() {
057        return cache.keySet();
058    }
059
060    @Override
061    public void invalidate(String key) {
062        if (key == null) {
063            throw new IllegalArgumentException(String.format("Can't invalidate a null key for the cache '%s'!", name));
064        }
065        cache.invalidate(key);
066    }
067
068    @Override
069    public void invalidateAll() {
070        cache.invalidateAll();
071    }
072
073    @Override
074    public void put(String key, Serializable value) {
075        if (key == null) {
076            throw new IllegalArgumentException(String.format("Can't put a null key for the cache '%s'!", name));
077        }
078        cache.put(key, value);
079    }
080
081    @Override
082    public boolean hasEntry(String key) {
083        if (key == null) {
084            return false;
085        }
086        return cache.hasEntry(key);
087    }
088
089}