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.backend;
018
019import java.lang.ref.SoftReference;
020
021/**
022 * Enumeration of supported caching strategies.
023 */
024public enum ECachingStrategy {
025
026        /** Performs no caching at all. */
027        OFF,
028
029        /**
030         * Caches exactly one element (basically the same as an LRU cache with size
031         * 1.
032         */
033        SINGLE,
034
035        /**
036         * Last recently used caching strategy. The parameter denotes the maximal
037         * number of elements stored.
038         */
039        LRU,
040
041        /**
042         * Memory sensitive caching using {@link SoftReference}s. This allows the
043         * cache to keep elements as long as memory is available, but free memory
044         * when requested by the garbage collector.
045         */
046        MEMORY,
047
048        /**
049         * Unlimited caching, i.e. the cache never discard data. This can lead to
050         * situations where the memory is exceeded.
051         */
052        UNLIMITED;
053
054        /**
055         * Returns the cache backend for the strategy (factory method).
056         * 
057         * @param parameter
058         *            the parameter used for the caching strategy. This is ignored
059         *            by those strategies that are not parameterizable.
060         */
061        public <K, V> ICacheBackend<K, V> getBackend(int parameter) {
062                switch (this) {
063                case OFF:
064                        return new NoneCacheBackend<K, V>();
065                case SINGLE:
066                        return new SingleElementCacheBackend<K, V>();
067                case LRU:
068                        if (parameter < 1) {
069                                parameter = 10;
070                        }
071                        return new LRUCacheBackend<K, V>(parameter);
072                case MEMORY:
073                        return new SoftRefCacheBackend<K, V>();
074                case UNLIMITED:
075                        return new UnlimitedCacheBackend<K, V>();
076                default:
077                        throw new AssertionError("Unknown enum value: " + this);
078                }
079        }
080}