author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 10:04:53 +0200 | |
changeset 210 | acf2c31e22d4 |
parent 209 | 1c999569643b |
permissions | -rw-r--r-- |
jtulach@26 | 1 |
package api; |
jtulach@26 | 2 |
|
jtulach@210 | 3 |
import java.util.ArrayList; |
jtulach@26 | 4 |
import java.util.Collection; |
jtulach@210 | 5 |
import java.util.Collections; |
jtulach@210 | 6 |
import java.util.HashSet; |
jtulach@210 | 7 |
import java.util.Iterator; |
jtulach@210 | 8 |
import java.util.List; |
jtulach@26 | 9 |
import java.util.Set; |
jtulach@26 | 10 |
|
jtulach@26 | 11 |
/** Simplified version of NetBeans |
jtulach@26 | 12 |
* <a href="http://bits.netbeans.org/6.0/javadoc/org-openide-util/org/openide/util/Lookup.html">Lookup</a> |
jtulach@26 | 13 |
* reimplemented to separate the API for clients |
jtulach@26 | 14 |
* from the API for implementators while guaranteeing |
jtulach@26 | 15 |
* consistency among all there methods. |
jtulach@26 | 16 |
* |
jtulach@26 | 17 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jtulach@26 | 18 |
* @version 2.0 |
jtulach@26 | 19 |
*/ |
jtulach@26 | 20 |
// BEGIN: design.consistency.2.0 |
jtulach@210 | 21 |
public abstract class Lookup { |
jtulach@210 | 22 |
/** only for classes in the same package */ |
jtulach@26 | 23 |
Lookup() { |
jtulach@26 | 24 |
} |
jtulach@26 | 25 |
|
jtulach@26 | 26 |
// BEGIN: design.consistency.lookup.2.0 |
jtulach@26 | 27 |
public <T> T lookup(Class<T> clazz) { |
jtulach@210 | 28 |
Iterator<T> it = doLookup(clazz); |
jtulach@210 | 29 |
return it.hasNext() ? it.next() : null; |
jtulach@26 | 30 |
} |
jtulach@26 | 31 |
// END: design.consistency.lookup.2.0 |
jtulach@26 | 32 |
|
jtulach@26 | 33 |
// BEGIN: design.consistency.lookupAll.2.0 |
jtulach@26 | 34 |
public <T> Collection<? extends T> lookupAll(Class<T> clazz) { |
jtulach@210 | 35 |
Iterator<T> it = doLookup(clazz); |
jtulach@210 | 36 |
if (!it.hasNext()) { |
jtulach@210 | 37 |
return Collections.emptyList(); |
jtulach@210 | 38 |
} else { |
jtulach@210 | 39 |
List<T> result = new ArrayList<T>(); |
jtulach@210 | 40 |
while (it.hasNext()) { |
jtulach@210 | 41 |
result.add(it.next()); |
jtulach@210 | 42 |
} |
jtulach@210 | 43 |
return result; |
jtulach@210 | 44 |
} |
jtulach@26 | 45 |
} |
jtulach@26 | 46 |
// END: design.consistency.lookupAll.2.0 |
jtulach@26 | 47 |
|
jtulach@26 | 48 |
// BEGIN: design.consistency.lookupAllClasses.2.0 |
jtulach@26 | 49 |
public <T> Set<Class<? extends T>> lookupAllClasses(Class<T> clazz) { |
jtulach@210 | 50 |
Iterator<T> it = doLookup(clazz); |
jtulach@210 | 51 |
if (!it.hasNext()) { |
jtulach@210 | 52 |
return Collections.emptySet(); |
jtulach@210 | 53 |
} else { |
jtulach@210 | 54 |
Set<Class<? extends T>> result = |
jtulach@210 | 55 |
new HashSet<Class<? extends T>>(); |
jtulach@210 | 56 |
while (it.hasNext()) { |
jtulach@210 | 57 |
result.add(it.next().getClass().asSubclass(clazz)); |
jtulach@210 | 58 |
} |
jtulach@210 | 59 |
return result; |
jtulach@210 | 60 |
} |
jtulach@26 | 61 |
} |
jtulach@26 | 62 |
// END: design.consistency.lookupAllClasses.2.0 |
jtulach@210 | 63 |
// FINISH: design.consistency.2.0 |
jtulach@210 | 64 |
|
jtulach@210 | 65 |
abstract <T> Iterator<T> doLookup(Class<T> clazz); |
jtulach@26 | 66 |
} |