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.Iterator; |
jtulach@26 | 6 |
import java.util.Set; |
jtulach@26 | 7 |
|
jtulach@26 | 8 |
/** Factory to create various types of lookup instances. |
jtulach@26 | 9 |
* |
jtulach@26 | 10 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jtulach@26 | 11 |
* @version 2.0 |
jtulach@26 | 12 |
*/ |
jtulach@26 | 13 |
public final class Lookups { |
jtulach@26 | 14 |
private Lookups() { |
jtulach@26 | 15 |
} |
jtulach@26 | 16 |
|
jtulach@210 | 17 |
public static Lookup fixed(final Object... instances) { |
jtulach@210 | 18 |
return new Lookup() { |
jtulach@210 | 19 |
@Override |
jtulach@210 | 20 |
<T> Iterator<T> doLookup(Class<T> clazz) { |
jtulach@210 | 21 |
ArrayList<T> result = new ArrayList<T>(); |
jtulach@210 | 22 |
for (Object obj : instances) { |
jtulach@210 | 23 |
if (clazz.isInstance(obj)) { |
jtulach@210 | 24 |
result.add(clazz.cast(obj)); |
jtulach@210 | 25 |
} |
jtulach@210 | 26 |
} |
jtulach@210 | 27 |
return result.iterator(); |
jtulach@210 | 28 |
} |
jtulach@210 | 29 |
}; |
jtulach@26 | 30 |
} |
jtulach@26 | 31 |
|
jtulach@210 | 32 |
public static Lookup dynamic(final Dynamic provider) { |
jtulach@210 | 33 |
return new Lookup() { |
jtulach@210 | 34 |
@Override |
jtulach@210 | 35 |
<T> Iterator<T> doLookup(Class<T> clazz) { |
jtulach@210 | 36 |
ArrayList<T> result = new ArrayList<T>(); |
jtulach@210 | 37 |
provider.computeInstances(clazz, result); |
jtulach@210 | 38 |
return result.iterator(); |
jtulach@210 | 39 |
} |
jtulach@210 | 40 |
}; |
jtulach@26 | 41 |
} |
jtulach@26 | 42 |
|
jtulach@26 | 43 |
public interface Dynamic { |
jtulach@26 | 44 |
public <T> void computeInstances(Class<T> requestedType, Collection<? super T> addInstancesTo); |
jtulach@26 | 45 |
} |
jtulach@26 | 46 |
} |