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     *****************************************************************************/
009    package org.picocontainer.references;
010    
011    import org.picocontainer.ObjectReference;
012    
013    import java.util.Map;
014    
015    /**
016     * Gets and sets references on a map stored in Thread Local  
017     * @author Paul Hammant
018     */
019    public class ThreadLocalMapObjectReference<T> implements ObjectReference<T> {
020        private final ThreadLocal<Map<Object, T>> threadLocal;
021        private final Object componentKey;
022    
023        public ThreadLocalMapObjectReference(ThreadLocal<Map<Object, T>> threadLocal, Object componentKey) {
024            this.threadLocal = threadLocal;
025            this.componentKey = componentKey;
026        }
027    
028        @SuppressWarnings("unchecked")
029        public T get() {
030            return threadLocal.get().get(componentKey) ;
031        }
032    
033        @SuppressWarnings("unchecked")
034        public void set(T item) {
035            threadLocal.get().put(componentKey, item) ;
036    
037        }
038    }