All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.cache.implementation.RIByValueSimpleCache Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright 2011 Terracotta, Inc.
 *  Copyright 2011 Oracle America Incorporated
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package javax.cache.implementation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * The reference implementation for JSR107.
 * 

* *

* * @param the type of keys maintained by this map * @param the type of mapped values* * @author Yannis Cosmadopoulos * @since 1.0 */ class RIByValueSimpleCache implements RISimpleCache { private final Serializer valueSerializer; private final Serializer keySerializer; private final RIByReferenceSimpleCache, Binary> store = new RIByReferenceSimpleCache, Binary>(); /** * Constructor * @param keySerializer the key serializer * @param valueSerializer the value serializer */ public RIByValueSimpleCache(Serializer keySerializer, Serializer valueSerializer) { this.keySerializer = keySerializer; this.valueSerializer = valueSerializer; } /** * {@inheritDoc} */ @Override public boolean putIfAbsent(K key, V value) { return store.putIfAbsent(createKeyHolder(key), createValueHolder(value)); } /** * {@inheritDoc} */ @Override public boolean replace(K key, V oldValue, V newValue) { return store.replace(createKeyHolder(key), createValueHolder(oldValue), createValueHolder(newValue)); } /** * {@inheritDoc} */ @Override public boolean replace(K key, V value) { return store.replace(createKeyHolder(key), createValueHolder(value)); } /** * {@inheritDoc} */ @Override public V getAndReplace(K key, V value) { return returnValue(store.getAndReplace(createKeyHolder(key), createValueHolder(value))); } /** * {@inheritDoc} */ @Override public int size() { return store.size(); } /** * {@inheritDoc} */ @Override public boolean containsKey(Object key) { return store.containsKey(createSearchObject(key)); } /** * {@inheritDoc} */ @Override public V get(Object key) { return returnValue(store.get(createSearchObject(key))); } /** * {@inheritDoc} */ @Override public void put(K key, V value) { store.put(createKeyHolder(key), createValueHolder(value)); } @Override public V getAndPut(K key, V value) { return returnValue(store.getAndPut(createKeyHolder(key), createValueHolder(value))); } /** * {@inheritDoc} */ @Override public boolean remove(Object key) { return store.remove(createSearchObject(key)); } /** * {@inheritDoc} */ @Override public boolean remove(Object key, V oldValue) { return store.remove(createSearchObject(key), createValueHolder(oldValue)); } /** * {@inheritDoc} */ @Override public V getAndRemove(Object key) { return returnValue(store.getAndRemove(createSearchObject(key))); } /** * {@inheritDoc} */ @Override public void putAll(Map map) { HashMap, Binary> toStore = new HashMap, Binary>(map.size()); for (Map.Entry entry : map.entrySet()) { toStore.put(createKeyHolder(entry.getKey()), createValueHolder(entry.getValue())); } store.putAll(toStore); } /** * {@inheritDoc} */ @Override public void removeAll() { store.removeAll(); } /** * {@inheritDoc} */ @Override public Iterator> iterator() { return new WrappedIterator(store.iterator()); } // utilities -------------------------------------------- private V returnValue(Binary binary) { return binary == null ? null : binary.get(); } private Binary createValueHolder(V value) { if (value == null) { throw new NullPointerException("value"); } return valueSerializer.createBinary(value); } private Binary createKeyHolder(K key) { if (key == null) { throw new NullPointerException("key"); } return keySerializer.createBinary(key); } private Object createSearchObject(Object o) { return new SearchObject(o); } /** * Wrapped object for search */ private static final class SearchObject { private final Object searchObject; private SearchObject(Object searchObject) { this.searchObject = searchObject; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Binary)) return false; Binary that = (Binary) o; return searchObject.hashCode() == that.hashCode() && searchObject.equals(that.get()); } @Override public int hashCode() { return searchObject.hashCode(); } } /** * Wrapped iterator * * @param key type * @param value type */ private static final class WrappedIterator implements Iterator> { private final Iterator, Binary>> iterator; private WrappedIterator(Iterator, Binary>> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Map.Entry next() { Map.Entry, Binary> next = iterator.next(); return new WrappedEntry(next); } @Override public void remove() { iterator.remove(); } /** * A wrapped entry * * @param key type * @param value type */ private static final class WrappedEntry implements Map.Entry { private final Map.Entry, Binary> entry; private WrappedEntry(Map.Entry, Binary> entry) { this.entry = entry; } @Override public K getKey() { return entry.getKey().get(); } @Override public V getValue() { return entry.getValue().get(); } @Override public V setValue(V v) { throw new UnsupportedOperationException(); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy