001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.cache4j;
018
019import org.conqat.lib.commons.cache4j.backend.ICacheBackend;
020import org.conqat.lib.commons.error.NeverThrownRuntimeException;
021import org.conqat.lib.commons.factory.IParameterizedFactory;
022
023/**
024 * Cache implementation that is synchronized to support multi-threaded
025 * environments.
026 * 
027 * @param <K>
028 *            the key type. This must have both {@link Object#equals(Object)}
029 *            and {@link Object#hashCode()} correctly implemented.
030 * @param <V>
031 *            the value type. It is generally recommended to use an immutable
032 *            type here, but this is not required.
033 * @param <X>
034 *            the type of exception thrown. If no exception will be throws, use
035 *            {@link NeverThrownRuntimeException}.
036 */
037public class SynchronizedCache<K, V, X extends Exception> extends BasicCache<K, V, X> {
038
039        /** Constructor. */
040        public SynchronizedCache(String name, IParameterizedFactory<V, K, X> factory, ICacheBackend<K, V> backend) {
041                super(name, factory, backend);
042        }
043
044        /**
045         * {@inheritDoc}
046         * <p>
047         * Overridden to provide synchronization.
048         */
049        @Override
050        public synchronized V obtain(K key) throws X {
051                return super.obtain(key);
052        }
053}