KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > memory > MemoryCacheLevel


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.sqlmap.engine.cache.memory;
17
18 import com.ibatis.sqlmap.client.SqlMapException;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * An enumeration for the values for the memory cache levels
25  */

26 public final class MemoryCacheLevel {
27
28   private static Map JavaDoc cacheLevelMap = new HashMap JavaDoc();
29
30   /**
31    * Constant for weak caching
32    * This cache model is probably the best choice in most cases. It will increase
33    * performance for popular results, but it will absolutely release the memory to
34    * be used in allocating other objects, assuming that the results are not currently
35    * in use.
36    */

37   public final static MemoryCacheLevel WEAK;
38
39   /**
40    * Constant for soft caching.
41    * This cache model will reduce the likelihood of running out of memory in case the
42    * results are not currently in use and the memory is needed for other objects.
43    * However, this is not the most aggressive cache-model in that regard. Hence,
44    * memory still might be allocated and unavailable for more important objects.
45    */

46   public final static MemoryCacheLevel SOFT;
47
48   /**
49    * Constant for strong caching.
50    * This cache model will guarantee that the results stay in memory until the cache
51    * is explicitly flushed. This is ideal for results that are:
52    * <ol>
53    * <li>very small</li>
54    * <li>absolutely static</li>
55    * <li>used very often</li>
56    * </ol>
57    * The advantage is that performance will be very good for this particular query.
58    * The disadvantage is that if the memory used by these results is needed, then it
59    * will not be released to make room for other objects (possibly more important
60    * objects).
61    */

62   public final static MemoryCacheLevel STRONG;
63
64   private String JavaDoc referenceType;
65
66   static {
67     WEAK = new MemoryCacheLevel("WEAK");
68     SOFT = new MemoryCacheLevel("SOFT");
69     STRONG = new MemoryCacheLevel("STRONG");
70
71     cacheLevelMap.put(WEAK.referenceType, WEAK);
72     cacheLevelMap.put(SOFT.referenceType, SOFT);
73     cacheLevelMap.put(STRONG.referenceType, STRONG);
74   }
75
76
77   /**
78    * Creates a new instance of CacheLevel
79    */

80   private MemoryCacheLevel(String JavaDoc type) {
81     this.referenceType = type;
82   }
83
84   /**
85    * Getter for the reference type
86    *
87    * @return the type of reference type used
88    */

89   public String JavaDoc getReferenceType() {
90     return this.referenceType;
91   }
92
93   /**
94    * Gets a MemoryCacheLevel by name
95    *
96    * @param refType the name of the reference type
97    * @return the MemoryCacheLevel that the name indicates
98    */

99   public static MemoryCacheLevel getByReferenceType(String JavaDoc refType) {
100     MemoryCacheLevel cacheLevel = (MemoryCacheLevel) cacheLevelMap.get(refType);
101     if (cacheLevel == null) {
102       throw new SqlMapException("Error getting CacheLevel (reference type) for name: '" + refType + "'.");
103     }
104     return cacheLevel;
105   }
106 }
107
108
Popular Tags