001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.cache.memory;
017:
018: import com.ibatis.sqlmap.client.SqlMapException;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * An enumeration for the values for the memory cache levels
025: */
026: public final class MemoryCacheLevel {
027:
028: private static Map cacheLevelMap = new HashMap();
029:
030: /**
031: * Constant for weak caching
032: * This cache model is probably the best choice in most cases. It will increase
033: * performance for popular results, but it will absolutely release the memory to
034: * be used in allocating other objects, assuming that the results are not currently
035: * in use.
036: */
037: public final static MemoryCacheLevel WEAK;
038:
039: /**
040: * Constant for soft caching.
041: * This cache model will reduce the likelihood of running out of memory in case the
042: * results are not currently in use and the memory is needed for other objects.
043: * However, this is not the most aggressive cache-model in that regard. Hence,
044: * memory still might be allocated and unavailable for more important objects.
045: */
046: public final static MemoryCacheLevel SOFT;
047:
048: /**
049: * Constant for strong caching.
050: * This cache model will guarantee that the results stay in memory until the cache
051: * is explicitly flushed. This is ideal for results that are:
052: * <ol>
053: * <li>very small</li>
054: * <li>absolutely static</li>
055: * <li>used very often</li>
056: * </ol>
057: * The advantage is that performance will be very good for this particular query.
058: * The disadvantage is that if the memory used by these results is needed, then it
059: * will not be released to make room for other objects (possibly more important
060: * objects).
061: */
062: public final static MemoryCacheLevel STRONG;
063:
064: private String referenceType;
065:
066: static {
067: WEAK = new MemoryCacheLevel("WEAK");
068: SOFT = new MemoryCacheLevel("SOFT");
069: STRONG = new MemoryCacheLevel("STRONG");
070:
071: cacheLevelMap.put(WEAK.referenceType, WEAK);
072: cacheLevelMap.put(SOFT.referenceType, SOFT);
073: cacheLevelMap.put(STRONG.referenceType, STRONG);
074: }
075:
076: /**
077: * Creates a new instance of CacheLevel
078: */
079: private MemoryCacheLevel(String type) {
080: this .referenceType = type;
081: }
082:
083: /**
084: * Getter for the reference type
085: *
086: * @return the type of reference type used
087: */
088: public String getReferenceType() {
089: return this .referenceType;
090: }
091:
092: /**
093: * Gets a MemoryCacheLevel by name
094: *
095: * @param refType the name of the reference type
096: * @return the MemoryCacheLevel that the name indicates
097: */
098: public static MemoryCacheLevel getByReferenceType(String refType) {
099: MemoryCacheLevel cacheLevel = (MemoryCacheLevel) cacheLevelMap
100: .get(refType);
101: if (cacheLevel == null) {
102: throw new SqlMapException(
103: "Error getting CacheLevel (reference type) for name: '"
104: + refType + "'.");
105: }
106: return cacheLevel;
107: }
108: }
|