|
@@ -5,10 +5,8 @@ |
5 |
5 |
|
|
6 |
6 |
|
import java.util.*; |
7 |
7 |
|
|
8 |
|
- |
public class ClassToInstanceMapperImpl implements ClassToInstanceMapper |
9 |
|
- |
{ |
10 |
|
- |
public ClassToInstanceMapperImpl() |
11 |
|
- |
{ |
|
8 |
+ |
public class ClassToInstanceMapperImpl implements ClassToInstanceMapper { |
|
9 |
+ |
public ClassToInstanceMapperImpl() { |
12 |
10 |
|
} |
13 |
11 |
|
|
14 |
12 |
|
private Map<Class<?>, NoInstanceFactory<?>> mFactoriesByClass = new HashMap<Class<?>, NoInstanceFactory<?>>(); |
|
@@ -17,8 +15,7 @@ |
17 |
15 |
|
/** |
18 |
16 |
|
* Get a List (!null) of all the currently "set" Factories |
19 |
17 |
|
*/ |
20 |
|
- |
public synchronized List<NoInstanceFactory<?>> getNoInstanceFactories() |
21 |
|
- |
{ |
|
18 |
+ |
public synchronized List<NoInstanceFactory<?>> getNoInstanceFactories() { |
22 |
19 |
|
return new ArrayList<NoInstanceFactory<?>>( mFactoriesByClass.values() ); |
23 |
20 |
|
} |
24 |
21 |
|
|
|
@@ -30,16 +27,13 @@ |
30 |
27 |
|
* @throws IllegalArgumentException if pFactory is null |
31 |
28 |
|
* @throws IllegalStateException if this is a duplicate Factory |
32 |
29 |
|
*/ |
33 |
|
- |
public void setNoInstanceFactory( NoInstanceFactory<?> pFactory ) |
34 |
|
- |
{ |
|
30 |
+ |
public void setNoInstanceFactory( NoInstanceFactory<?> pFactory ) { |
35 |
31 |
|
Objects.assertNotNull( "NoInstanceFactory", pFactory ); |
36 |
32 |
|
Class<?> zClass = pFactory.getFor(); |
37 |
33 |
|
assertClass( zClass ); |
38 |
|
- |
synchronized ( this ) |
39 |
|
- |
{ |
|
34 |
+ |
synchronized ( this ) { |
40 |
35 |
|
NoInstanceFactory<?> zFactory = mFactoriesByClass.get( zClass ); |
41 |
|
- |
if ( zFactory == null ) |
42 |
|
- |
{ |
|
36 |
+ |
if ( zFactory == null ) { |
43 |
37 |
|
mFactoriesByClass.put( zClass, pFactory ); |
44 |
38 |
|
return; |
45 |
39 |
|
} |
|
@@ -55,11 +49,9 @@ |
55 |
49 |
|
* @throws IllegalArgumentException if pClass is null |
56 |
50 |
|
*/ |
57 |
51 |
|
public <T> T checkGet( Class<T> pClass ) |
58 |
|
- |
throws IllegalArgumentException |
59 |
|
- |
{ |
|
52 |
+ |
throws IllegalArgumentException { |
60 |
53 |
|
assertClass( pClass ); |
61 |
|
- |
synchronized ( this ) |
62 |
|
- |
{ |
|
54 |
+ |
synchronized ( this ) { |
63 |
55 |
|
//noinspection unchecked |
64 |
56 |
|
return (T) mInstanceByClass.get( pClass ); |
65 |
57 |
|
} |
|
@@ -77,26 +69,20 @@ |
77 |
69 |
|
* @throws IllegalStateException if no instance previously 'registered' for pClass |
78 |
70 |
|
*/ |
79 |
71 |
|
public <T> T get( Class<T> pClass ) |
80 |
|
- |
throws IllegalArgumentException, IllegalStateException |
81 |
|
- |
{ |
|
72 |
+ |
throws IllegalArgumentException, IllegalStateException { |
82 |
73 |
|
assertClass( pClass ); |
83 |
74 |
|
Object zInstance; |
84 |
|
- |
synchronized ( this ) |
85 |
|
- |
{ |
86 |
|
- |
if ( null == (zInstance = mInstanceByClass.get( pClass )) ) |
87 |
|
- |
{ |
|
75 |
+ |
synchronized ( this ) { |
|
76 |
+ |
if ( null == (zInstance = mInstanceByClass.get( pClass )) ) { |
88 |
77 |
|
NoInstanceFactory<?> zFactory = mFactoriesByClass.get( pClass ); |
89 |
|
- |
if ( zFactory != null ) |
90 |
|
- |
{ |
91 |
|
- |
if ( null != (zInstance = zFactory.createInstance()) ) |
92 |
|
- |
{ |
|
78 |
+ |
if ( zFactory != null ) { |
|
79 |
+ |
if ( null != (zInstance = zFactory.createInstance()) ) { |
93 |
80 |
|
mInstanceByClass.put( pClass, zInstance ); |
94 |
81 |
|
} |
95 |
82 |
|
} |
96 |
83 |
|
} |
97 |
84 |
|
} |
98 |
|
- |
if ( zInstance == null ) |
99 |
|
- |
{ |
|
85 |
+ |
if ( zInstance == null ) { |
100 |
86 |
|
throw new IllegalStateException( "No instance found for: " + pClass ); |
101 |
87 |
|
} |
102 |
88 |
|
//noinspection unchecked |
|
@@ -113,14 +99,11 @@ |
113 |
99 |
|
* @throws IllegalStateException if an instance for pClass IS currently 'registered' |
114 |
100 |
|
*/ |
115 |
101 |
|
public <T, V extends T> void set( Class<T> pClass, V pInstance ) |
116 |
|
- |
throws IllegalArgumentException, IllegalStateException |
117 |
|
- |
{ |
|
102 |
+ |
throws IllegalArgumentException, IllegalStateException { |
118 |
103 |
|
assertClassAndInstance( pClass, pInstance ); |
119 |
|
- |
synchronized ( this ) |
120 |
|
- |
{ |
|
104 |
+ |
synchronized ( this ) { |
121 |
105 |
|
Object zInstance = mInstanceByClass.get( pClass ); |
122 |
|
- |
if ( zInstance == null ) |
123 |
|
- |
{ |
|
106 |
+ |
if ( zInstance == null ) { |
124 |
107 |
|
mInstanceByClass.put( pClass, pInstance ); |
125 |
108 |
|
return; |
126 |
109 |
|
} |
|
@@ -138,14 +121,11 @@ |
138 |
121 |
|
* @throws IllegalStateException if an instance for pClass is NOT currently 'registered' |
139 |
122 |
|
*/ |
140 |
123 |
|
public <T, V extends T> void replace( Class<T> pClass, V pInstance ) |
141 |
|
- |
throws IllegalArgumentException, IllegalStateException |
142 |
|
- |
{ |
|
124 |
+ |
throws IllegalArgumentException, IllegalStateException { |
143 |
125 |
|
assertClassAndInstance( pClass, pInstance ); |
144 |
|
- |
synchronized ( this ) |
145 |
|
- |
{ |
|
126 |
+ |
synchronized ( this ) { |
146 |
127 |
|
Object zInstance = mInstanceByClass.get( pClass ); |
147 |
|
- |
if ( zInstance != null ) |
148 |
|
- |
{ |
|
128 |
+ |
if ( zInstance != null ) { |
149 |
129 |
|
mInstanceByClass.put( pClass, pInstance ); |
150 |
130 |
|
return; |
151 |
131 |
|
} |
|
@@ -163,38 +143,31 @@ |
163 |
143 |
|
* @throws IllegalStateException if pInstance of pClass is NOT currently 'registered' for pClass |
164 |
144 |
|
*/ |
165 |
145 |
|
public <T, V extends T> void remove( Class<T> pClass, V pInstance ) |
166 |
|
- |
throws IllegalArgumentException, IllegalStateException |
167 |
|
- |
{ |
|
146 |
+ |
throws IllegalArgumentException, IllegalStateException { |
168 |
147 |
|
assertClassAndInstance( pClass, pInstance ); |
169 |
148 |
|
Object zInstance; |
170 |
|
- |
synchronized ( this ) |
171 |
|
- |
{ |
|
149 |
+ |
synchronized ( this ) { |
172 |
150 |
|
zInstance = mInstanceByClass.get( pClass ); |
173 |
|
- |
if ( pInstance == zInstance ) |
174 |
|
- |
{ |
|
151 |
+ |
if ( pInstance == zInstance ) { |
175 |
152 |
|
mInstanceByClass.remove( pClass ); |
176 |
153 |
|
return; |
177 |
154 |
|
} |
178 |
155 |
|
} |
179 |
|
- |
if ( zInstance == null ) |
180 |
|
- |
{ |
|
156 |
+ |
if ( zInstance == null ) { |
181 |
157 |
|
throw new IllegalStateException( "No instance currently exists for: " + pClass ); |
182 |
158 |
|
} |
183 |
159 |
|
throw new IllegalStateException( "Instance to remove '" + pInstance + "' mismatchs found '" + zInstance + "' for: " + pClass ); |
184 |
160 |
|
} |
185 |
161 |
|
|
186 |
|
- |
public synchronized void clearAllInstances() |
187 |
|
- |
{ |
|
162 |
+ |
public synchronized void clearAllInstances() { |
188 |
163 |
|
mInstanceByClass.clear(); |
189 |
164 |
|
} |
190 |
165 |
|
|
191 |
|
- |
private void assertClass( Class<?> pClass ) |
192 |
|
- |
{ |
|
166 |
+ |
private void assertClass( Class<?> pClass ) { |
193 |
167 |
|
Objects.assertNotNull( "Class (key)", pClass ); |
194 |
168 |
|
} |
195 |
169 |
|
|
196 |
|
- |
private void assertClassAndInstance( Class<?> pClass, Object pInstance ) |
197 |
|
- |
{ |
|
170 |
+ |
private void assertClassAndInstance( Class<?> pClass, Object pInstance ) { |
198 |
171 |
|
assertClass( pClass ); |
199 |
172 |
|
Objects.assertNotNull( "pInstance (value)", pInstance ); |
200 |
173 |
|
} |