-1

Question

I am trying to pass a double to a method that accepts T. That method (In this case. I plan to add the cases for String, int, etc.) requires the item passed to be a double or else it will throw an error.

Basically, I want to pass a double to commitEdit(T).

Background

I understand I am being very vague so I'll try to elaborate. I am attempting to create a class that extends TableCell<S,T> such that I can use it generically. I have no experience with <S,T> parameters. The method I am trying to call is commitEdit(T) which up until now I have been creating a new class that extends TableCell<CustomRowObject, Double> and everything has been working fine because commitEdit(Double) is easy enough to call.

The problem is that every time I want to make a new table, I have to make a new cell class and I would rather create a generic cell that handles the casting of the value from an Object to Double, String, Integer, etc, and passes it to commitEdit(T).

In all my years of programming i have always tried to avoid generic arguments but now I believe it is a good time to learn.

Code

Here's an example of the wrapper for commitEdit(T) that I've come up with so far(Doesnt work)

    public static class EditingCell<S, T> extends TableCell<S, T> {

        public EditingCell() {
        }

        public void commit(Object val) {
            if(val instanceof Double) {
                commitEdit((Double) val);
            } else if(val instanceof String) {
                commitEdit((String) val);
            }
        }
    }

The method commitEdit(T) in the type TableCell<S,T> is not applicable for the arguments (double)

Im Dumb

As in this specific case, all I had to do was cast to T as such commitEdit( (T) val).

Chris
  • 2,435
  • 6
  • 26
  • 49
  • What have you actually tried? It works for me… – Moritz Petersen Jun 06 '16 at 13:56
  • I just added the code as you replied, sorry – Chris Jun 06 '16 at 13:57
  • 1
    Sorry, when instanceof tells you that your object is a Double, why do you convert the Double to String, to then parse it to get a Double? Ever heard of casting? – GhostCat Jun 06 '16 at 14:04
  • Good point. However this method still doesn't accept a casted value. I think I am attempting to do something that I'd need more information on how `commitEdit()` actualyl works though. Sorry – Chris Jun 06 '16 at 14:08
  • If you pass a `double` to a method which expects an `Object` (or a `T`, which is erased to `Object`), it would be auto-boxed to `Double`. – Andy Turner Jun 06 '16 at 14:10
  • turns out I over-complicated it and all I had to do was cast the value to T.. – Chris Jun 06 '16 at 14:11
  • 1
    @Duck even that sounds over-complicated. Why not just `public void commit(T val)`? (Or have you omitted the `@Override`?) – Andy Turner Jun 06 '16 at 14:12
  • If I use `public void commit(T val)` I get errors that I cant pass a double to the function. However `public void commit(Object val)` seems to be working as intended. – Chris Jun 06 '16 at 14:14
  • 1
    Well, you shouldn't really need to cast if you use generics: that's kind of the point. Shouldn't you just define `commit` as `public void commit(T val) { ... }`? (Though it's not really clear what your `commit` method provides that is different to `commitEdit` anyway.) – James_D Jun 06 '16 at 14:19
  • 1
    the more that I get into this, the more I realize it provides absolutely nothing compared to `commitEdit` – Chris Jun 06 '16 at 14:22

2 Answers2

4

This works:

@Test
public void testDoubleGeneric()
{
    commitEdit(3.1514D);
}

private <T> void commitEdit(final T value)
{
    if (!(value instanceof Double))
    {
        throw new IllegalArgumentException();
    }
}

However I'd argue, that your approach to use generics is not the right approach for your problem. Whenever you have to use instanceof, generics are not the solution. Consider method overloading instead.

public void commit(Double d) ...

public void commit(String str) ...
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • This is a good answer however my specific solution didn't really pertain to the question I asked. – Chris Jun 06 '16 at 14:16
0
public class Example extends TableCell {

    public <T> void commit(T value) {
        if(arg instanceof Double) {
            // do whatever
        } else if(arg instanceof String) {
            // do something else
        }
    }
}
explv
  • 2,709
  • 10
  • 17