author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 10:07:15 +0200 | |
changeset 228 | 984a01bbac90 |
child 233 | 15d31c98a129 |
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 |
private static Map<Class<?>,Object[]> instances = new LinkedHashMap<Class<?>,Object[]>(); |
jtulach@228 | 8 |
|
jtulach@228 | 9 |
private Registry() { |
jtulach@228 | 10 |
} |
jtulach@228 | 11 |
|
jtulach@228 | 12 |
public static void registerClass(Class<?> impl) { |
jtulach@228 | 13 |
instances.put(impl, new Object[1]); |
jtulach@228 | 14 |
} |
jtulach@228 | 15 |
|
jtulach@228 | 16 |
public static <T> T find(Class<T> whatType) { |
jtulach@228 | 17 |
for (Map.Entry<Class<?>, Object[]> entry : instances.entrySet()) { |
jtulach@228 | 18 |
if (whatType.isAssignableFrom(entry.getKey())) { |
jtulach@228 | 19 |
if (entry.getValue()[0] == null) { |
jtulach@228 | 20 |
try { |
jtulach@228 | 21 |
entry.getValue()[0] = entry.getKey().newInstance(); |
jtulach@228 | 22 |
} catch (Exception ex) { |
jtulach@228 | 23 |
throw new IllegalStateException(ex); |
jtulach@228 | 24 |
} |
jtulach@228 | 25 |
} |
jtulach@228 | 26 |
return whatType.cast(entry.getValue()[0]); |
jtulach@228 | 27 |
} |
jtulach@228 | 28 |
} |
jtulach@228 | 29 |
return null; |
jtulach@228 | 30 |
} |
jtulach@228 | 31 |
} |
jtulach@228 | 32 |