001 /*--------------------------------------------------------------------------+
002 $Id: SoftRefCachingParameterizedFactory.java 26268 2010-02-18 10:44:30Z juergens $
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.SoftRefStraightCacheBase;
022 import edu.tum.cs.commons.error.NeverThrownRuntimeException;
023
024 /**
025 * Caching factory which can reuse created objects. Creation is delegated to an
026 * inner factory, while for caching a {@link SoftRefStraightCacheBase} is used.
027 *
028 * @param <T>
029 * Type that gets created by the factory.
030 * @param <P>
031 * Parameter that is used for creation.
032 * @param <X>
033 * Exception that can get thrown during execution of the factory
034 * method. If no exception is thrown, use
035 * {@link NeverThrownRuntimeException}.
036 *
037 * @author hummelb
038 * @author $Author: juergens $
039 * @version $Rev: 26268 $
040 * @levd.rating GREEN Hash: B334A34CE1769C1621A5E7A691D2270C
041 */
042 public class SoftRefCachingParameterizedFactory<T, P, X extends Exception>
043 extends SoftRefStraightCacheBase<P, T, X> implements
044 IParameterizedFactory<T, P, X> {
045
046 /** The wrapped factory we delegate to. */
047 private final IParameterizedFactory<T, P, X> inner;
048
049 /** Constructor. */
050 public SoftRefCachingParameterizedFactory(
051 IParameterizedFactory<T, P, X> inner) {
052 CCSMPre.isNotNull(inner, "Delegate factory may not be null!");
053 this.inner = inner;
054 }
055
056 /**
057 * {@inheritDoc}
058 * <p>
059 * Delegates to the inner factory.
060 */
061 @Override
062 protected T obtainItem(P identifier) throws X {
063 return inner.create(identifier);
064 }
065
066 /**
067 * {@inheritDoc}
068 * <p>
069 * Forwards to the {@link #getItem(Object)} method.
070 */
071 @Override
072 public T create(P parameter) throws X {
073 return getItem(parameter);
074 }
075
076 }