如何为具有不同属性的多个类调用过滤器和排序方法? [英] How to call a filter and sorting method for several classes with different properties?

查看:65
本文介绍了如何为具有不同属性的多个类调用过滤器和排序方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我有一个固定的结构,其中某些基团位于其中.添加新条目时,它将插入到末尾.数据库中设置的ID是连续的,但是问题在于它不在相应的组中.因此,我无法按ID排序.每个条目始终获得一个或多个所谓的DB号.使用此功能,我可以将新条目添加到特定组.我根据数据库编号的数字限制对条目进行排序.

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.

为此的基本实体如下:

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...

}

如果添加了一个新条目,并且Db编号在10000到10070的范围内,则在这种情况下将其分配给第一个组,我对此进行了如下解决:

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.

在这种情况下,过滤和排序与属性DB_1相关.但是,系统中还有其他一些类,它们不具有属性Db_1,例如,仅具有Db_2或Db_3.

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.

类似的事情:

public class FcEntity {

    private Long fcId;
    ...
    ...
    private int db2;

    getters & setters...

}

如果我想将列表从FcEntity发送到buildGroup()方法,则从逻辑上讲我不能调用getDb_1()方法,因为它不包含该方法.

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.

我的目标是无论我使用哪种类型的类,以及该类具有什么Db属性,都执行此操作.使用buildGroup()方法.因此,我不必为每个类重写方法,并且避免代码重复.

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.

我不知道是否有可能.我以为我可以用Java泛型来解决这个问题.但是到目前为止,在泛型领域我几乎没有经验.还是我应该或可以使用的这种情况的模式?

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.

推荐答案

您可以使buildGroup接受Collection<? extends T>Function<? super T, Integer>,以便它可以使用函数将集合元素映射为整数:

You can make buildGroup accept Collection<? extends T> and Function<? super T, Integer> so that it can use a function to map collection elements to integers:

public <T> List<T> buildGroup(
        Collection<? extends T> entities,
        Function<? super T, Integer> property,
        int from, int to, int sum
) {
    return entities.stream()
            .filter(elm -> isPropertyMatch(property.apply(elm), from, to, sum))
            .sorted(Comparator.comparing(property))
            .collect(Collectors.toList());
}

isPropertyMatch所在的位置:

private static boolean isPropertyMatch(Integer value, int from, int to, int sum) {
    if(value == null) return false;
    return value >= from && value <= to || value.equals(sum);
}

用例:

buildGroup(kEntityList, KEntity::getDb_1, from, to, sum);

这篇关于如何为具有不同属性的多个类调用过滤器和排序方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆