Author: simonetripodi Date: Mon Jan 11 17:31:32 2010 New Revision: 897960 URL: http://svn.apache.org/viewvc?rev=897960&view=rev Log: first checkin of InMemoryLRUCache DigesterLoader stores RuleSet for visited classes on an InMemoryLRUCache instance
Added: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java (with props) Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java Modified: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java?rev=897960&r1=897959&r2=897960&view=diff ============================================================================== --- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java (original) +++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/DigesterLoader.java Mon Jan 11 17:31:32 2010 @@ -21,13 +21,12 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.LinkedHashMap; -import java.util.Map; import org.apache.commons.digester.Digester; import org.apache.commons.digester.RuleSet; import org.apache.commons.digester.annotations.reflect.MethodArgument; import org.apache.commons.digester.annotations.utils.AnnotationUtils; +import org.apache.commons.digester.annotations.utils.InMemoryLRUCache; /** * This class manages the creation of Digester instances analyzing target classes @@ -42,33 +41,11 @@ public final class DigesterLoader { /** - * The fixed cache size. - */ - private static final int CACHE_SIZE = 255; - - /** - * The fixed cache load facor. - */ - private static final float LOAD_FACTOR = 0.75f; - - /** - * The fixed cache capacity. - */ - private static final int CACHE_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) + 1; - - /** * In-memory LRU cache that stores already analyzed classes and relative * {...@link RuleSet}. */ - private static final Map<Class<?>, FromAnnotationsRuleSet> CACHED_RULESET = - new LinkedHashMap<Class<?>, FromAnnotationsRuleSet>(CACHE_CAPACITY, LOAD_FACTOR) { - - private static final long serialVersionUID = 1L; - - protected boolean removeEldestEntry(Map.Entry<Class<?>,FromAnnotationsRuleSet> eldest) { - return size() > CACHE_SIZE; - }; - }; + private static final InMemoryLRUCache<Class<?>, FromAnnotationsRuleSet> CACHED_RULESET = + new InMemoryLRUCache<Class<?>, FromAnnotationsRuleSet>(); /** * This class can't be instantiated. @@ -178,9 +155,11 @@ } /** - * - * @param annotation - * @param element + * Handles the current visited element and related annotation, invoking the + * right handler putting the rule provider in the rule set. + * + * @param annotation the current visited annotation. + * @param element the current visited element. */ private static void handle(Annotation annotation, AnnotatedElement element, FromAnnotationsRuleSet ruleSet) { Class<?> annotationType = annotation.annotationType(); Added: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java?rev=897960&view=auto ============================================================================== --- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java (added) +++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java Mon Jan 11 17:31:32 2010 @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.digester.annotations.utils; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Simple in-memory LRU cache implementation. + * + * @version $Id$ + * @since 2.1 + */ +public final class InMemoryLRUCache<K, V> { + + /** + * The fixed cache size. + */ + private static final int CACHE_SIZE = 255; + + /** + * The fixed cache load facor. + */ + private static final float LOAD_FACTOR = 0.75f; + + /** + * The fixed cache capacity. + */ + private static final int CACHE_CAPACITY = (int) Math.ceil(CACHE_SIZE / LOAD_FACTOR) + 1; + + /** + * The map that implements the LRU cache. + */ + private final Map<K, V> data = new LinkedHashMap<K, V>(CACHE_CAPACITY, LOAD_FACTOR) { + /** + * This class serialVersionUID. + */ + private static final long serialVersionUID = 1L; + + /** + * {...@inheritdoc} + */ + protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { + return size() > CACHE_SIZE; + } + }; + + /** + * Returns true if this cache contains a mapping for the specified key. + * + * @param key key whose presence in this map is to be tested. + * @return true if this map contains a mapping for the specified key, false + * otherwise. + */ + public boolean containsKey(K key) { + checkKey(key); + return this.data.containsKey(key); + } + + /** + * Returns the value to which the specified key is cached, or null if this + * cache contains no mapping for the key. + * + * Key parameter must not be null. + * + * @param key the key has to be checked it is present, it must not be null. + * @return the value to which the specified key is cached, null if this + * cache contains no mapping for the key. + */ + public V get(K key) { + checkKey(key); + return this.data.get(key); + } + + /** + * Associates the specified value with the specified key in this cache. + * + * Key parameter must not be null. + * + * @param key key with which the specified value is to be associated. + * @param value value to be associated with the specified key. + */ + public void put(K key, V value) { + checkKey(key); + this.data.put(key, value); + } + + /** + * Verify that a key is not null. + * + * @param <T> the generic key type. + * @param key the key object. + */ + private static <T> void checkKey(T key) { + if (key == null) { + throw new IllegalArgumentException("null keys not supported"); + } + } + +} Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/InMemoryLRUCache.java ------------------------------------------------------------------------------ svn:mime-type = text/plain