This is the first time I tried to use generics and luckily, it works for me, but I don't know if this is really supposed to be like this (below) or if there is another - more elegant - way to solve this nestings/abstraction.
If so, could you please give me a hint / link or how to ease this without destroying the structure?
This is just a simplification of the original structure, but it is kind of similar nested in one graph. The original code is about physics / mechanics (I'm sorry about the lack of senselessness in this example).
I'm working with:
- Hibernate -> Entities
- Java-GraphQL -> API / DTO-Serialization
- MapStruct -> Entities <-bidirectional with ignorings, etc-> DTO's
- Lombok -> lombok.experimental is good enough
- Spring
My abstract classes are looking like:
@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class City<HM extends Humanoides<LB>, AN extends Animal<LB>, LB extends Limbs> {
SortedSet<HM> humans;
SortedSet<AN> animals;
SortedSet<LB> limbs;
}
@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Mammal<LB extends Limbs> {
private SortedSet<LB> extremities;
}
@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Humanoides<LB extends Limbs> extends Mammal<LB> implements Comparable<Humanoides<LB>> {
private int iq;
//compareTo(Humanoides<LB> h)
}
@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Animal<LB extends Limbs> extends Mamal<LB> implements Comparable<Animal<LB>> {
private int speed;
//compareTo(Animal<LB> a)
}
@Getter @Setter @AllArgsConstructor @SuperBuilder @MappedSuperclass
public abstract class Limbs implements Comparable<Limbs> {
private int length;
//compareTo(Limbs l)
}
my Entities are:
@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(CityStuff.class)
public class CityEntity extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {}
@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(HumanoideStuff.class)
public class HumanoideEntity extends Humanoide<LimbsEntity> {}
@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(AnimalStuff.class)
public class AnimalEntity extends Animal<LimbsEntity> {}
@Getter @Setter @NoArgsConstructor @SuperBuilder @IdClass(LimbsStuff.class)
public class LimbsEntity extends Limbs{}
and my DTO's are:
@Getter @Setter @NoArgsConstructor @SuperBuilder
public class CityDto extends City<HumanoideEntity, AnimalEntity, LimbsEntity> {
//manipulate()
}
@Getter @Setter @NoArgsConstructor @SuperBuilder
public class HumanoideDto extends Humanoide<LimbsEntity> {
//manipulate()
}
@Getter @Setter @NoArgsConstructor @SuperBuilder
public class AnimalDto extends Animal<LimbsEntity> {
//manipulate()
}
@Getter @Setter @NoArgsConstructor @SuperBuilder
public class LimbsDto extends Limbs {
//manipulate()
}
implements Comparable
– Andy Turner Apr 20 '21 at 15:42`, you are saying there is a natural ordering on Humanoideses. I don't think there really is such an ordering, since there are many ways in which you can order Humanoids.