Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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 3969 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: 1371
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: 1371
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: 796



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

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

« JGO Overlord »


Medals: 1371
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  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!