1 /**
2  *  Copyright 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.java 5631 2012-05-10 08:31:33Z teck $
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     /**
59      * FIFO - first in first out, the oldest element by creation time.
60      */

61     public static final MemoryStoreEvictionPolicy CLOCK = new MemoryStoreEvictionPolicy("CLOCK");
62
63     private static final Logger LOG = LoggerFactory.getLogger(MemoryStoreEvictionPolicy.class.getName());
64
65     private final String myName;
66
67     /**
68      * This class should not be subclassed or have instances created.
69      * @param policy
70      */

71     private MemoryStoreEvictionPolicy(String policy) {
72         myName = policy;
73     }
74
75     /**
76      * @return a String representation of the policy
77      */

78     @Override
79     public String toString() {
80         return myName;
81     }
82
83     /**
84      * Converts a string representation of the policy into a policy.
85      *
86      * @param policy either LRU, LFU or FIFO
87      * @return one of the static instances
88      */

89     public static MemoryStoreEvictionPolicy fromString(String policy) {
90         if (policy != null) {
91             if (policy.equalsIgnoreCase("LRU")) {
92                 return LRU;
93             } else if (policy.equalsIgnoreCase("LFU")) {
94                 return LFU;
95             } else if (policy.equalsIgnoreCase("FIFO")) {
96                 return FIFO;
97             } else if (policy.equalsIgnoreCase("CLOCK")) {
98                 return CLOCK;
99             }
100         }
101             LOG.warn("The memoryStoreEvictionPolicy of {} cannot be resolved. The policy will be set to LRU", policy);
102         return LRU;
103     }
104     
105     /**
106      * Enum for {@link MemoryStoreEvictionPolicy}
107      * 
108      */

109     public static enum MemoryStoreEvictionPolicyEnum {
110         /**
111          * Value for {@link MemoryStoreEvictionPolicy#LFU}
112          */

113         LFU,
114         /**
115          * Value for {@link MemoryStoreEvictionPolicy#LRU}
116          */

117         LRU,
118         /**
119          * Value for {@link MemoryStoreEvictionPolicy#FIFO}
120          */

121         FIFO;
122     }
123 }
124