src/core/net/sf/basedb/util/ClassLocal.java

Code
Comments
Other
Rev Date Author Line
29 22 Feb 05 nicklas 1 /*
192 17 Mar 05 jari 2   $Id$
29 22 Feb 05 nicklas 3
4889 06 Apr 09 nicklas 4   Copyright (C) 2005 Jari Häkkinen, Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Martin Svensson
29 22 Feb 05 nicklas 6
2304 22 May 06 jari 7   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 8   Available at http://base.thep.lu.se/
29 22 Feb 05 nicklas 9
29 22 Feb 05 nicklas 10   BASE is free software; you can redistribute it and/or
29 22 Feb 05 nicklas 11   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
29 22 Feb 05 nicklas 13   of the License, or (at your option) any later version.
29 22 Feb 05 nicklas 14
29 22 Feb 05 nicklas 15   BASE is distributed in the hope that it will be useful,
29 22 Feb 05 nicklas 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
29 22 Feb 05 nicklas 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 22 Feb 05 nicklas 18   GNU General Public License for more details.
29 22 Feb 05 nicklas 19
29 22 Feb 05 nicklas 20   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 21   along with BASE. If not, see <http://www.gnu.org/licenses/>.
29 22 Feb 05 nicklas 22 */
29 22 Feb 05 nicklas 23 package net.sf.basedb.util;
29 22 Feb 05 nicklas 24
29 22 Feb 05 nicklas 25 import java.util.Map;
29 22 Feb 05 nicklas 26 import java.util.HashMap;
29 22 Feb 05 nicklas 27 import java.util.Collections;
29 22 Feb 05 nicklas 28
29 22 Feb 05 nicklas 29 /**
29 22 Feb 05 nicklas 30
29 22 Feb 05 nicklas 31   This class is a similar to the JDK {@link ThreadLocal} class,
29 22 Feb 05 nicklas 32   but stores one object per class instead of one object per thread.
29 22 Feb 05 nicklas 33   <p>
29 22 Feb 05 nicklas 34   The main usage for this class is to allow a superclass
29 22 Feb 05 nicklas 35   to hold one object for each subclass that is extending it.
29 22 Feb 05 nicklas 36
29 22 Feb 05 nicklas 37   @author Nicklas
29 22 Feb 05 nicklas 38   @version 2.0
29 22 Feb 05 nicklas 39 */
29 22 Feb 05 nicklas 40 public class ClassLocal<E>
29 22 Feb 05 nicklas 41 {
29 22 Feb 05 nicklas 42
6875 20 Apr 15 nicklas 43   private Map<Class<?>,E> objects;
29 22 Feb 05 nicklas 44
29 22 Feb 05 nicklas 45   /**
29 22 Feb 05 nicklas 46     Create a new <code>ClassLocal</code> object.
29 22 Feb 05 nicklas 47   */
29 22 Feb 05 nicklas 48   public ClassLocal()
29 22 Feb 05 nicklas 49   {
6875 20 Apr 15 nicklas 50     objects = Collections.synchronizedMap(new HashMap<Class<?>,E>());
29 22 Feb 05 nicklas 51   }
29 22 Feb 05 nicklas 52
29 22 Feb 05 nicklas 53   /**
29 22 Feb 05 nicklas 54     Get the object associated with the class of
29 22 Feb 05 nicklas 55     the specified instance.
29 22 Feb 05 nicklas 56     @param instance An object instance of the class
29 22 Feb 05 nicklas 57     @see ThreadLocal#get()
29 22 Feb 05 nicklas 58   */
29 22 Feb 05 nicklas 59   public E get(Object instance)
29 22 Feb 05 nicklas 60   {
6875 20 Apr 15 nicklas 61     Class<?> clazz = instance.getClass();
29 22 Feb 05 nicklas 62     E object = null;
29 22 Feb 05 nicklas 63     synchronized (objects)
29 22 Feb 05 nicklas 64     {
29 22 Feb 05 nicklas 65       object = objects.get(clazz);
29 22 Feb 05 nicklas 66       if ((object == null) && !objects.containsKey(clazz))
29 22 Feb 05 nicklas 67       {
29 22 Feb 05 nicklas 68         object = initialValue(instance);
29 22 Feb 05 nicklas 69         set(clazz, object);
29 22 Feb 05 nicklas 70       }
29 22 Feb 05 nicklas 71     }
29 22 Feb 05 nicklas 72     return object;
29 22 Feb 05 nicklas 73   }
29 22 Feb 05 nicklas 74   /**
29 22 Feb 05 nicklas 75     Store an object and associate it with the class
29 22 Feb 05 nicklas 76     of the specified instance.
29 22 Feb 05 nicklas 77     @param instance An object instance of the class
29 22 Feb 05 nicklas 78     @param object The object to store
29 22 Feb 05 nicklas 79     @see ThreadLocal#set(Object)
29 22 Feb 05 nicklas 80   */
29 22 Feb 05 nicklas 81   public void set(Object instance, E object)
29 22 Feb 05 nicklas 82   {
29 22 Feb 05 nicklas 83     objects.put(instance.getClass(), object);
29 22 Feb 05 nicklas 84   }
29 22 Feb 05 nicklas 85   /**
29 22 Feb 05 nicklas 86     Subclasses may override this to initialize the object
29 22 Feb 05 nicklas 87     stored for that class.
29 22 Feb 05 nicklas 88     @param instance An object instance of the class
29 22 Feb 05 nicklas 89     @return The object to associate with the class
29 22 Feb 05 nicklas 90   */
29 22 Feb 05 nicklas 91   protected E initialValue(Object instance)
29 22 Feb 05 nicklas 92   {
29 22 Feb 05 nicklas 93     return null;
29 22 Feb 05 nicklas 94   }
29 22 Feb 05 nicklas 95 }