如何正确使用持有人的泛型 [英] How to use generics properly for a Holder

查看:135
本文介绍了如何正确使用持有人的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的应用程序中使用的不同对象创建一个Holder类,直到某种程度上,此代码才能正常工作,但构建器模式对于可选字段工作正常,但我想这个持有者可以被重构为接受任意数量的参数

  package pojos; 

公开课持有人< T,R,S,U> {
private final T t;
private final R r;

private final S s;
private final U u;


私人持有人(最终建造者< T,R,S,U>建造者){
this.t = builder.t;
this.r = builder.r;
this.s = builder.s;
this.u = builder.u;
}


public T getField1(){
return this.t;
}

public R getField2(){
return this.r;
}

public S getField3(){
return this.s;
}

public U getField4(){
return this.u;
}


public static class Builder< T,R,S,U> {
private T t;
私人R r;
私人S s;
私人U u;


public Builder field1(final T t){
this.t = t;
返回此;
}

public Builder field2(final R r){
this.r = r;
返回此;
}

public Builder field3(final S s){
this.s = s;
返回此;
}

public Builder field4(final U u){
this.u = u;
返回此;
}

公共持有人< T,R,S,U> build(){
返回新的Holder<>(this);
}

public Builder< T,R,S,U>复制(最终Holder< T,R,S,U> rowMappingsHolder){
this.t = rowMappingsHolder.getField1();
this.r = rowMappingsHolder.getField2();
this.s = rowMappingsHolder.getField3();
this.u = rowMappingsHolder.getField4();
返回此;
}


}

}}使用示例:

 受保护的Holder< Row,Map< Integer, String>,Void,Void> getRowMapHolder(Row row,Map< Integer,String> map){
return(Holder< Row,Map< Integer,String> ;, Void,Void>)new Holder.Builder< Row,Map< Integer,String> ;, Void>().field1(row).field2(map).build();

$ / code>

任何想法?

问候



〜Marco

解决方案

感谢Andy的评论和Google Autovalue是一个很好的解决方案:

所以我们可以创建不同的有意义的类,没有更多的field1,field2...

  package pojos; 

导入com.google.auto.value.AutoValue;
import org.apache.poi.ss.usermodel.Row;

import java.util.Map;

@AutoValue
公共抽象类RowMapHolder {

public abstract Row row();

public abstract Map< Integer,String>映射();
$ b $ public static RowMapHolder create(Row row,Map< Integer,String> mapping){
return new AutoValue_RowMapHolder(row,mapping);
}

}

  package pojos; 

导入com.google.auto.value.AutoValue;

import java.util.List;
import java.util.Map;

@AutoValue
public abstract class KeyValuesMapHolder {

public abstract List< KeyValue< String,String>>键值();
public abstract Map< Integer,String>映射();
$ b $ public static KeyValuesMapHolder create(List< KeyValue< String,String>> keyValues,Map< Integer,String>映射){
返回新的AutoValue_KeyValuesMapHolder(keyValues,mapping);
}

}


I am trying to create a Holder class for different objects to be used in my application, I ended up with this code that works fine until some extent, the builder pattern works fine for the optional fields, but I guess this holder could be refactored to accept any arbitrarily number of parameter

package pojos;

public class Holder<T, R, S, U> {
private final T t;
private final R r;

private final S s;
private final U u;


private Holder(final Builder<T, R, S, U> builder) {
    this.t = builder.t;
    this.r = builder.r;
    this.s = builder.s;
    this.u = builder.u;
}


public T getField1() {
    return this.t;
}

public R getField2() {
    return this.r;
}

public S getField3() {
    return this.s;
}

public U getField4() {
    return this.u;
}


public static class Builder<T, R, S, U> {
    private T t;
    private R r;
    private S s;
    private U u;


    public Builder field1(final T t) {
        this.t = t;
        return this;
    }

    public Builder field2(final R r) {
        this.r = r;
        return this;
    }

    public Builder field3(final S s) {
        this.s = s;
        return this;
    }

    public Builder field4(final U u) {
        this.u = u;
        return this;
    }

    public Holder<T, R, S, U> build() {
        return new Holder<>(this);
    }

    public Builder<T, R, S, U> copy(final Holder<T, R, S, U> rowMappingsHolder) {
        this.t = rowMappingsHolder.getField1();
        this.r = rowMappingsHolder.getField2();
        this.s = rowMappingsHolder.getField3();
        this.u = rowMappingsHolder.getField4();
        return this;
    }


}

}

Example of usage:

protected Holder<Row, Map<Integer, String>, Void, Void> getRowMapHolder(Row row, Map<Integer,String> map) {
    return (Holder<Row, Map<Integer, String>, Void, Void>) new Holder.Builder<Row, Map<Integer, String>,Void, Void>().field1(row).field2(map).build();
}

Any ideas?

Regards

~Marco

解决方案

Thanks to Andy's comment and Google Autovalue, a good solution arose:

So we can create different classes that have meaning, no more "field1", "field2"...

package pojos;

import com.google.auto.value.AutoValue;
import org.apache.poi.ss.usermodel.Row;

import java.util.Map;

@AutoValue
public abstract class RowMapHolder {

    public abstract Row row();

    public abstract Map<Integer,String> mapping();

    public static RowMapHolder create(Row row, Map<Integer, String> mapping) {
        return new AutoValue_RowMapHolder(row, mapping);
    }

}

or

package pojos;

import com.google.auto.value.AutoValue;

import java.util.List;
import java.util.Map;

@AutoValue
public abstract class KeyValuesMapHolder {

    public abstract List<KeyValue<String,String>> keyValues();
    public abstract Map<Integer,String> mapping();

    public static KeyValuesMapHolder create(List<KeyValue<String, String>> keyValues, Map<Integer, String> mapping) {
        return new AutoValue_KeyValuesMapHolder(keyValues, mapping);
    }

}

这篇关于如何正确使用持有人的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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