|
@@ -1,7 +1,6 @@ |
1 |
1 |
|
package com.temp.shared.utils; |
2 |
2 |
|
|
3 |
|
- |
import java.util.HashMap; |
4 |
|
- |
import java.util.Map; |
|
3 |
+ |
import java.util.*; |
5 |
4 |
|
|
6 |
5 |
|
public class Global { |
7 |
6 |
|
@SuppressWarnings("rawtypes") |
|
@@ -10,41 +9,38 @@ |
10 |
9 |
|
/** |
11 |
10 |
|
* Get the required instance associated with the klass. |
12 |
11 |
|
* |
13 |
|
- |
* @param klass |
14 |
|
- |
* !null |
15 |
|
- |
* |
16 |
|
- |
* @throws IllegalStateException |
17 |
|
- |
* if there is currently nothing associated with the klass |
|
12 |
+ |
* @param klass !null |
18 |
13 |
|
* |
19 |
14 |
|
* @return !null instance associated with the klass |
|
15 |
+ |
* |
|
16 |
+ |
* @throws IllegalStateException if there is currently nothing associated with the klass |
20 |
17 |
|
*/ |
21 |
|
- |
public static <T> T get(Class<T> klass) throws IllegalStateException { |
22 |
|
- |
T instance = getOptional(klass); |
23 |
|
- |
if (instance != null) { |
|
18 |
+ |
public static <T> T get( Class<T> klass ) |
|
19 |
+ |
throws IllegalStateException { |
|
20 |
+ |
T instance = getOptional( klass ); |
|
21 |
+ |
if ( instance != null ) { |
24 |
22 |
|
return instance; |
25 |
23 |
|
} |
26 |
|
- |
throw new IllegalStateException("No 'instance' associated with class: " + klass); |
|
24 |
+ |
throw new IllegalStateException( "No 'instance' associated with class: " + klass ); |
27 |
25 |
|
} |
28 |
26 |
|
|
29 |
27 |
|
/** |
30 |
28 |
|
* Set/Replace/Clear the current instance associated with a class |
31 |
29 |
|
* |
32 |
|
- |
* @param klass |
33 |
|
- |
* !null |
34 |
|
- |
* @param instance |
35 |
|
- |
* if null then removes current instance associated with the |
36 |
|
- |
* class, if !null then sets / replaces the current instance |
37 |
|
- |
* associated with the class. |
|
30 |
+ |
* @param klass !null |
|
31 |
+ |
* @param instance if null then removes current instance associated with the |
|
32 |
+ |
* class, if !null then sets / replaces the current instance |
|
33 |
+ |
* associated with the class. |
38 |
34 |
|
* |
39 |
35 |
|
* @return the previous instance associated with klass. |
40 |
36 |
|
*/ |
41 |
|
- |
public static <T> T set(Class<T> klass, T instance) { |
42 |
|
- |
Assert.notNull("Class", klass); |
43 |
|
- |
synchronized (INSTANCES_BY_CLASS) { |
44 |
|
- |
if (instance == null) { |
45 |
|
- |
return ObjectUtils.cast(INSTANCES_BY_CLASS.remove(klass)); |
|
37 |
+ |
public static <T> T set( Class<T> klass, T instance ) { |
|
38 |
+ |
Assert.notNull( "Class", klass ); |
|
39 |
+ |
synchronized ( INSTANCES_BY_CLASS ) { |
|
40 |
+ |
if ( instance == null ) { |
|
41 |
+ |
return ObjectUtils.cast( INSTANCES_BY_CLASS.remove( klass ) ); |
46 |
42 |
|
} else { |
47 |
|
- |
return ObjectUtils.cast(put(klass, instance)); |
|
43 |
+ |
return ObjectUtils.cast( put( klass, instance ) ); |
48 |
44 |
|
} |
49 |
45 |
|
} |
50 |
46 |
|
} |
|
@@ -52,20 +48,19 @@ |
52 |
48 |
|
/** |
53 |
49 |
|
* Get the optional instance associated with the klass. |
54 |
50 |
|
* |
55 |
|
- |
* @param klass |
56 |
|
- |
* !null |
|
51 |
+ |
* @param klass !null |
57 |
52 |
|
* |
58 |
53 |
|
* @return instance associated with the klass or null |
59 |
54 |
|
*/ |
60 |
|
- |
public static <T> T getOptional(Class<T> klass) { |
61 |
|
- |
Assert.notNull("Class", klass); |
62 |
|
- |
synchronized (INSTANCES_BY_CLASS) { |
63 |
|
- |
return ObjectUtils.cast(INSTANCES_BY_CLASS.get(klass)); |
|
55 |
+ |
public static <T> T getOptional( Class<T> klass ) { |
|
56 |
+ |
Assert.notNull( "Class", klass ); |
|
57 |
+ |
synchronized ( INSTANCES_BY_CLASS ) { |
|
58 |
+ |
return ObjectUtils.cast( INSTANCES_BY_CLASS.get( klass ) ); |
64 |
59 |
|
} |
65 |
60 |
|
} |
66 |
61 |
|
|
67 |
62 |
|
@SuppressWarnings("unchecked") |
68 |
|
- |
private static Object put(Object key, Object value) { |
69 |
|
- |
return INSTANCES_BY_CLASS.put(key, value); |
|
63 |
+ |
private static Object put( Object key, Object value ) { |
|
64 |
+ |
return INSTANCES_BY_CLASS.put( key, value ); |
70 |
65 |
|
} |
71 |
66 |
|
} |