The situation:
I have a fixed structure in which certain groups are located. When a new entry is added, it is inserted at the end. The ID set in the database is continuous, but the problem is that it is not in the corresponding group. So I can't sort by IDs. Each entry always gets one or more so-called DB-numbers. Using this, I can add the new entry to a specific group. I sort the entries according to the numerical limits of the DB-numbers.
The base entity for this is as follows:
public class KEntity {
private Long kId;
private int kNumber;
private String kText;
private Integer db_8;
private Integer db_7;
private Integer db_6;
private Integer db_5;
private Integer db_4;
private Integer db_3;
private Integer db_2;
private Integer db_1;
getters & setters...
}
If a new entry is added, and the Db number, for example, is in the range 10000 to 10070, it is assigned to the first group in this case, which I solved as follows:
int from = 10000;
int to = 10070;
int sum = 1010000;
public List<KEntity> buildGroup(List<KEntity> kEntityList, int from, int to, int sum) {
return kEntityList.stream()
.filter(k -> k.getDb_1() != null && k.getDb_1() >= from && k.getDb_1() <= to || k.getDb_1() != null && k.getDb_1()
.equals(sum))
.sorted(Comparator.comparingInt(KagEntity::getDb_1))
.collect(Collectors.toList());
}
I call this method to sort and create different groups based on the numeric boundaries. This works quite well so far.
In this case the filtering and sorting is related to the property DB_1. However, there are other classes in the system that do not have the property Db_1, for example, but only Db_2 or Db_3.
Something like that, for example:
public class FcEntity {
private Long fcId;
...
...
private int db2;
getters & setters...
}
If I want to send a list from FcEntity to the buildGroup() method, I logically can't call the getDb_1() method because it doesn't include the method.
My goal is to do this no matter what kind of class I'm using and no matter what Db property this class has. Use the buildGroup() method. So I don't have to rewrite the method for each class and avoid code duplication.
I don't know if there is a possibility. I thought I might solve this with Java Generics. But in the area of generics I have little to no experience so far. Or are there patterns for this case that I should or could use?
I hope the problem is understandable and you can help. I am open for improvements.
Aucun commentaire:
Enregistrer un commentaire