001/*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved.            *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD      *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file.                                                     *
007 *                                                                           *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
009 *****************************************************************************/
010
011package org.picocontainer.behaviors;
012
013import org.picocontainer.Behavior;
014import org.picocontainer.ComponentAdapter;
015import org.picocontainer.ObjectReference;
016import org.picocontainer.references.SimpleReference;
017
018/**
019 * <p>
020 * {@link ComponentAdapter} implementation that caches the component instance.
021 * </p>
022 * <p>
023 * This adapter supports components with a lifecycle, as it is a
024 * {@link Behavior lifecycle manager} which will apply the delegate's
025 * {@link org.picocontainer.LifecycleStrategy lifecycle strategy} to the cached
026 * component instance. The lifecycle state is maintained so that the component
027 * instance behaves in the expected way: it can't be started if already started,
028 * it can't be started or stopped if disposed, it can't be stopped if not
029 * started, it can't be disposed if already disposed.
030 * </p>
031 * 
032 * @author Mauro Talevi
033 */
034@SuppressWarnings("serial")
035public class Cached<T> extends Stored<T> {
036
037
038    public Cached(ComponentAdapter delegate) {
039                this(delegate, new SimpleReference<Instance<T>>());
040        }
041
042        public Cached(ComponentAdapter delegate, ObjectReference<Instance<T>> instanceReference) {
043                super(delegate, instanceReference);
044        }
045
046    public String getDescriptor() {
047        return "Cached" + getLifecycleDescriptor();
048    }
049}