29 |
22 Feb 05 |
nicklas |
1 |
/* |
192 |
17 Mar 05 |
jari |
$Id$ |
29 |
22 Feb 05 |
nicklas |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2005 Jari Häkkinen, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Martin Svensson |
29 |
22 Feb 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
29 |
22 Feb 05 |
nicklas |
9 |
|
29 |
22 Feb 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
29 |
22 Feb 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
29 |
22 Feb 05 |
nicklas |
of the License, or (at your option) any later version. |
29 |
22 Feb 05 |
nicklas |
14 |
|
29 |
22 Feb 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
29 |
22 Feb 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
29 |
22 Feb 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29 |
22 Feb 05 |
nicklas |
GNU General Public License for more details. |
29 |
22 Feb 05 |
nicklas |
19 |
|
29 |
22 Feb 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
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 |
This class is a similar to the JDK {@link ThreadLocal} class, |
29 |
22 Feb 05 |
nicklas |
but stores one object per class instead of one object per thread. |
29 |
22 Feb 05 |
nicklas |
<p> |
29 |
22 Feb 05 |
nicklas |
The main usage for this class is to allow a superclass |
29 |
22 Feb 05 |
nicklas |
to hold one object for each subclass that is extending it. |
29 |
22 Feb 05 |
nicklas |
36 |
|
29 |
22 Feb 05 |
nicklas |
@author Nicklas |
29 |
22 Feb 05 |
nicklas |
@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 |
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 |
Get the object associated with the class of |
29 |
22 Feb 05 |
nicklas |
the specified instance. |
29 |
22 Feb 05 |
nicklas |
@param instance An object instance of the class |
29 |
22 Feb 05 |
nicklas |
@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 |
Store an object and associate it with the class |
29 |
22 Feb 05 |
nicklas |
of the specified instance. |
29 |
22 Feb 05 |
nicklas |
@param instance An object instance of the class |
29 |
22 Feb 05 |
nicklas |
@param object The object to store |
29 |
22 Feb 05 |
nicklas |
@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 |
Subclasses may override this to initialize the object |
29 |
22 Feb 05 |
nicklas |
stored for that class. |
29 |
22 Feb 05 |
nicklas |
@param instance An object instance of the class |
29 |
22 Feb 05 |
nicklas |
@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 |
} |