Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (732)
Games in Android Showcase (221)
games submitted by our members
Games in WIP (806)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Returning specific type of Object that extends Object?  (Read 1116 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2017-04-29 15:50:51 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   public Component findComponentOfType(Class<? extends Component> componentType){
     
      Component thisComponent = null;

      for(int i = 0; i < components.size(); i++){
         if(components.get(i).getClass().equals(componentType)){
            thisComponent = components.get(i);
         }
      }
     

      return (componentType.cast(thisComponent));
}


I'm trying to convert thisComponent to the same type of componentType and return it to use as componentType in another method. They both extend the same class which is Component.
I'm assuming it has something to do with me not knowing how to make my method as generic as possible. Any ideas?

1  
public Component findComponentOfType

"f**k it, maybe it'll work." -Me
Offline Riven
Administrator

« JGO Overlord »


Medals: 1289
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2017-04-29 17:07:29 »

This ought to work:

1  
public <T extends Component> T findComponentOfType(Class<T> componentType){

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline MrPork
« Reply #2 - Posted 2017-04-29 17:26:02 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
@SuppressWarnings("unchecked")
   public <T extends Component> T findComponentOfType(Class<T> componentType){
     
      Component thisComponent = null;

      for(int i = 0; i < components.size(); i++){
         if(components.get(i).getClass() == componentType){
            thisComponent = components.get(i);
         }
      }
     

      return (T) thisComponent;
     
}


Thanks that lets me use it in the other method! Problem now is that my code is only giving me null objects. Not because of your solution of course.  Emo

"f**k it, maybe it'll work." -Me
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #3 - Posted 2017-04-29 17:31:58 »

I'm an idiot, my problem is elsewhere. Thank you!

"f**k it, maybe it'll work." -Me
Offline CoDi^R
« Reply #4 - Posted 2017-04-29 23:44:12 »

1  
2  
3  
4  
5  
6  
7  
@SuppressWarnings("unchecked")
for(int i = 0; i < components.size(); i++){
   if(components.get(i).getClass() == componentType){
      thisComponent = components.get(i);
      break; /* <--- this here <--- */
   }
}


You probably want to stop iterating the loop after you find a matching component.

Robotality - steamworks4j - @code_disaster - codi^r @ #java-gaming
Offline MrPork
« Reply #5 - Posted 2017-05-01 22:35:07 »

Cool! I didn't know you could do that!   Grin

"f**k it, maybe it'll work." -Me
Offline SHC
« Reply #6 - Posted 2017-05-01 23:33:14 »

You can even return mid loop.

1  
2  
3  
4  
5  
6  
7  
8  
9  
@SuppressWarnings("unchecked")
public <T extends Component> T getComponent(Class<T> klass)
{
    for (Component c : components)
        if (klass.isInstance(c))
            return (T) c; // Is Class#cast better here?

    return null;
}

Offline Riven
Administrator

« JGO Overlord »


Medals: 1289
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2017-05-02 06:18:12 »

1  
2  
        if (klass.isInstance(c))
}

I'd opt for:
1  
klass.isAssignableFrom(c)
which is the conceptually equal to
c instanceof klass
.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline KaiHH

JGO Kernel


Medals: 424



« Reply #8 - Posted 2017-05-02 06:20:39 »

You surely mean:
klass.isAssignableFrom(c.getClass())
Offline Riven
Administrator

« JGO Overlord »


Medals: 1289
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2017-05-02 07:06:04 »

Ofcourse persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Archive (402 views)
2017-04-27 17:45:51

buddyBro (586 views)
2017-04-05 03:38:00

CopyableCougar4 (1044 views)
2017-03-24 15:39:42

theagentd (1073 views)
2017-03-24 15:32:08

Rule (1049 views)
2017-03-19 12:43:22

Rule (1033 views)
2017-03-19 12:42:17

Rule (1025 views)
2017-03-19 12:36:21

theagentd (1164 views)
2017-03-16 05:07:07

theagentd (1107 views)
2017-03-15 22:37:06

theagentd (833 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!