简体   繁体   English

从枚举到类型的泛型映射

[英]Mapping from an Enum to a Type for a generic

I am writing a class that writes data (defined per interface) to different xml- output formats (different JAXB-Classes). 我正在编写一个类,将数据(每个接口定义)写入不同的xml输出格式(不同的JAXB类)。 All supported types are stored in an Enum (SupportedTypes). 所有受支持的类型都存储在一个Enum(SupportedTypes)中。 The Enum stores the the corresponding JAXB-Class. 枚举存储相应的JAXB-Class。 The enum looks like this: 枚举看起来像这样:

public enum Types {
/**
 * Invoice from hospitals.
 */
Type1(...generatedClasses.invoice.hospital400.request.RequestType.class),
/**
 * Invoice from hospital but with MediData's quality extensions. The
 * response has no extensions.
 */
Type2(...generatedClasses.invoice.hospital400_QO.request.RequestType.class);

/**
 * Class for request. represents the root element in corresponding xml.
 */
private Class<?> rType;

/**
 * 
 * @param requestType
 *            class of corresponding request type
 */
private InvoiceTypes(final Class<?> requestType) {
    this.requestType = requestType;
}

/**
 * @return the requestType
 */
public final Class<?> getRequestType() {
    return requestType;
}

}

My problem is how to use this types to instantiate typed generics like JAXBElement. 我的问题是如何使用此类型实例化类型化的泛型,如JAXBElement。 The typeEnum is given as parameter and i want to create JAXBElement but this is obviously not working. typeEnum作为参数给出,我想创建JAXBElement,但这显然不起作用。 Now i'am stuck. 现在我卡住了。 How to construct such a constructer or method. 如何构造这样的构造器或方法。

thx in advance 提前

Edit for clarification: 编辑以澄清:

Lets assume you create a class ("ClassForTypes") that supports different types - for whatever it does with them (TheirClass, SpecialClass, MyClass). 假设您创建了一个支持不同类型的类(“ ClassForTypes”),无论该类如何处理它们(TheirClass,SpecialClass,MyClass)。 The api will not publish those classes (they are very specific) but instead it will publish an "TypeEnum" (TypeOne, TypeTwo, TypeThree) that stores the types of the classes (TheirClass, SpecialClass, MyClass). 该api不会发布这些类(它们是非常特定的),而是会发布一个“ TypeEnum”(TypeOne,TypeTwo,TypeThree),用于存储类的类型(TheirClass,SpecialClass,MyClass)。 At construction time of ClassForTypes it will use the given TypeEnum to create let's say a List<type saved in enum> . 在构造ClassForTypes时,它将使用给定的TypeEnum来创建一个List<type saved in enum> How to construct such a ClassForTypes or it's constructor? 如何构造这样的ClassForTypes或它的构造函数?

Some example code (does not work): The enum from above i want to use this way: 一些示例代码(不起作用):从上面的枚举我想使用这种方式:

public class Blub{

    public Blub(Types type){
        List<type.getRequestType> typedList = new ArrayList...
    }

}

This does not work. 这是行不通的。 But the type for the list is known on compile time (because it is stored in an enum?). 但是列表的类型在编译时是已知的(因为它存储在枚举中?)。 Is there any way to statically store a type and use it to get a typed generic? 有什么方法可以静态存储类型并使用它来获得类型化的泛型? I don't want the api user knows something about the single request types the user should only know about "supported types" delivered via the enum. 我不希望api用户了解有关单个请求类型的信息,而用户只应了解通过枚举传递的“受支持的类型”。

Your question is not too clear on exactly what you want to do. 您对要做什么的问题不太清楚。 Are you trying to add functionality to your enum? 您是否要向枚举添加功能? Something like this? 像这样吗

public enum Types {
  Type1(String.class) {
    @Override
    public Object make () {
      return new String();
    }
  },
  Type2(Integer.class) {
    @Override
    public Object make () {
      return new Integer(0);
    }
  };

  private Class<?> rType;

  Types(final Class<?> requestType) {
    this.rType = requestType;
  }

  public final Class<?> getRequestType() {
    return rType;
  }

  // All types must have a make method.
  public abstract Object make();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM