java连接重复方法

pkbketx9  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(365)

除了实体和可运行类型之外,我在服务中有两个方法基本相同。
有办法加入他们吗?
我认为泛型可能会有所帮助,但我不知道如何在这里应用它们

public class PoliceService {
    private static final int API_REQUEST_PER_SEC = 15;
    private static final ExecutorService apiExecutor = Executors.newFixedThreadPool(API_REQUEST_PER_SEC); 

 public void downloadAllCrimesInDateRange(Map<String, String> map) {
        // Duplicate variables from map here

        List<Station> list = new ArrayList<>();
        CsvParser.getLinesFromCsvFile(file)
                 .forEach(strings -> list.add(new Station(strings[0], strings[2], strings[1])));

        for (int i = 0; i <= months; i++) {
            LinkedBlockingQueue<Station> queue = new LinkedBlockingQueue<>(list);

            for (int j = 0; j < queue.size(); j++) {
                apiExecutor.submit(new CrimeTask(queue.remove(),
                        from.plusMonths(i),
                        writeFile));
            }
        }
    }

    public void downloadStopAndSearch(Map<String, String> map) {
        // Duplicate variables from map here

        List<Force> list = new ArrayList<>();
        CsvParser.getLinesFromCsvFile(file)
                 .forEach(strings -> list.add(new Force(strings[0], strings[1])));

        for (int i = 0; i <= months; i++) {
            LinkedBlockingQueue<Force> queue = new LinkedBlockingQueue<>(list);

            for (int j = 0; j < queue.size(); j++) {
                apiExecutor.submit(new StopSearchTask(queue.remove(),
                        from.plusMonths(i),
                        writeFile));
            }
        }
    }
}
sc4hvdpw

sc4hvdpw1#

这是我找到的最好的方法。如果你真的想这样做,这是一种方法,但我并不真的建议。
您最终将拥有以下方法:

public void downloadAllCrimesInDateRange(Map<String, String> map) {

    newMethod(map, TypeEnum.STATION);
}

public void downloadStopAndSearch(Map<String, String> map) {

    newMethod(map, TypeEnum.FORCE);
}

public void newMethod(Map<String, String> map, TypeEnum type) {

    List<BaseClass> list = new ArrayList<>();

    switch (type) {
    case FORCE:
        CsvParser.getLinesFromCsvFile(file)
                .forEach(strings -> list.add(new Force(strings[0], strings[1])));
        break;

    case STATION:
        CsvParser.getLinesFromCsvFile(file)
            .forEach(strings -> list.add(new Station(strings[0], strings[2], strings[1])));
        break;
    }

    for (int i = 0; i <= months; i++) {
        LinkedBlockingQueue<BaseClass> queue = new LinkedBlockingQueue<>(list);

        for (int j = 0; j < queue.size(); j++) {
            switch (type) {
            case FORCE:
                apiExecutor.submit(new StopSearchTask(queue.remove(),
                        from.plusMonths(i), writeFile));
                break;

            case STATION:
                apiExecutor.submit(new CrimeTask(queue.remove(),
                        from.plusMonths(i), writeFile));
                break;

            }
        }

    }
}

这需要为其创建父类 StopSearchTaskCrimeTask :

public class BaseTask { }

public class StopSearchTask extends BaseTask { }

public class CrimeTask extends BaseTask { }

和一个用于 ForceStation :

public class BaseClass { }

public class Station extends BaseClass { }

public class Force extends BaseClass { }

和一个枚举来处理这两种类型。

public static enum TypeEnum {
        STATION ,
        FORCE;
}

相关问题