1 /**
2 * Copyright 2003-2006 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.store;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23 * A typesafe enumeration of eviction policies.
24 * The policy used to evict elements from the {@link net.sf.ehcache.store.MemoryStore}.
25 * This can be one of:
26 * <ol>
27 * <li>LRU - least recently used
28 * <li>LFU - least frequently used
29 * <li>FIFO - first in first out, the oldest element by creation time
30 * </ol>
31 * The default value is LRU
32 *
33 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
34 * @version $Id: MemoryStoreEvictionPolicy.java 52 2006-04-24 14:50:03Z gregluck $
35 * @since 1.2
36 */
37 public final class MemoryStoreEvictionPolicy {
38
39 /**
40 * LRU - least recently used.
41 */
42 public static final MemoryStoreEvictionPolicy LRU = new MemoryStoreEvictionPolicy("LRU");
43
44 /**
45 * LFU - least frequently used.
46 */
47
48 public static final MemoryStoreEvictionPolicy LFU = new MemoryStoreEvictionPolicy("LFU");
49
50 /**
51 * FIFO - first in first out, the oldest element by creation time.
52 */
53 public static final MemoryStoreEvictionPolicy FIFO = new MemoryStoreEvictionPolicy("FIFO");
54
55 private static final Log LOG = LogFactory.getLog(MemoryStoreEvictionPolicy.class.getName());
56
57
58 private final String myName;
59
60 /**
61 * This class should not be subclassed or have instances created.
62 * @param policy
63 */
64 private MemoryStoreEvictionPolicy(String policy) {
65 myName = policy;
66 }
67
68 /**
69 * @return a String representation of the policy
70 */
71 public String toString() {
72 return myName;
73 }
74
75 /**
76 * Converts a string representation of the policy into a policy.
77 *
78 * @param policy either LRU, LFU or FIFO
79 * @return one of the static instances
80 */
81 public static MemoryStoreEvictionPolicy fromString(String policy) {
82 if (policy != null) {
83 if (policy.equalsIgnoreCase("LRU")) {
84 return LRU;
85 } else if (policy.equalsIgnoreCase("LFU")) {
86 return LFU;
87 } else if (policy.equalsIgnoreCase("FIFO")) {
88 return FIFO;
89 }
90 }
91
92 if (LOG.isWarnEnabled()) {
93 LOG.warn("The memoryStoreEvictionPolicy of " + policy + " cannot be resolved. The policy will be" +
94 " set to LRU");
95 }
96 return LRU;
97 }
98 }