1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
20 import java.io.Serializable;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /***
26 * A typesafe enumeration of eviction policies.
27 * The policy used to evict elements from the {@link net.sf.ehcache.store.MemoryStore}.
28 * This can be one of:
29 * <ol>
30 * <li>LRU - least recently used
31 * <li>LFU - least frequently used
32 * <li>FIFO - first in first out, the oldest element by creation time
33 * </ol>
34 * The default value is LRU
35 *
36 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
37 * @version $Id: MemoryStoreEvictionPolicy.html 13146 2011-08-01 17:12:39Z oletizi $
38 * @since 1.2
39 */
40 public final class MemoryStoreEvictionPolicy implements Serializable {
41
42 /***
43 * LRU - least recently used.
44 */
45 public static final MemoryStoreEvictionPolicy LRU = new MemoryStoreEvictionPolicy("LRU");
46
47 /***
48 * LFU - least frequently used.
49 */
50
51 public static final MemoryStoreEvictionPolicy LFU = new MemoryStoreEvictionPolicy("LFU");
52
53 /***
54 * FIFO - first in first out, the oldest element by creation time.
55 */
56 public static final MemoryStoreEvictionPolicy FIFO = new MemoryStoreEvictionPolicy("FIFO");
57
58 private static final Logger LOG = LoggerFactory.getLogger(MemoryStoreEvictionPolicy.class.getName());
59
60 private final String myName;
61
62 /***
63 * This class should not be subclassed or have instances created.
64 * @param policy
65 */
66 private MemoryStoreEvictionPolicy(String policy) {
67 myName = policy;
68 }
69
70 /***
71 * @return a String representation of the policy
72 */
73 @Override
74 public String toString() {
75 return myName;
76 }
77
78 /***
79 * Converts a string representation of the policy into a policy.
80 *
81 * @param policy either LRU, LFU or FIFO
82 * @return one of the static instances
83 */
84 public static MemoryStoreEvictionPolicy fromString(String policy) {
85 if (policy != null) {
86 if (policy.equalsIgnoreCase("LRU")) {
87 return LRU;
88 } else if (policy.equalsIgnoreCase("LFU")) {
89 return LFU;
90 } else if (policy.equalsIgnoreCase("FIFO")) {
91 return FIFO;
92 }
93 }
94 LOG.warn("The memoryStoreEvictionPolicy of {} cannot be resolved. The policy will be set to LRU", policy);
95 return LRU;
96 }
97
98 /***
99 * Enum for {@link MemoryStoreEvictionPolicy}
100 *
101 */
102 public static enum MemoryStoreEvictionPolicyEnum {
103 /***
104 * Value for {@link MemoryStoreEvictionPolicy#LFU}
105 */
106 LFU,
107 /***
108 * Value for {@link MemoryStoreEvictionPolicy#LRU}
109 */
110 LRU,
111 /***
112 * Value for {@link MemoryStoreEvictionPolicy#FIFO}
113 */
114 FIFO;
115 }
116 }