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.factory.IParameterizedFactory;
021
022/**
023 * Enumeration of strategies for supporting threads.
024 */
025public enum ECacheThreadSupport {
026
027        /** No synchronization. */
028        NONE,
029
030        /** Java synchronization (i.e. single cache with synchronized access). */
031        SYNCHRONIZED,
032
033        /** Thread local implementation, i.e. each thread has a separate cache. */
034        THREADLOCAL;
035
036        /** Factory method for creating a cache from a synchronization strategy. */
037        public <K, V, X extends Exception> ICache<K, V, X> createCache(String name, IParameterizedFactory<V, K, X> factory,
038                        ICacheBackend<K, V> backend) {
039                switch (this) {
040                case NONE:
041                        return new BasicCache<K, V, X>(name, factory, backend);
042                case SYNCHRONIZED:
043                        return new SynchronizedCache<K, V, X>(name, factory, backend);
044                case THREADLOCAL:
045                        return new ThreadLocalCache<K, V, X>(name, factory, backend);
046                default:
047                        throw new AssertionError("Unknown enum value: " + this);
048                }
049        }
050}