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 /** @author Paul Hammant */
016 public class ThreadLocalMapObjectReference<T> implements ObjectReference<T> {
017 private final ThreadLocal<Map> threadLocal;
018 private final Object componentKey;
019
020 public ThreadLocalMapObjectReference(ThreadLocal<Map> threadLocal, Object componentKey) {
021 this.threadLocal = threadLocal;
022 this.componentKey = componentKey;
023 }
024
025 @SuppressWarnings("unchecked")
026 public T get() {
027 return (T) ((Map)threadLocal.get()).get(componentKey) ;
028 }
029
030 @SuppressWarnings("unchecked")
031 public void set(T item) {
032 ((Map)threadLocal.get()).put(componentKey, item) ;
033
034 }
035 }