|
| 1 | +package org.framefork.typedIds.uuid.hibernate; |
| 2 | + |
| 3 | +import org.framefork.typedIds.uuid.ObjectUuid; |
| 4 | +import org.framefork.typedIds.uuid.ObjectUuidTypeUtils; |
| 5 | +import org.hibernate.dialect.Dialect; |
| 6 | +import org.hibernate.engine.jdbc.Size; |
| 7 | +import org.hibernate.type.SqlTypes; |
| 8 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 9 | +import org.hibernate.type.descriptor.java.BasicJavaType; |
| 10 | +import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; |
| 11 | +import org.hibernate.type.descriptor.java.MutabilityPlan; |
| 12 | +import org.hibernate.type.descriptor.java.UUIDJavaType; |
| 13 | +import org.hibernate.type.descriptor.jdbc.AdjustableJdbcType; |
| 14 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 15 | +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; |
| 16 | +import org.hibernate.type.descriptor.jdbc.VarbinaryJdbcType; |
| 17 | +import org.hibernate.usertype.DynamicParameterizedType; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | + |
| 21 | +import java.io.ObjectInputStream; |
| 22 | +import java.io.ObjectOutputStream; |
| 23 | +import java.io.Serializable; |
| 24 | +import java.lang.invoke.MethodHandle; |
| 25 | +import java.lang.reflect.Type; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.Properties; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +public class ObjectUuidJavaType implements BasicJavaType<ObjectUuid<?>>, DynamicParameterizedType, Serializable |
| 31 | +{ |
| 32 | + |
| 33 | + public static final int UUID_BYTE_LENGTH = 16; |
| 34 | + |
| 35 | + private final UUIDJavaType inner; |
| 36 | + |
| 37 | + @Nullable |
| 38 | + private Class<ObjectUuid<?>> identifierClass; |
| 39 | + @Nullable |
| 40 | + private MethodHandle constructor; |
| 41 | + |
| 42 | + public ObjectUuidJavaType() |
| 43 | + { |
| 44 | + this.inner = UUIDJavaType.INSTANCE; |
| 45 | + } |
| 46 | + |
| 47 | + @SuppressWarnings("unchecked") |
| 48 | + @Override |
| 49 | + public void setParameterValues(final Properties parameters) |
| 50 | + { |
| 51 | + @Nullable var parameterType = (ParameterType) parameters.get(PARAMETER_TYPE); |
| 52 | + if (parameterType != null) { |
| 53 | + this.identifierClass = (Class<ObjectUuid<?>>) parameterType.getReturnedClass(); |
| 54 | + |
| 55 | + } else { |
| 56 | + String entityClass = Objects.requireNonNull(parameters.get(ENTITY), "parameters.get(ENTITY) must not be null").toString(); |
| 57 | + String propertyName = Objects.requireNonNull(parameters.get(PROPERTY), "parameters.get(PROPERTY) must not be null").toString(); |
| 58 | + |
| 59 | + this.identifierClass = ObjectUuidTypeUtils.readIdentifierClass(entityClass, propertyName); |
| 60 | + } |
| 61 | + |
| 62 | + if (!ObjectUuid.class.isAssignableFrom(identifierClass)) { |
| 63 | + throw new IllegalArgumentException("Type %s is not a subtype of %s".formatted(identifierClass, ObjectUuid.class)); |
| 64 | + } |
| 65 | + |
| 66 | + this.constructor = ObjectUuidTypeUtils.getMainConstructor(identifierClass); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Type getJavaType() |
| 71 | + { |
| 72 | + return getJavaTypeClass(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Class<ObjectUuid<?>> getJavaTypeClass() |
| 77 | + { |
| 78 | + return Objects.requireNonNull(identifierClass, "identifierClass must not be null"); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int extractHashCode(final ObjectUuid<?> value) |
| 83 | + { |
| 84 | + return Objects.hashCode(value); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean areEqual( |
| 89 | + final ObjectUuid<?> one, |
| 90 | + final ObjectUuid<?> another |
| 91 | + ) |
| 92 | + { |
| 93 | + return Objects.equals(one, another); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public JdbcType getRecommendedJdbcType(final JdbcTypeIndicators indicators) |
| 98 | + { |
| 99 | + final JdbcType descriptor = indicators.getJdbcType(indicators.resolveJdbcTypeCode(SqlTypes.UUID)); |
| 100 | + return descriptor instanceof AdjustableJdbcType |
| 101 | + ? ((AdjustableJdbcType) descriptor).resolveIndicatedType(indicators, this) |
| 102 | + : descriptor; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public long getDefaultSqlLength(final Dialect dialect, final JdbcType jdbcType) |
| 107 | + { |
| 108 | + if (jdbcType instanceof VarbinaryJdbcType) { |
| 109 | + return UUID_BYTE_LENGTH; |
| 110 | + } |
| 111 | + |
| 112 | + return Size.DEFAULT_LENGTH; |
| 113 | + } |
| 114 | + |
| 115 | + @Nullable |
| 116 | + @Override |
| 117 | + public <X> X unwrap( |
| 118 | + @Nullable final ObjectUuid<?> value, |
| 119 | + @NotNull final Class<X> type, |
| 120 | + @NotNull final WrapperOptions options |
| 121 | + ) |
| 122 | + { |
| 123 | + if (value == null) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + return inner.unwrap(value.toNativeUuid(), type, options); |
| 128 | + } |
| 129 | + |
| 130 | + @Nullable |
| 131 | + @Override |
| 132 | + public <X> ObjectUuid<?> wrap( |
| 133 | + @Nullable final X value, |
| 134 | + @NotNull final WrapperOptions options |
| 135 | + ) |
| 136 | + { |
| 137 | + if (value == null) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + return wrapUuidToIdentifier(inner.wrap(value, options)); |
| 142 | + } |
| 143 | + |
| 144 | + @Nullable |
| 145 | + @Override |
| 146 | + public ObjectUuid<?> fromString(@Nullable final CharSequence string) |
| 147 | + { |
| 148 | + return (string == null) ? null : wrapUuidToIdentifier(UUID.fromString(string.toString())); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public MutabilityPlan<ObjectUuid<?>> getMutabilityPlan() |
| 153 | + { |
| 154 | + return ImmutableMutabilityPlan.instance(); |
| 155 | + } |
| 156 | + |
| 157 | + private ObjectUuid<?> wrapUuidToIdentifier(final UUID uuid) |
| 158 | + { |
| 159 | + return ObjectUuidTypeUtils.wrapUuidToIdentifier( |
| 160 | + uuid, |
| 161 | + Objects.requireNonNull(constructor, "constructor was not yet initialized") |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public String toString() |
| 167 | + { |
| 168 | + return "object-uuid(%s)".formatted(identifierClass != null ? identifierClass.getName() : "???"); |
| 169 | + } |
| 170 | + |
| 171 | + @SuppressWarnings("unused") |
| 172 | + private void writeObject(final ObjectOutputStream stream) |
| 173 | + { |
| 174 | + throw new UnsupportedOperationException("Serialization not supported"); |
| 175 | + } |
| 176 | + |
| 177 | + @SuppressWarnings("unused") |
| 178 | + private void readObject(final ObjectInputStream stream) |
| 179 | + { |
| 180 | + throw new UnsupportedOperationException("Serialization not supported"); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments