Hello Riccardo,

It should be possible to support EnumSet with ExtendedType.
I did some prototyping, and it looks like you need to implement these parts:

1. an ExtendedTypeFactory as EnumSet has internal subclasses (that also
prevents using ValueType for this):

public ExtendedType getType(Class<?> objectClass) {
  if(EnumSet.class.isAssignableFrom(objectClass)) {
      return new EnumSetType();
  }
  return null;
}

2. an ExtendedType should deal with the serialization to/from the actual
datatype you are using in the DB:

public class EnumSetType implements ExtendedType<EnumSet> {
  @Override
  public void setJdbcObject(PreparedStatement statement, EnumSet value, int
pos, int type, int scale) throws Exception {
    statement.setString(pos, toString(value));
  }

  @Override
  public EnumSet<?> materializeObject(ResultSet rs, int index, int type)
throws Exception {
    return fromString(rs.getString(index));
  }

  String toString(EnumSet value) {
    // probably should account for the Enum type
  }

  EnumSet fromString(String value) {
  }
}

3. and finally register factory with the runtime:

ServerModule.contributeTypeFactories(binder)
        .add(new EnumSetFactory())

Hope this helps!

On Sat, Apr 5, 2025 at 8:03 PM Riccardo De Menna <deme...@tuorlo.net> wrote:

> Hello,
>
> I was wondering if there is a way to add a custom data type for a non
> extendable class. Specifically I’m thinking of java.util.EnumSet.
>
> I often use Enums in my object classes and I occasionally employ EnumSets
> when the case requires it. Unfortunately EnumSet is non extendable so I
> can’t define a subclass and then use that as java type in the modeller.
>
> Is there a way to hack the runtime into returning an EnumSet using a
> custom ValueType or ExtenedType for multiple attributes (it does work if
> there’s only one attribute using EnumSet but as soon as you define two the
> runtime gets confused).
>
> At the moment I’m just extending a HashSet<E extends Enum<E>> as a
> workaround but every time I see that I feel guilty not being able to use
> the ‘proper’ set collection for enums.
>
> Thank you,
> Riccardo De Menna



-- 
Best regards,
Nikita Timofeev

Reply via email to