Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics without collection

Tags:

java

generics

I have a method that usually takes an item from a list and has a signature of:

myMethod(T item)

I want to use this method but I know what I am sending the method.

SpecificItem myItem = new SpecificItem();

myMethod((T) myItem);

This doesn't sit well with me. Is this a sign of bad code?

like image 666
Lumpy Avatar asked Dec 12 '22 19:12

Lumpy


2 Answers

myMethod is defined in a generic class, somewhat like:

public class MyClass<T> {
   T myItem;

   public void myMethod(T item) {
      // do Something with item
   }

   public T myOtherMethod() {
      myMethod(myItem);   // casting is not necessary
      return myItem;
   }

}

If you instantiate this class, you exchange the variable type T with a real one:

MyClass<SpecificItem > concreteClass = new MyClass<SpecificItem >();

And if you call myMethod on this instance, you have to provide a SpecificItem, because SpecificItem is the generic type for this instance.

(I'm not sure it my post answers your question, please comment so I can improve it)

like image 192
Andreas Dolk Avatar answered Jan 02 '23 14:01

Andreas Dolk


It's better that you code to interface. For example :

In myMethod :

<T extends <? super Item>> void (T item);

This tells compiler to only accepts a generic type of T which is an implementation/extention of Item interface/class. This will make sure that given input is in correct type. Compiler guarantees it.

In main class :

Item myItem = new SpecificItem();

Code given above is the best practice. Get used to it. But (i discourage this) you can code like this too :

SpecificItem myItem = new SpecificItem();

You can read Java source code. For example in class java.util.Collections. In method sort(List) you may notice that Joshua Bloch makes sure that given input is always in correct format. To give it a try, do this :

public class Class1 {
    public static void main(String[] args) {
        List<Class1> list = new ArrayList<Class1>();

        Collections.sort(list);
    }
}

Above code will produce compilation error. To fix this compilation error Class1 must implement interface Comparable. This maintains the precondition of method sort(List) which assumes that given input is a List of Comparable.

Oh i almost forget about your question. Actually it's not a bad code since it works. I just want to tell you that there is a better way to do that.

like image 37
Hendra Jaya Avatar answered Jan 02 '23 12:01

Hendra Jaya