Java加入重复的方法
Java join duplicate methods
除了实体和可运行类型之外,我有两个服务方法大部分相同。
有没有办法加入他们?
我认为泛型可能会有帮助,但不知道如何在此处应用它们
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));
}
}
}
}
这是我找到的最好的方法。如果你真的想这样做,这是一种方式,但我真的不推荐它。
你最终会得到那些方法:
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;
}
}
}
}
这需要为 StopSearchTask
和 CrimeTask
创建一个 parent class:
public class BaseTask { }
public class StopSearchTask extends BaseTask { }
public class CrimeTask extends BaseTask { }
parent class Force
和 Station
:
public class BaseClass { }
public class Station extends BaseClass { }
public class Force extends BaseClass { }
还有一个枚举来处理这两种类型。
public static enum TypeEnum {
STATION ,
FORCE;
}
除了实体和可运行类型之外,我有两个服务方法大部分相同。
有没有办法加入他们?
我认为泛型可能会有帮助,但不知道如何在此处应用它们
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));
}
}
}
}
这是我找到的最好的方法。如果你真的想这样做,这是一种方式,但我真的不推荐它。
你最终会得到那些方法:
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;
}
}
}
}
这需要为 StopSearchTask
和 CrimeTask
创建一个 parent class:
public class BaseTask { }
public class StopSearchTask extends BaseTask { }
public class CrimeTask extends BaseTask { }
parent class Force
和 Station
:
public class BaseClass { }
public class Station extends BaseClass { }
public class Force extends BaseClass { }
还有一个枚举来处理这两种类型。
public static enum TypeEnum {
STATION ,
FORCE;
}