|
| 1 | +package org.framefork.typedIds.bigint.hibernate; |
| 2 | + |
| 3 | +import org.framefork.typedIds.bigint.ObjectBigIntId; |
| 4 | +import org.framefork.typedIds.bigint.ObjectBigIntIdTypeUtils; |
| 5 | +import org.framefork.typedIds.common.ReflectionHacks; |
| 6 | +import org.framefork.typedIds.common.hibernate.ParameterizedTypeUtils; |
| 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.LongJavaType; |
| 12 | +import org.hibernate.type.descriptor.java.MutabilityPlan; |
| 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.usertype.DynamicParameterizedType; |
| 17 | +import org.jetbrains.annotations.Contract; |
| 18 | +import org.jspecify.annotations.NonNull; |
| 19 | +import org.jspecify.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 | + |
| 29 | +@SuppressWarnings("removal") // DynamicParameterizedType usage |
| 30 | +public class ObjectBigIntIdJavaType implements BasicJavaType<ObjectBigIntId<?>>, DynamicParameterizedType, Serializable |
| 31 | +{ |
| 32 | + |
| 33 | + private final LongJavaType inner; |
| 34 | + |
| 35 | + @Nullable |
| 36 | + private Class<ObjectBigIntId<?>> identifierClass; |
| 37 | + @Nullable |
| 38 | + private MethodHandle constructor; |
| 39 | + |
| 40 | + public ObjectBigIntIdJavaType() |
| 41 | + { |
| 42 | + this.inner = LongJavaType.INSTANCE; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void setParameterValues(final Properties parameters) |
| 47 | + { |
| 48 | + this.identifierClass = ParameterizedTypeUtils.getReturnedClass(parameters, ObjectBigIntId.class); |
| 49 | + |
| 50 | + if (!ObjectBigIntId.class.isAssignableFrom(identifierClass)) { |
| 51 | + throw new IllegalArgumentException("Type %s is not a subtype of %s".formatted(identifierClass, ObjectBigIntId.class)); |
| 52 | + } |
| 53 | + |
| 54 | + this.constructor = ReflectionHacks.getConstructor(identifierClass, long.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Type getJavaType() |
| 59 | + { |
| 60 | + return getJavaTypeClass(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Class<ObjectBigIntId<?>> getJavaTypeClass() |
| 65 | + { |
| 66 | + return Objects.requireNonNull(identifierClass, "identifierClass must not be null"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public int extractHashCode(final ObjectBigIntId<?> value) |
| 71 | + { |
| 72 | + return Objects.hashCode(value); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean areEqual( |
| 77 | + final ObjectBigIntId<?> one, |
| 78 | + final ObjectBigIntId<?> another |
| 79 | + ) |
| 80 | + { |
| 81 | + return Objects.equals(one, another); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public JdbcType getRecommendedJdbcType(final JdbcTypeIndicators indicators) |
| 86 | + { |
| 87 | + final JdbcType descriptor = indicators.getJdbcType(indicators.resolveJdbcTypeCode(SqlTypes.BIGINT)); |
| 88 | + return descriptor instanceof AdjustableJdbcType |
| 89 | + ? ((AdjustableJdbcType) descriptor).resolveIndicatedType(indicators, this) |
| 90 | + : descriptor; |
| 91 | + } |
| 92 | + |
| 93 | + @Contract("null, _, _ -> null; !null, _, _ -> !null") |
| 94 | + @Nullable |
| 95 | + @Override |
| 96 | + public <X> X unwrap( |
| 97 | + @Nullable final ObjectBigIntId<?> value, |
| 98 | + @NonNull final Class<X> type, |
| 99 | + @Nullable final WrapperOptions options |
| 100 | + ) |
| 101 | + { |
| 102 | + if (value == null) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + return inner.unwrap(value.toLong(), type, options); |
| 107 | + } |
| 108 | + |
| 109 | + @Contract("null, _ -> null; !null, _ -> !null") |
| 110 | + @Nullable |
| 111 | + @Override |
| 112 | + public <X> ObjectBigIntId<?> wrap( |
| 113 | + @Nullable final X value, |
| 114 | + @Nullable final WrapperOptions options |
| 115 | + ) |
| 116 | + { |
| 117 | + if (value == null) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return wrapBigIntToIdentifier(inner.wrap(value, options)); |
| 122 | + } |
| 123 | + |
| 124 | + @Nullable |
| 125 | + @Override |
| 126 | + public ObjectBigIntId<?> fromString(@Nullable final CharSequence string) |
| 127 | + { |
| 128 | + return (string == null) ? null : wrapBigIntToIdentifier(Long.parseLong(string.toString())); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public MutabilityPlan<ObjectBigIntId<?>> getMutabilityPlan() |
| 133 | + { |
| 134 | + return ImmutableMutabilityPlan.instance(); |
| 135 | + } |
| 136 | + |
| 137 | + private ObjectBigIntId<?> wrapBigIntToIdentifier(final long id) |
| 138 | + { |
| 139 | + return ObjectBigIntIdTypeUtils.wrapBigIntToIdentifier( |
| 140 | + id, |
| 141 | + Objects.requireNonNull(constructor, "constructor was not yet initialized") |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String toString() |
| 147 | + { |
| 148 | + return "object-bigint-id(%s)".formatted(identifierClass != null ? identifierClass.getName() : "???"); |
| 149 | + } |
| 150 | + |
| 151 | + @SuppressWarnings("unused") |
| 152 | + private void writeObject(final ObjectOutputStream stream) |
| 153 | + { |
| 154 | + throw new UnsupportedOperationException("Serialization not supported"); |
| 155 | + } |
| 156 | + |
| 157 | + @SuppressWarnings("unused") |
| 158 | + private void readObject(final ObjectInputStream stream) |
| 159 | + { |
| 160 | + throw new UnsupportedOperationException("Serialization not supported"); |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments