0

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()
}
getnosleep
  • 1
  • 1
  • 4
  • I am not very sure what is your actual question, but this looks good. very good for a "first time" with generics. – Eugene Apr 20 '21 at 15:24
  • This seems very complicated to me. Do you really need `LB extends Limbs`? What does the type `Limbs` really mean? Is it just something to hold a number of limbs? If so, you don't need it to be a type parameter, you just need classes of things which have limbs to implement an interface e.g. `HasLimbs`. – Andy Turner Apr 20 '21 at 15:33
  • I'd also question the `implements Comparable<...>` here: if `Humanoides implements Comparable`, 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. – Andy Turner Apr 20 '21 at 15:42
  • @Eugene thanks. And it's really my first time (but the compiler and debugger helped me a lot). I just didn't knew if such a construct is supposed to look like this or if there is a more elegant way to do it. As you can imagine, the original objects are not Humanoide etc. The original objects are about measurements in physics / mechanics *- as I wrote above -* and ... yeah ... I think I'll loose my job when I publish it :D – getnosleep Apr 20 '21 at 17:55

0 Answers0