001: /*
002: * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.cache;
017:
018: import java.util.Collection;
019: import java.util.Map;
020:
021: import net.sf.dozer.util.mapping.stats.GlobalStatistics;
022: import net.sf.dozer.util.mapping.stats.StatisticTypeConstants;
023: import net.sf.dozer.util.mapping.stats.StatisticsManagerIF;
024:
025: import org.apache.commons.collections.map.LinkedMap;
026: import org.apache.commons.lang.builder.ReflectionToStringBuilder;
027: import org.apache.commons.lang.builder.ToStringStyle;
028:
029: /**
030: * Internal class that provides an interface to a single cache. Holds all of the cache entries for the cache. Only
031: * intended for internal use.
032: *
033: * @author tierney.matt
034: */
035: public class Cache {
036: private final String name;
037:
038: private long maximumSize;
039: private final Map cacheMap;
040: private long hitCount;
041: private long missCount;
042:
043: public Cache(String name, long maximumSize) {
044: this .name = name;
045: this .maximumSize = maximumSize;
046: this .cacheMap = new CacheLinkedHashMap();
047: }
048:
049: public void clear() {
050: cacheMap.clear();
051: }
052:
053: public synchronized void put(CacheEntry cacheEntry) {
054: if (cacheEntry == null) {
055: throw new IllegalArgumentException(
056: "Cache Entry cannot be null");
057: }
058: cacheMap.put(cacheEntry.getKey(), cacheEntry);
059: if (cacheMap.size() > maximumSize) {
060: // remove eldest entry
061: ((LinkedMap) cacheMap).remove(cacheMap.size() - 1);
062: }
063: }
064:
065: public CacheEntry get(Object key) {
066: if (key == null) {
067: throw new IllegalArgumentException("Key cannot be null");
068: }
069: CacheEntry result = (CacheEntry) cacheMap.get(key);
070: StatisticsManagerIF statMgr = GlobalStatistics.getInstance()
071: .getStatsMgr();
072: if (result != null) {
073: hitCount++;
074: statMgr.increment(StatisticTypeConstants.CACHE_HIT_COUNT,
075: name);
076: } else {
077: missCount++;
078: statMgr.increment(StatisticTypeConstants.CACHE_MISS_COUNT,
079: name);
080: }
081: return result;
082: }
083:
084: public Collection getEntries() {
085: return cacheMap.values();
086: }
087:
088: public String getName() {
089: return name;
090: }
091:
092: public int getSize() {
093: return cacheMap.size();
094: }
095:
096: public long getMaxSize() {
097: return maximumSize;
098: }
099:
100: public long getHitCount() {
101: return hitCount;
102: }
103:
104: public long getMissCount() {
105: return missCount;
106: }
107:
108: public String toString() {
109: return ReflectionToStringBuilder.toString(this ,
110: ToStringStyle.MULTI_LINE_STYLE);
111: }
112:
113: private class CacheLinkedHashMap extends LinkedMap {
114: // Order the map by which its entries were last accessed, from least-recently accessed to most-recently (access-order)
115: public CacheLinkedHashMap() {
116: super (100, 0.75F);
117: }
118: }
119:
120: }
|