DevFaqLookupGenerics

(Difference between revisions)
(How do I use Java Generics with Lookup?)
 
(One intermediate revision not shown)
Line 2: Line 2:
===How do I use Java Generics with Lookup?===
===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:
+
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
 +
|-
 +
| <tt>DataObject dob = (DataObject) lkp.lookup(DataObject.class)</tt>
 +
| <tt>DataObject dob = lkp.lookup(DataObject.class)</tt>
|-  
|-  
-
| <pre>Lookup.Result r = lkp.lookup(
+
| <tt>Lookup.Result r = lkp.lookup(new Lookup.Template(X.class))</tt>
 +
| <tt>Lookup.Result<? extends X> r = lkp.lookupResult(X.class)</tt>
 +
|-
 +
|<tt>Collection c = r.allInstances()</tt>
 +
|<tt>Collection<? extends X> c = r.allInstances()</tt>
 +
|-
 +
| <tt>Lookup.Template t = new Lookup.Template(X.class);
 +
Lookup.Result r = lkp.lookup(t);<br/>
 +
Collection c = r.allInstances();</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/>
-
  new Lookup.Template(X.class))</pre>|<pre>Lookup.Result<? extends X> r =
+
Generics work well with JDK-5-style for-loops:
-
  lkp.lookupResult(X.class)</pre>
+
-
{| border=1 class="simple"
+
-
!<pre>Collection c = r.allInstances()</pre>
+
-
!<tt>{Collection<? extends X> c =
+
-
|}
+
-
 
+
-
  r.allInstances()</pre>
+
-
|<pre>Lookup.Template t = new Lookup.Template(X.class);
+
-
Lookup.Result r = lkp.lookup(t);
+
-
Collection c = r.allInstances();</pre>|<pre>Collection<? extends X> c =
+
-
  lkp.lookupAll(X.class);</pre>
+
-
 
+
-
The new style also works well with for-loops and avoids casts:
+
<pre>
<pre>
for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
for (SomeService s : Lookup.getDefault().lookupAll(SomeService.class)) {
-
    // ...
 
-
}
 
-
// ...
 
-
SomeSingleton s = Lookup.getDefault().lookup(SomeSingleton.class);
 
-
if (s != null) {
 
     // ...
     // ...
}
}
</pre>
</pre>

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