Search code examples
javagenericsjooq

JOOQ choose field Converter by another field


I want to convert between Set of Enum POJO and String[] Database(postgres) column. and the enum class would be changed by another field type. So I can say Enum class which's using in fooSet is changable and it's up to field type.

I know it's a messy. but I need a help.

Below are models

public interface A {

  enum B implements A {
    step1,
    step2,
    step3
  }

  enum C implements A {
    step4,
    step5,
    step6
  }
}
public abstract class Foo {
  private String type;
}
public abstract class FooA {
  private Set<B> fooSet;
}
public abstract class FooB {
  private Set<C> fooSet;
}

I want to make a Converter like below.

SetOfEnumConverter<U extends Enum<U> & A> implements Converter<String[], Set<U>> {

  @Override
  public Set<U> from(String[] databaseObject) {
    if (databaseObject == null) {
      return null;
    }
    return Arrays.stream(databaseObject)
        .map(x -> U.valueOf(U.class, x)).  // here's the problem point
        .collect(Collectors.toSet());
  }

  @Override
  public String[] to(Set<U> userObject) {
    if (userObject == null || userObject.isEmpty()) {
      return null;
    }
    String[] strings = userObject.stream()
        .map(Enum::name)
        .toArray(String[]::new);
    return ArrayUtils.isEmpty(strings) ? new String[0]: strings;
  }

  @Override
  public Class<String[]> fromType() {
    return String[].class;
  }

  @Override
  public Class<Set<U>> toType() {
    return (Class) TreeSet.class;
  }
}

But the problem is I can't point .class attribute from a generic type maybe because of Generic Type erasure.

So, What I want to do is mapping the setEnum class to be used in the field fooSet according to the field type. Because I have to make a single table for Foo and map from FooA, FooB and FooZ.


Solution

  • You can't do this without passing an actual Class<U> reference to your converter, e.g. like this:

    SetOfEnumConverter<U extends Enum<U> & A> implements Converter<String[], Set<U>> {
    
        final Class<U> u;
    
        SetOfEnumConverter(Class<U> u) {
            this.u = u;
        }
        // ...
    }
    

    And inside of the converter, you can use:

    Enum.valueOf(u, x)
    

    To look up arbitrary enum values by their names. Then, instantiate it with e.g.

    new SetOfEnumConverter<>(MyEnum.class);