[committed] changing return-type of descendantMatches

Before(No cast possible, result-type List<Spatial> required):


      List<Spatial>firetubes =descendantMatches(FireTubeNode.class);



After: (As we know there is only the specified Class-Instances inside of the list we should be allowed to do the cast)


      List<FireTubeNode>firetubes =(List<FireTubeNode>)descendantMatches(FireTubeNode.class);





Index: src/com/jme/scene/Node.java
===================================================================
--- src/com/jme/scene/Node.java   (revision 4727)
+++ src/com/jme/scene/Node.java   (working copy)
@@ -760,7 +760,7 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<Spatial> descendantMatches(
+    public List<? extends Spatial> descendantMatches(
             Class<? extends Spatial> spatialSubclass) {
         return descendantMatches(spatialSubclass, null);
     }

Also changed the return value of the other two descendantMatches classes


Index: src/com/jme/scene/Node.java
===================================================================
--- src/com/jme/scene/Node.java   (revision 4727)
+++ src/com/jme/scene/Node.java   (working copy)
@@ -737,12 +737,12 @@
      *                        Null causes all Spatials to qualify.
      * @param nameRegex  Regular expression to match Spatial name against.
      *                        Null causes all Names to qualify.
-     * @return Non-null, but possibly 0-element, list of matching Spatials.
+     * @return Non-null, but possibly 0-element, list of matching Spatials (also Instances extending Spatials).
      *
      * @see java.util.regex.Pattern
      * @see Spatial#matches(Class<? extends Spatial>, String)
      */
-    public List<Spatial> descendantMatches(
+    public List<? extends Spatial> descendantMatches(
             Class<? extends Spatial> spatialSubclass, String nameRegex) {
         List<Spatial> newList = new ArrayList<Spatial>();
         if (getQuantity() < 1) return newList;
@@ -760,7 +760,7 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<Spatial> descendantMatches(
+    public List<? extends Spatial> descendantMatches(
             Class<? extends Spatial> spatialSubclass) {
         return descendantMatches(spatialSubclass, null);
     }
@@ -770,7 +770,7 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<Spatial> descendantMatches(String nameRegex) {
+    public List<? extends Spatial> descendantMatches(String nameRegex) {
         return descendantMatches(null, nameRegex);
     }
 }

Excellent improvement, ToM.



This hasn't been committed.  Do you need somebody to commit this?

can you(blaine) commit this for now! I first would have to checkout from the https with svn-account but no time at the moment!



thx

ttrocha said:

Before(No cast possible, result-type List<Spatial> required):


      List<Spatial>firetubes =descendantMatches(FireTubeNode.class);



After: (As we know there is only the specified Class-Instances inside of the list we should be allowed to do the cast)


      List<FireTubeNode>firetubes =(List<FireTubeNode>)descendantMatches(FireTubeNode.class);





Index: src/com/jme/scene/Node.java
===================================================================
--- src/com/jme/scene/Node.java   (revision 4727)
+++ src/com/jme/scene/Node.java   (working copy)
@@ -760,7 +760,7 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<Spatial> descendantMatches(
+    public List<? extends Spatial> descendantMatches(
             Class<? extends Spatial> spatialSubclass) {
         return descendantMatches(spatialSubclass, null);
     }





Good idea, now the arryayList can hold Geometry& Node objects =]

Committed w/ rev. 4737.  Unit tests run clean (except for unrelated Java 5 problems).

Combining Tom's enhancement and example with Falken's improvement, the loop idiom becomes:


      for (FireTubeNode ftNode : descendantMatches(FireTubeNode.class)) {
                ...

ToM's new patch committed w/ rev. 4742 after regression testing.



I had to add a SuppressWarnings to


public <T extends Spatial>List<T> descendantMatches(
            Class<T> spatialSubclass, String nameRegex)



since the Node.getChildren() used within is not generified, so the compiler wants the (T) child cast confirmed.  Nice to do that at one place where we know it's safe, instead of leaving it up to the users.

@Falken: Yeah, this is great! Actually I wanted to do it like this but didn't know how. Your way is much more elegant and no Casting is needed anymore. Many thanx. Here the patch:


Index: src/com/jme/scene/Node.java
===================================================================
--- src/com/jme/scene/Node.java   (revision 4740)
+++ src/com/jme/scene/Node.java   (working copy)
@@ -742,12 +742,12 @@
      * @see java.util.regex.Pattern
      * @see Spatial#matches(Class<? extends Spatial>, String)
      */
-    public List<? extends Spatial> descendantMatches(
-            Class<? extends Spatial> spatialSubclass, String nameRegex) {
-        List<Spatial> newList = new ArrayList<Spatial>();
+    public <T extends Spatial>List<T> descendantMatches(
+            Class<T> spatialSubclass, String nameRegex) {
+        List<T> newList = new ArrayList<T>();
         if (getQuantity() < 1) return newList;
         for (Spatial child : getChildren()) {
-            if (child.matches(spatialSubclass, nameRegex)) newList.add(child);
+            if (child.matches(spatialSubclass, nameRegex)) newList.add((T)child);
             if (child instanceof Node)
                 newList.addAll(((Node) child).descendantMatches(
                         spatialSubclass, nameRegex));
@@ -760,8 +760,8 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<? extends Spatial> descendantMatches(
-            Class<? extends Spatial> spatialSubclass) {
+    public <T extends Spatial>List<T> descendantMatches(
+            Class<T> spatialSubclass) {
         return descendantMatches(spatialSubclass, null);
     }
 
@@ -770,7 +770,7 @@
      *
      * @see #descendantMatches(Class<? extends Spatial>, String)
      */
-    public List<? extends Spatial> descendantMatches(String nameRegex) {
+    public <T extends Spatial>List<T> descendantMatches(String nameRegex) {
         return descendantMatches(null, nameRegex);
     }
 }

Arg.  I missed falken's post.  I'll apply ToM's new patch.



See update to my previous post in this thread (corrected after knowing about Falken's improvement).

chuckle



Had another big post ready to go, but it's a moot point now.



Glad I could help.



-Falken