Skip to content

Commit cd800c2

Browse files
committed
simplification to CollectionJavaType
1 parent 69ed948 commit cd800c2

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/CollectionJavaType.java

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -51,49 +51,34 @@ public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
5151
return null;
5252
}
5353

54-
@Override
54+
@Override @SuppressWarnings({"unchecked", "rawtypes"})
5555
public JavaType<C> createJavaType(
5656
ParameterizedType parameterizedType,
5757
TypeConfiguration typeConfiguration) {
58-
final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
59-
final var javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
60-
switch ( semantics.getCollectionClassification() ) {
61-
case ARRAY:
62-
//noinspection unchecked
63-
return (JavaType<C>) new ArrayJavaType<>(
64-
javaTypeRegistry.resolveDescriptor(
65-
( (Class<?>) parameterizedType.getRawType() ).getComponentType()
66-
)
67-
);
68-
case BAG:
69-
case ID_BAG:
70-
case LIST:
71-
case SET:
72-
case SORTED_SET:
73-
case ORDERED_SET:
74-
//noinspection unchecked,rawtypes
75-
return new BasicCollectionJavaType(
76-
parameterizedType,
77-
javaTypeRegistry.resolveDescriptor( actualTypeArguments[actualTypeArguments.length - 1] ),
78-
semantics
79-
);
80-
81-
}
82-
// Construct a basic java type that knows its parametrization
83-
//noinspection unchecked,rawtypes
84-
return new UnknownBasicJavaType(
85-
parameterizedType,
86-
new MapMutabilityPlan<>(
87-
(MapSemantics<Map<Object, Object>, Object, Object>) semantics,
88-
javaTypeRegistry.resolveDescriptor( actualTypeArguments[0] ),
89-
javaTypeRegistry.resolveDescriptor( actualTypeArguments[actualTypeArguments.length - 1] )
90-
)
91-
);
92-
}
93-
94-
@Override
95-
public C fromString(CharSequence string) {
96-
throw new UnsupportedOperationException();
58+
final Type[] typeArguments = parameterizedType.getActualTypeArguments();
59+
final var registry = typeConfiguration.getJavaTypeRegistry();
60+
return switch ( semantics.getCollectionClassification() ) {
61+
case ARRAY -> {
62+
final var arrayClass = (Class<?>) parameterizedType.getRawType();
63+
yield (JavaType<C>) new ArrayJavaType<>( registry.resolveDescriptor( arrayClass.getComponentType() ) );
64+
}
65+
case BAG, ID_BAG, LIST, SET, SORTED_SET, ORDERED_SET ->
66+
new BasicCollectionJavaType(
67+
parameterizedType,
68+
registry.resolveDescriptor( typeArguments[typeArguments.length-1] ),
69+
semantics
70+
);
71+
case MAP, ORDERED_MAP, SORTED_MAP ->
72+
// Construct a basic java type that knows its parametrization
73+
new UnknownBasicJavaType(
74+
parameterizedType,
75+
new MapMutabilityPlan(
76+
(MapSemantics) semantics,
77+
registry.resolveDescriptor( typeArguments[0] ),
78+
registry.resolveDescriptor( typeArguments[typeArguments.length-1] )
79+
)
80+
);
81+
};
9782
}
9883

9984
@Override
@@ -111,16 +96,20 @@ public boolean areEqual(C one, C another) {
11196
if ( one == another ) {
11297
return true;
11398
}
114-
115-
if ( one instanceof PersistentCollection<?> pc ) {
116-
return pc.wasInitialized() && ( pc.isWrapper( another ) || pc.isDirectlyProvidedCollection( another ) );
99+
else if ( one instanceof PersistentCollection<?> collection ) {
100+
return wraps( collection, another );
117101
}
118-
119-
if ( another instanceof PersistentCollection<?> pc ) {
120-
return pc.wasInitialized() && ( pc.isWrapper( one ) || pc.isDirectlyProvidedCollection( one ) );
102+
else if ( another instanceof PersistentCollection<?> collection ) {
103+
return wraps( collection, one );
121104
}
105+
else {
106+
return Objects.equals( one, another );
107+
}
108+
}
122109

123-
return Objects.equals( one, another );
110+
private static <C> boolean wraps(PersistentCollection<?> collection, C other) {
111+
return collection.wasInitialized()
112+
&& collection.isWrapper( other );
124113
}
125114

126115
@Override
@@ -153,13 +142,14 @@ public C deepCopy(C value) {
153142
if ( value == null ) {
154143
return null;
155144
}
156-
final C copy = semantics.instantiateRaw( value.size(), null );
157-
158-
for ( var entry : value.entrySet() ) {
159-
copy.put( keyPlan.deepCopy( entry.getKey() ),
160-
valuePlan.deepCopy( entry.getValue() ) );
145+
else {
146+
final C copy = semantics.instantiateRaw( value.size(), null );
147+
for ( var entry : value.entrySet() ) {
148+
copy.put( keyPlan.deepCopy( entry.getKey() ),
149+
valuePlan.deepCopy( entry.getValue() ) );
150+
}
151+
return copy;
161152
}
162-
return copy;
163153
}
164154

165155
@Override

0 commit comments

Comments
 (0)