001 /*--------------------------------------------------------------------------+ 002 $Id: LRUCachingParameterizedFactory.java 27446 2010-04-11 09:57:00Z heineman $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.factory; 019 020 import edu.tum.cs.commons.assertion.CCSMPre; 021 import edu.tum.cs.commons.cache.LRUCacheBase; 022 import edu.tum.cs.commons.cache.LRUStraightCacheBase; 023 import edu.tum.cs.commons.error.NeverThrownRuntimeException; 024 025 /** 026 * Caching factory which can reuse created objects. Creation is delegated to an 027 * inner factory, while for caching a {@link LRUCacheBase} is used. 028 * 029 * @param <T> 030 * Type that gets created by the factory. 031 * @param <P> 032 * Parameter that is used for creation. 033 * @param <X> 034 * Exception that can get thrown during execution of the factory 035 * method. If no exception is thrown, use 036 * {@link NeverThrownRuntimeException}. 037 * 038 * @author hummelb 039 * @author $Author: heineman $ 040 * @version $Rev: 27446 $ 041 * @levd.rating GREEN Hash: 5D97F2DF67786BC74BEB3611B6017360 042 */ 043 public class LRUCachingParameterizedFactory<T, P, X extends Exception> 044 extends LRUStraightCacheBase<P, T, X> implements 045 IParameterizedFactory<T, P, X> { 046 047 /** The wrapped factory we delegate to. */ 048 private final IParameterizedFactory<T, P, X> inner; 049 050 /** Constructor. */ 051 public LRUCachingParameterizedFactory( 052 IParameterizedFactory<T, P, X> inner, int maxSize) { 053 super(maxSize); 054 CCSMPre.isNotNull(inner, "Delegate factory may not be null!"); 055 this.inner = inner; 056 } 057 058 /** 059 * {@inheritDoc} 060 * <p> 061 * Delegates to the inner factory. 062 */ 063 @Override 064 protected T obtainItem(P identifier) throws X { 065 return inner.create(identifier); 066 } 067 068 /** 069 * {@inheritDoc} 070 * <p> 071 * Forwards to the {@link #getItem(Object)} method. 072 */ 073 @Override 074 public T create(P parameter) throws X { 075 return getItem(parameter); 076 } 077 078 }