Java 从不同对象中的同一方法中删除重复代码
我有两个不同的对象,参数和变量,它们是上下文字段。Java 从不同对象中的同一方法中删除重复代码,java,oop,object,collections,java-stream,Java,Oop,Object,Collections,Java Stream,我有两个不同的对象,参数和变量,它们是上下文字段。 我还有第三个对象:ParameterBase,它不是上下文的一部分。 所有3个类都是自动生成的,我无法修改它们。 所有3个类都有2个字段:名称和值,类型为字符串。 所有3个类都有getter和setter 我实施了以下方法: public static List<ParametersBase> removeDuplicate(List<ParametersBase> parameterList) { return
我还有第三个对象:ParameterBase,它不是上下文的一部分。
所有3个类都是自动生成的,我无法修改它们。
所有3个类都有2个字段:名称和值,类型为字符串。
所有3个类都有getter和setter 我实施了以下方法:
public static List<ParametersBase> removeDuplicate(List<ParametersBase> parameterList) {
return new ArrayList<>(parameterList.stream().collect(
Collectors.toMap(
ParametersBase::getParamName,
Function.identity(),
Utils::mergeDuplicate
)
).values());
}
private static ParametersBase mergeDuplicate(ParametersBase a, ParametersBase b) {
if (a.getParamValue().equals(b.getParamValue())) {
return a;
} else {
throw new IllegalArgumentException("Error message");
}
}
public static List removeDuplicate(列表参数列表){
返回新的ArrayList(parameterList.stream().collect(
汤姆(
ParametersBase::getParamName,
Function.identity(),
Utils::mergeDuplicate
)
).values());
}
私有静态参数base mergeDuplicate(参数base a、参数base b){
如果(a.getParamValue().equals(b.getParamValue())){
返回a;
}否则{
抛出新的IllegalArgumentException(“错误消息”);
}
}
我想对其他两个类使用这个方法,所以我这样做了:
private static void removeDuplicatesParams(Context context) {
final List<ParametersBase> parameterList = emptyIfNull(
context.getParameters()).stream()
.map(parameter -> new ParametersBase()
.paramName(parameter.getParamName())
.paramValue(parameter.getParamValue()))
.collect(Collectors.toList());
List<ParametersBase> distinctParameterList = removeDuplicate(parameterList);
List<Parameter> distinctParametersList = distinctParameterList
.stream().map(temp -> new Parameter().paramName(temp.getParamName())
.paramValue(temp.getParamValue())).collect(Collectors.toList());
context.setParameters(distinctParametersList);
}
private static void removeDuplicatesVariables(Context context) {
final List<ParametersBase> parameterList = emptyIfNull(
context.getVariables()).stream()
.map(parameter -> new ParametersBase()
.paramName(parameter.getParamName())
.paramValue(parameter.getParamValue()))
.collect(Collectors.toList());
List<ParametersBase> distinctParameterList = removeDuplicate(parameterList);
List<Variable> distinctParametersList = distinctParameterList
.stream().map(temp -> new Variable().paramName(temp.getParamName())
.paramValue(temp.getParamValue())).collect(Collectors.toList());
context.setVariables(distinctParametersList);
}
private static void removedupplicatesparams(上下文){
最终列表参数List=emptyIfNull(
context.getParameters()).stream()
.map(参数->新参数数据库()
.paramName(参数.getParamName())
.paramValue(parameter.getParamValue())
.collect(Collectors.toList());
List distinctParameterList=移除的副本(parameterList);
List distinctParametersList=distinctParameterList
.stream().map(temp->new Parameter().paramName(temp.getParamName())
.paramValue(temp.getParamValue()).collect(Collectors.toList());
setParameters(distinctParametersList);
}
私有静态void removedupplicatesvariables(上下文){
最终列表参数List=emptyIfNull(
context.getVariables()).stream()
.map(参数->新参数数据库()
.paramName(参数.getParamName())
.paramValue(parameter.getParamValue())
.collect(Collectors.toList());
List distinctParameterList=移除的副本(parameterList);
List distinctParametersList=distinctParameterList
.stream().map(temp->new Variable().paramName(temp.getParamName())
.paramValue(temp.getParamValue()).collect(Collectors.toList());
setVariables(distinctParametersList);
}
如您所见,我创建的两个方法几乎相同,但由于我有两个自动生成的类,所以我必须复制代码。有没有办法让代码更漂亮?我正在使用Java 8。您可以泛化
removeDuplicate
方法:
private static <T> List<T> removeDuplicate(
Collection<? extends T> parameterList,
Function<? super T, String> name,
Function<? super T, String> value
) {
return new ArrayList<>(parameterList.stream().collect(
Collectors.<T, String, T>toMap(
name,
Function.identity(),
(a, b) -> mergeDuplicate(a, b, value)
)).values()
);
}
private static <T> T mergeDuplicate(T a, T b, Function<? super T, String> value) {
if (value.apply(a).equals(value.apply(b))) return a;
else throw new IllegalArgumentException("Error message");
}
或者,您可以应用:
专用接口参数适配器{
T原始();
字符串名();
字符串值();
}
私有静态列表移除副本(
收集据我所知,你可以用泛型解决这个问题。也许这个可以帮助你:@Hubi这里的情况有点不同,因为这个问题涉及复杂的对象,所以需要映射。你能添加removeDuplicate
方法源代码吗?我认为它可以泛型。public static List removeDuplicate(List parameterList){返回新的ArrayList(parameterList.stream().collect(Collectors.toMap(ParametersBase::getParamName,Function.identity(),Utils::mergeDuplicate)).values();}私有静态参数base mergeDuplicate(ParametersBase a,ParametersBase b){如果(a.getParamValue().equals(b.getParamValue()){返回a;}否则{抛出新异常();}@BananonIs这三个类之间有继承关系吗?我使用了你建议的适配器设计模式。谢谢。你一定读过OP的想法,因为问题中没有任何信息表明整个操作都是关于删除重复名称的。它也没有告诉我们你使用的合并逻辑。@Holger我是我要求添加removeDuplicate
方法源代码,@Amir在评论部分发布了它以及mergeDuplicate
。
private static void removeDuplicatesParams(Context context) {
context.setParameters(removeDuplicate(
context.getParameters(),
Parameter::getParamName,
Parameter::getParamValue
));
}
private interface ParameterAdapter<T> {
T original();
String name();
String value();
}
private static <T> List<T> removeDuplicate(
Collection<? extends T> parameterList,
Function<? super T, ? extends ParameterAdapter<T>> adapter
) {
return parameterList.stream()
.<ParameterAdapter<T>>map(adapter)
.collect(Collectors.toMap(
ParameterAdapter::name,
Function.identity(),
Utils::mergeDuplicate
)).values().stream()
.map(ParameterAdapter::original)
.collect(Collectors.toList());
}
private static <T> ParameterAdapter<T> mergeDuplicate(
ParameterAdapter<T> a,
ParameterAdapter<T> b
) {
if (a.value().equals(b.value())) return a;
else throw new IllegalArgumentException("Error message");
}
private static void removeDuplicatesParams(Context context) {
context.setParameters(removeDuplicate(
context.getParameters(),
Utils::adaptParameter
));
}
private static ParameterAdapter<Parameter> adaptParameter(Parameter parameter) {
return new ParameterAdapter<Parameter>() {
@Override
public Parameter original() { return parameter; }
@Override
public String name() { return parameter.getParamName(); }
@Override
public String value() { return parameter.getParamValue(); }
};
}