Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare type of an object's instance to a generic type?

how can i write this code in java?

public class ComponentsManager 
    {
        private List<IComponent> list = new ArrayList<IComponent>();

        public <U extends IComponent> U GetComponent() {

            for (IComponent component : list) {

                if(component instanceof U)
                {
                    return component;
                }
            }
        }
}

but i cant perform instanceof on generic types. how should i do it? thanks.

like image 847
Adibe7 Avatar asked May 17 '11 11:05

Adibe7


People also ask

How do you find the type of generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

How do I compare generic types in C#?

To enable two objects of a generic type parameter to be compared, they must implement the IComparable or IComparable<T>, and/or IEquatable<T> interfaces. Both versions of IComparable define the CompareTo() method and IEquatable<T> defines the Equals() method.

Which types can be used as arguments of a generic type?

The actual type arguments of a generic type are. reference types, wildcards, or. parameterized types (i.e. instantiations of other generic types).

How do you indicate that a class has a generic type parameter?

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.


2 Answers

Basically you can't do that due to type erasure. The normal workaround is to pass a Class object as a parameter; e.g.

    public <U extends IComponent> U GetComponent(Class<U> clazz) {
        for (IComponent component : list) {
            if (clazz.isInstance(component)) {
                return clazz.cast(component);
            }
        }
    }

You could also use if (clazz.equals(component.getClass())) { ... but that does an exact type match ... which is not what the instanceof operator does. The instanceof operator and the Class.instanceOf method both test to see if the value's type is assignment compatible.

like image 176
Stephen C Avatar answered Oct 12 '22 02:10

Stephen C


You could try adding a couple of 'test' methods:

private static <U extends IComponent> boolean testComponent(U u) {
  return true;
}

private static boolean testComponent(Object o) {
  return false;
}

Then, use testComponent(component) instead of component instanceof U.

Example code:

import java.util.*;

class IComponent {
}

class T1 extends IComponent {
}

public class Test {

  public static <U extends IComponent> boolean testComponent(U u) {
    return true;
  }

  public static boolean testComponent(Object o) {
    return false;
  }

  public static void main(String[] args) {
    T1 t = new T1();
    System.out.println("hm? " + (testComponent(t) ? "true" : "false"));
  }
}

Output:

hm? true

like image 20
sje397 Avatar answered Oct 12 '22 02:10

sje397