DevFaqLookupGenerics

(Difference between revisions)
(How do I use Java Generics with Lookup?)
 
Line 4: Line 4:
As of NetBeans 6, a number of convenience methods have been added to lookup, and support for Java generics has been added to Lookup.  The following are differences (assume that <code>lkp</code> is a [[DevFaqLookup| <tt>Lookup</tt>]]):
As of NetBeans 6, a number of convenience methods have been added to lookup, and support for Java generics has been added to Lookup.  The following are differences (assume that <code>lkp</code> is a [[DevFaqLookup| <tt>Lookup</tt>]]):
-
{| border=1 class="simple"
+
{| border=1 class="simple" cellpadding="20" cellspacing="0" style="background-color:#FFF9EF;"
! NB 5.x Code  
! NB 5.x Code  
! NB 6 Code
! NB 6 Code
Line 21: Line 21:
Collection c = r.allInstances();</tt>
Collection c = r.allInstances();</tt>
| <tt>Collection<? extends X> c = lkp.lookupAll(X.class);</tt>
| <tt>Collection<? extends X> c = lkp.lookupAll(X.class);</tt>
 +
|-
 +
|<tt>Lookup.Template t = new Lookup.Template(X.class);<br/>
 +
Lookup.Result r = lkp.lookup(t);<br/>
 +
Collection c = r.allInstances(); <br/></tt>
 +
<tt>for (Iterator it=c.iterator(); it.hasNext();) {
 +
&nbsp;&nbsp;X x = (X) it.next();<br/>
 +
&nbsp;&nbsp;//do something with x<br/>
 +
}</tt>
 +
|<tt>for (X x : lkp.lookupAll(X.class)) {
 +
&nbsp;&nbsp;//do something with x<br/>
 +
}</tt>
|}
|}
-
 
+
<br/>
Generics work well with JDK-5-style for-loops:
Generics work well with JDK-5-style for-loops:

Current revision as of 00:29, 19 February 2010

How do I use Java Generics with Lookup?

As of NetBeans 6, a number of convenience methods have been added to lookup, and support for Java generics has been added to Lookup. The following are differences (assume that lkp is a Lookup):

NB 5.x Code NB 6 Code
DataObject dob = (DataObject) lkp.lookup(DataObject.class) DataObject dob = lkp.lookup(DataObject.class)
Lookup.Result r = lkp.lookup(new Lookup.Template(X.class)) Lookup.Result<? extends X> r = lkp.lookupResult(X.class)
Collection c = r.allInstances() Collection<? extends X> c = r.allInstances()
Lookup.Template t = new Lookup.Template(X.class);

Lookup.Result r = lkp.lookup(t);
Collection c = r.allInstances();

Collection<? extends X> c = lkp.lookupAll(X.class);
Lookup.Template t = new Lookup.Template(X.class);

Lookup.Result r = lkp.lookup(t);
Collection c = r.allInstances();
for (Iterator it=c.iterator(); it.hasNext();) {   X x = (X) it.next();
  //do something with x
}

for (X x : lkp.lookupAll(X.class)) {

  //do something with x
}


Generics work well with JDK-5-style for-loops:

for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
    // ...
}
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo