JOOQ在另一个字段中选择字段转换器

发布于 2025-01-22 14:05:56 字数 1955 浏览 2 评论 0原文

我想在枚举POJO和String []数据库(Postgres)列之间转换。 枚举类将通过另一个字段type更改。 因此,我可以说在fooset中使用的枚举类是可更改的,并且可以通过字段type

我知道这很混乱。但是我需要帮助。

以下是

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;
}

我想制作一个类似于下面的转换器的型号。

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;
  }
}

但是问题是我不能指向.class属性,可能是由于通用类型的擦除而属性。

因此,我想做的是根据 fooset setEnum 类 根据 type 。 因为我必须为Foo制作一个表,并从Fooa,Foob和Fooz制作映射。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2025-01-29 14:05:56

如果不传递实际class&lt; u&gt;对转换器的引用,例如,例如这样:

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

    final Class<U> u;

    SetOfEnumConverter(Class<U> u) {
        this.u = u;
    }
    // ...
}

在转换器的内部,您可以使用:您可以使用:您可以使用:

Enum.valueOf(u, x)

查找任意枚举值。然后,用例如

new SetOfEnumConverter<>(MyEnum.class);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文