1 /**
2 * Copyright 2003-2007 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 import java.io.Serializable;
23
24 /**
25 * A typesafe enumeration of eviction policies.
26 * The policy used to evict elements from the {@link net.sf.ehcache.store.MemoryStore}.
27 * This can be one of:
28 * <ol>
29 * <li>LRU - least recently used
30 * <li>LFU - least frequently used
31 * <li>FIFO - first in first out, the oldest element by creation time
32 * </ol>
33 * The default value is LRU
34 *
35 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
36 * @version $Id: MemoryStoreEvictionPolicy.java 512 2007-07-10 09:18:45Z gregluck $
37 * @since 1.2
38 */
39 public final class MemoryStoreEvictionPolicy implements Serializable {
40
41 /**
42 * LRU - least recently used.
43 */
44 public static final MemoryStoreEvictionPolicy LRU = new MemoryStoreEvictionPolicy("LRU");
45
46 /**
47 * LFU - least frequently used.
48 */
49
50 public static final MemoryStoreEvictionPolicy LFU = new MemoryStoreEvictionPolicy("LFU");
51
52 /**
53 * FIFO - first in first out, the oldest element by creation time.
54 */
55 public static final MemoryStoreEvictionPolicy FIFO = new MemoryStoreEvictionPolicy("FIFO");
56
57 private static final Log LOG = LogFactory.getLog(MemoryStoreEvictionPolicy.class.getName());
58
59 private final String myName;
60
61 /**
62 * This class should not be subclassed or have instances created.
63 * @param policy
64 */
65 private MemoryStoreEvictionPolicy(String policy) {
66 myName = policy;
67 }
68
69 /**
70 * @return a String representation of the policy
71 */
72 public String toString() {
73 return myName;
74 }
75
76 /**
77 * Converts a string representation of the policy into a policy.
78 *
79 * @param policy either LRU, LFU or FIFO
80 * @return one of the static instances
81 */
82 public static MemoryStoreEvictionPolicy fromString(String policy) {
83 if (policy != null) {
84 if (policy.equalsIgnoreCase("LRU")) {
85 return LRU;
86 } else if (policy.equalsIgnoreCase("LFU")) {
87 return LFU;
88 } else if (policy.equalsIgnoreCase("FIFO")) {
89 return FIFO;
90 }
91 }
92
93 if (LOG.isWarnEnabled()) {
94 LOG.warn("The memoryStoreEvictionPolicy of " + policy + " cannot be resolved. The policy will be" +
95 " set to LRU");
96 }
97 return LRU;
98 }
99 }