Java Recursive generic pattern: what does it mean ... S extends Writer <E>> extends Entity <E, S>
Can someone explain the rather complex recursive use of template templates below?
public abstract class Data<E extends Data<E, S>,
S extends Writer<E>> extends Entity<E,S>
What we should keep in mind when using recursive generics as above. And what will be the relationships and rules between these types, here is E and S ?
If available, list some resources / links / books about this type of shared use. I know that one book talks about it, Effective Java, 2nd edition of Joshua Bloch (Paragraph 27)
Data : E, , S, Writer (, E). , Data<E,S> / Entity, E S (.. Entity Data<E,S> Writer<E>).
:
NumericalData extends Data<NumericalData, NumWriter> NumWriter / Writer<NumericalData> NumericalData Entity<NumericalData, NumWriter>.
EDIT:
- ? , /, Data<E,S>, / . , Data<E,S>
E doSomething(E toThis) { toThis.aDataClassMethod(); return toThis; }
, , E Data<E,S>, , , toThis E.
, - , , . , "", - , .
S extends Writer<E>
S E
extends Entity<E,S>
, Data Entity.
E extends Data<E, S>
, E, Data / Data, , , .
E S :
//E = Example, S = ExampleWriter
public class ExampleWriter implements Writer<Example>{
//...
}
public class Example extends Data<Example,ExampleWriter>{
//...
}
: , Writer<SomeChildOfExample> Writer<SomeParentOfExample>, , , .
, , , "" . , Java rtl , .
For example, even Object should probably be an abstract recursive type, at least to provide strict equality rules:
public abstract class Object<T extends Object<T>> {
...
public boolean equals( o :T ) {
...
}
}
There are no more validation instances in equals () implementations, and more importantly, it is better to check compile time for equals () calls.
However, perhaps a more appropriate and less complex function would be the type "I" ...