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.commons.jcs.jcache;
020
021import javax.cache.Cache;
022import javax.cache.processor.MutableEntry;
023
024public class JCSMutableEntry<K, V> implements MutableEntry<K, V>
025{
026    private final Cache<K, V> cache;
027    private final K key;
028
029    public JCSMutableEntry(final Cache<K, V> cache, final K key)
030    {
031        this.cache = cache;
032        this.key = key;
033    }
034
035    @Override
036    public boolean exists()
037    {
038        return cache.containsKey(key);
039    }
040
041    @Override
042    public void remove()
043    {
044        cache.remove(key);
045    }
046
047    @Override
048    public void setValue(final V value)
049    {
050        cache.put(key, value);
051    }
052
053    @Override
054    public K getKey()
055    {
056        return key;
057    }
058
059    @Override
060    public V getValue()
061    {
062        return cache.get(key);
063    }
064
065    @Override
066    public <T> T unwrap(final Class<T> clazz)
067    {
068        if (clazz.isInstance(this))
069        {
070            return clazz.cast(this);
071        }
072        throw new IllegalArgumentException(clazz.getName() + " not supported in unwrap");
073    }
074}