author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 10:07:30 +0200 | |
changeset 233 | 15d31c98a129 |
parent 228 | 984a01bbac90 |
permissions | -rw-r--r-- |
jtulach@228 | 1 |
package org.apidesign.anagram.app.registergeneral; |
jtulach@228 | 2 |
|
jtulach@228 | 3 |
import java.util.LinkedHashMap; |
jtulach@228 | 4 |
import java.util.Map; |
jtulach@228 | 5 |
|
jtulach@228 | 6 |
public final class Registry { |
jtulach@228 | 7 |
|
jtulach@228 | 8 |
private Registry() { |
jtulach@228 | 9 |
} |
jtulach@233 | 10 |
|
jtulach@233 | 11 |
// BEGIN: anagram.registry |
jtulach@233 | 12 |
private static Map<Class<?>,Object[]> instances = |
jtulach@233 | 13 |
new LinkedHashMap<Class<?>,Object[]>(); |
jtulach@228 | 14 |
public static void registerClass(Class<?> impl) { |
jtulach@228 | 15 |
instances.put(impl, new Object[1]); |
jtulach@228 | 16 |
} |
jtulach@228 | 17 |
|
jtulach@228 | 18 |
public static <T> T find(Class<T> whatType) { |
jtulach@228 | 19 |
for (Map.Entry<Class<?>, Object[]> entry : instances.entrySet()) { |
jtulach@228 | 20 |
if (whatType.isAssignableFrom(entry.getKey())) { |
jtulach@228 | 21 |
if (entry.getValue()[0] == null) { |
jtulach@228 | 22 |
try { |
jtulach@228 | 23 |
entry.getValue()[0] = entry.getKey().newInstance(); |
jtulach@228 | 24 |
} catch (Exception ex) { |
jtulach@228 | 25 |
throw new IllegalStateException(ex); |
jtulach@228 | 26 |
} |
jtulach@228 | 27 |
} |
jtulach@228 | 28 |
return whatType.cast(entry.getValue()[0]); |
jtulach@228 | 29 |
} |
jtulach@228 | 30 |
} |
jtulach@228 | 31 |
return null; |
jtulach@228 | 32 |
} |
jtulach@233 | 33 |
// END: anagram.registry |
jtulach@228 | 34 |
} |
jtulach@228 | 35 |