Why does IntegerProperty implement the <Number> property rather than the <Integer> property?

The Property interface added by JavaFX has a parameter of type T , which is the type of value wrapped by this property.

Among the implementations of the Property interface, there are some for numbers: IntegerProperty , FloatProperty , etc. All of these classes implement Property<Number> .

Take, for example, IntegerProperty . What is the reason it implements Property<Number> rather than Property<Integer> , as I expected?


Here is a UML diagram that clarifies the IntegerProperty hierarchy:

enter image description here

+5
source share
2 answers

As mentioned in the comment section of the Java bug report ( DoubleProperty has an unexpected generic type ),

This design is intended. It saves the number of required methods much less.


In this answer to James_D's comments , I found out about a later bug report that raised this issue, ChangeListener cannot be added to SimpleIntegerProperty ). A comment

We decided not to change the general properties of the properties of primitive types (from a number to a certain type) due to backward compatibility problems. However, this means that this problem cannot be fixed.

suggests the team considered a design change, but it was too late.

+5
source

I would say that they use Number , so you can use these classes with AtomicInteger and BigInteger etc.

As far as I can tell, the only "real" difference between DoubleProperty and IntegerProperty is the setValue(Number v) method - one uses v.doubleValue() , the other v.intValue() .

I don't have a javafx working environment, so I cannot test if using IntegerProperty with Double will throw an exception.

0
source

Source: https://habr.com/ru/post/1240157/


All Articles