rakamakafo
rakamakafo

Reputation: 1154

Making normal generic class

I have many controller classes. And in each such class I'm using folliwng code many times:

tc_mt_buy_amount.setCellFactory(param -> {
            return new TableCell<FaClConversionDeals, BigDecimal>(){
                @Override
                protected void updateItem(BigDecimal item, boolean empty) {
                    super.updateItem(item, empty);
                    if(empty || item == null){
                        setText("");
                    } else {
                        setText(df.format(item));
                    }
                }
            };
        });

tc_mt_sell_amount.setCellFactory(param -> {
            return new TableCell<FaClConversionDeals, BigDecimal>(){
                @Override
                protected void updateItem(BigDecimal item, boolean empty) {
                    super.updateItem(item, empty);
                    if(empty || item == null){
                        setText("");
                    } else {
                        setText(df.format(item));
                    }
                }
            };
        });

In order to not dublicate code so many times in each class, I've created an inner classes in each controller class:

tc_mt_buy_amount.setCellFactory(param -> {
                return new FormatMainColumn();
            });
tc_mt_sell_amount.setCellFactory(param -> {
            return new FormatMainColumn();
        });

private class FormatMainColumn extends TableCell<FaClConversionDeals, BigDecimal> {

        DecimalFormat df = new DecimalFormat("#,###.00");
        @Override
    protected void updateItem(BigDecimal item, boolean empty) {
        super.updateItem(item, empty);
        if(empty || item == null){
            setText("");
        } else {
            setText(df.format(item));
        }
    }
}

Now, in order to not to write inner classes in each controller, I want to create standalone generic class to where I can refer from each controller. The problem is that, for instance, FaClConversionDeals in above example is different in each controller (i.e., it may be other classes). In structural way, it should look similar to this:

From controller:

tc_mt_buy_amount.setCellFactory(param -> {
                    return new FormatMainColumn(ClassName);
                });

Generic Class:

 private class FormatMainColumn(ClassName class) extends TableCell<ClassName, BigDecimal> {

                DecimalFormat

 df = new DecimalFormat("#,###.00");
            @Override
        protected void updateItem(BigDecimal item, boolean empty) {
            super.updateItem(item, empty);
            if(empty || item == null){
                setText("");
            } else {
                setText(df.format(item));
            }
        }
    }

Upvotes: 1

Views: 48

Answers (1)

Kayaman
Kayaman

Reputation: 73558

That's not how the generics syntax works. If I understood you correctly, you want your class declared as

private class FormatMainColumn<T> extends TableCell<T, BigDecimal> {

and then you can do return new FormatMainColumn<ClassName>();.

Upvotes: 1

Related Questions