averbraeck / djunits

Delft Java UNIT System for using strongly-typed quantities and units
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Simplify class structure #2

Closed averbraeck closed 1 year ago

averbraeck commented 1 year ago

The current class structure for types of Scalar, Vector and Matrix is tremendously complex, especially given that the code for these classes is generated. To show the complexity for, e.g., a Length:

In other words, 14 classes and interfaces define the contract and implementation of the Length scalar. And I left out the tremendously complex generics like:

interface RelWithAbs<AU extends AbsoluteLinearUnit<AU, RU>, A extends DoubleScalarInterface.Abs<AU, A, RU, R>,
        RU extends Unit<RU>, R extends DoubleScalarInterface.RelWithAbs<AU, A, RU, R>>
        extends DoubleScalarInterface<RU, R>, Scalar.RelWithAbs<AU, A, RU, R>

With the default option in interfaces, and the possibility to define static functions in interfaces, several abstract classes can be removed from the hierarchy, without changing anything in the contract of a Scalar, Vector or Matrix such as the Length.

averbraeck commented 1 year ago

Base idea (without generics):

For the specific implementation for DoubleScalar (as an example -- the design for Vector and Matrix and for Float is similar), we have 6 classes in the old situation: DoubleScalarInterface, DoubleScalar, AbstractDoubleScalar, AbstractDoubleScalarRel, AbstractDoubleScalarAbs, and AbstractDoubleScalarRelWithAbs. This will be reduced to one abstract classDoubleScalar with three inner classes: Rel, Abs and RelWithAbs each implementing one of the Relative, Absolute or RelWithAbs interfaces.

averbraeck commented 1 year ago

Now with the generics (first design):

The Value as the basis:

The Absolute, Relative and AbsWithRel interfaces:

The Scalar for 0-dimensional Values:

The IndexedValue for Vectors and Matrices:

The Vector for 1-dimensional Values:

The Matrix for 2-dimensional Values:

averbraeck commented 1 year ago

Based on the above, DoubleScalar would be defined as follows:

The DoubleScalar classes for Abs, Rel and RelWithAbs can be implemented as follows:

To stay consistent with the currently generated classes, the last three classes are called AbstractDoubleScalarRel, AbstractDoubleScalarAbs and AbstractDoubleScalarRelRelWithAbs, which will be changed to the classnames without 'Abstract' later.

averbraeck commented 1 year ago

This implementation would simplify the number of interfaces and classes. A Speed would implement/extend as follows:

   Value --> Serializable, Cloneable
     ^
   Scalar --> Number, Comparable
     ^
DoubleScalar
     ^
DoubleScalarRel --> Relative
     ^
   Speed

A Length would implement/extend as follows:

   Value --> Serializable, Cloneable
     ^
   Scalar --> Number, Comparable
     ^
DoubleScalar
     ^
DoubleScalarRelWithAbs --> RelWithAbs --> Relative
     ^
   Speed
averbraeck commented 1 year ago