Skip to content

Commit 26988dd

Browse files
committed
start using flow typing in the JavaTypes
- but definitely not done with this squash Signed-off-by: Gavin King <[email protected]>
1 parent 65bc730 commit 26988dd

22 files changed

+204
-231
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigDecimalJavaType.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ public <X> BigDecimal wrap(X value, WrapperOptions options) {
8282
if ( value == null ) {
8383
return null;
8484
}
85-
if ( value instanceof BigDecimal ) {
86-
return (BigDecimal) value;
85+
if ( value instanceof BigDecimal bigDecimal ) {
86+
return bigDecimal;
8787
}
88-
if ( value instanceof BigInteger ) {
89-
return new BigDecimal( (BigInteger) value );
88+
if ( value instanceof BigInteger bigInteger ) {
89+
return new BigDecimal( bigInteger );
9090
}
91-
if ( value instanceof Number ) {
92-
return BigDecimal.valueOf( ( (Number) value ).doubleValue() );
91+
if ( value instanceof Number number ) {
92+
return BigDecimal.valueOf( number.doubleValue() );
9393
}
94-
if ( value instanceof String ) {
95-
return new BigDecimal( (String) value );
94+
if ( value instanceof String string ) {
95+
return new BigDecimal( string );
9696
}
9797
throw unknownWrap( value.getClass() );
9898
}
@@ -125,17 +125,17 @@ public <X> BigDecimal coerce(X value, CoercionContext coercionContext) {
125125
return null;
126126
}
127127

128-
if ( value instanceof BigDecimal ) {
129-
return (BigDecimal) value;
128+
if ( value instanceof BigDecimal bigDecimal ) {
129+
return bigDecimal;
130130
}
131131

132-
if ( value instanceof Number ) {
133-
return BigDecimal.valueOf( ( (Number) value ).doubleValue() );
132+
if ( value instanceof Number number ) {
133+
return BigDecimal.valueOf( number.doubleValue() );
134134
}
135135

136-
if ( value instanceof String ) {
136+
if ( value instanceof String string ) {
137137
return CoercionHelper.coerceWrappingError(
138-
() -> BigDecimal.valueOf( Double.parseDouble( (String) value ) )
138+
() -> BigDecimal.valueOf( Double.parseDouble( string ) )
139139
);
140140
}
141141

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BigIntegerJavaType.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ public <X> BigInteger wrap(X value, WrapperOptions options) {
8080
if ( value == null ) {
8181
return null;
8282
}
83-
if ( value instanceof BigInteger ) {
84-
return (BigInteger) value;
83+
if ( value instanceof BigInteger bigInteger ) {
84+
return bigInteger;
8585
}
86-
if ( value instanceof BigDecimal ) {
87-
return ( (BigDecimal) value ).toBigIntegerExact();
86+
if ( value instanceof BigDecimal bigDecimal ) {
87+
return bigDecimal.toBigIntegerExact();
8888
}
89-
if ( value instanceof Number ) {
90-
return BigInteger.valueOf( ( (Number) value ).longValue() );
89+
if ( value instanceof Number number ) {
90+
return BigInteger.valueOf( number.longValue() );
9191
}
92-
if ( value instanceof String ) {
93-
return new BigInteger( (String) value );
92+
if ( value instanceof String string ) {
93+
return new BigInteger( string );
9494
}
9595
throw unknownWrap( value.getClass() );
9696
}
@@ -127,41 +127,41 @@ public <X> BigInteger coerce(X value, CoercionContext coercionContext) {
127127
return null;
128128
}
129129

130-
if ( value instanceof BigInteger ) {
131-
return (BigInteger) value;
130+
if ( value instanceof BigInteger bigInteger ) {
131+
return bigInteger;
132132
}
133133

134-
if ( value instanceof Byte ) {
135-
return BigInteger.valueOf( ( (Byte) value ) );
134+
if ( value instanceof Byte byteValue ) {
135+
return BigInteger.valueOf( byteValue );
136136
}
137137

138-
if ( value instanceof Short ) {
139-
return BigInteger.valueOf( ( (Short) value ) );
138+
if ( value instanceof Short shortValue ) {
139+
return BigInteger.valueOf( shortValue );
140140
}
141141

142-
if ( value instanceof Integer ) {
143-
return BigInteger.valueOf( ( (Integer) value ) );
142+
if ( value instanceof Integer integerValue ) {
143+
return BigInteger.valueOf( integerValue );
144144
}
145145

146-
if ( value instanceof Long ) {
147-
return BigInteger.valueOf( ( (Long) value ) );
146+
if ( value instanceof Long longValue ) {
147+
return BigInteger.valueOf( longValue );
148148
}
149149

150-
if ( value instanceof Double ) {
151-
return CoercionHelper.toBigInteger( (Double) value );
150+
if ( value instanceof Double doubleValue ) {
151+
return CoercionHelper.toBigInteger( doubleValue );
152152
}
153153

154-
if ( value instanceof Float ) {
155-
return CoercionHelper.toBigInteger( (Float) value );
154+
if ( value instanceof Float floatValue ) {
155+
return CoercionHelper.toBigInteger( floatValue );
156156
}
157157

158-
if ( value instanceof BigDecimal ) {
159-
return CoercionHelper.toBigInteger( (BigDecimal) value );
158+
if ( value instanceof BigDecimal bigDecimal ) {
159+
return CoercionHelper.toBigInteger( bigDecimal );
160160
}
161161

162-
if ( value instanceof String ) {
162+
if ( value instanceof String string ) {
163163
return CoercionHelper.coerceWrappingError(
164-
() -> BigInteger.valueOf( Long.parseLong( (String) value ) )
164+
() -> BigInteger.valueOf( Long.parseLong( string ) )
165165
);
166166
}
167167

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BlobJavaType.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ public <X> X unwrap(Blob value, Class<X> type, WrapperOptions options) {
110110

111111
try {
112112
if ( BinaryStream.class.isAssignableFrom( type ) ) {
113-
if (value instanceof BlobImplementer) {
113+
if (value instanceof BlobImplementer blobImplementer) {
114114
// if the incoming Blob is a wrapper, just pass along its BinaryStream
115-
return (X) ( (BlobImplementer) value ).getUnderlyingStream();
115+
return (X) blobImplementer.getUnderlyingStream();
116116
}
117117
else {
118118
// otherwise we need to build a BinaryStream...
119119
return (X) new BinaryStreamImpl( DataHelper.extractBytes( value.getBinaryStream() ) );
120120
}
121121
}
122122
else if ( byte[].class.isAssignableFrom( type )) {
123-
if (value instanceof BlobImplementer) {
123+
if (value instanceof BlobImplementer blobImplementer) {
124124
// if the incoming Blob is a wrapper, just grab the bytes from its BinaryStream
125-
return (X) ( (BlobImplementer) value ).getUnderlyingStream().getBytes();
125+
return (X) blobImplementer.getUnderlyingStream().getBytes();
126126
}
127127
else {
128128
// otherwise extract the bytes from the stream manually
@@ -141,8 +141,8 @@ else if ( Blob.class.isAssignableFrom( type ) ) {
141141
}
142142

143143
private Blob getOrCreateBlob(Blob value, WrapperOptions options) throws SQLException {
144-
if ( value instanceof WrappedBlob ) {
145-
value = ( (WrappedBlob) value ).getWrappedBlob();
144+
if ( value instanceof WrappedBlob wrappedBlob ) {
145+
value = wrappedBlob.getWrappedBlob();
146146
}
147147
if ( options.getDialect().useConnectionToCreateLob() ) {
148148
if ( value.length() == 0 ) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanJavaType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ public <X> Boolean wrap(X value, WrapperOptions options) {
108108
if ( value == null ) {
109109
return null;
110110
}
111-
if (value instanceof Boolean) {
112-
return (Boolean) value;
111+
if (value instanceof Boolean booleanValue) {
112+
return booleanValue;
113113
}
114-
if (value instanceof Number) {
115-
final int intValue = ( (Number) value ).intValue();
114+
if (value instanceof Number number) {
115+
final int intValue = number.intValue();
116116
return intValue != 0;
117117
}
118-
if (value instanceof Character) {
119-
return isTrue( (Character) value );
118+
if (value instanceof Character character) {
119+
return isTrue( character );
120120
}
121-
if (value instanceof String) {
122-
return isTrue( (String) value );
121+
if (value instanceof String string) {
122+
return isTrue( string );
123123
}
124124
throw unknownWrap( value.getClass() );
125125
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public <X> Character wrap(X value, WrapperOptions options) {
6464
if ( value == null ) {
6565
return null;
6666
}
67-
if (value instanceof Character) {
68-
return (Character) value;
67+
if (value instanceof Character character) {
68+
return character;
6969
}
7070
if ( value instanceof String ) {
7171
if ( value.equals( "" ) ) {
@@ -74,9 +74,8 @@ public <X> Character wrap(X value, WrapperOptions options) {
7474
final String str = (String) value;
7575
return str.charAt( 0 );
7676
}
77-
if (value instanceof Number) {
78-
final Number nbr = (Number) value;
79-
return (char) nbr.shortValue();
77+
if (value instanceof Number number) {
78+
return (char) number.shortValue();
8079
}
8180
throw unknownWrap( value.getClass() );
8281
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClassJavaType.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@
1313
*
1414
* @author Steve Ebersole
1515
*/
16-
public class ClassJavaType extends AbstractClassJavaType<Class> {
16+
public class ClassJavaType extends AbstractClassJavaType<Class<?>> {
1717
public static final ClassJavaType INSTANCE = new ClassJavaType();
1818

19+
@SuppressWarnings({"unchecked", "rawtypes"} )
1920
public ClassJavaType() {
20-
super( Class.class );
21+
super( (Class) Class.class );
2122
}
2223

2324
@Override
2425
public boolean useObjectEqualsHashCode() {
2526
return true;
2627
}
2728

28-
public String toString(Class value) {
29+
public String toString(Class<?> value) {
2930
return value.getName();
3031
}
3132

32-
public Class fromString(CharSequence string) {
33+
public Class<?> fromString(CharSequence string) {
3334
if ( string == null ) {
3435
return null;
3536
}
@@ -43,7 +44,7 @@ public Class fromString(CharSequence string) {
4344
}
4445

4546
@SuppressWarnings("unchecked")
46-
public <X> X unwrap(Class value, Class<X> type, WrapperOptions options) {
47+
public <X> X unwrap(Class<?> value, Class<X> type, WrapperOptions options) {
4748
if ( value == null ) {
4849
return null;
4950
}
@@ -56,12 +57,12 @@ public <X> X unwrap(Class value, Class<X> type, WrapperOptions options) {
5657
throw unknownUnwrap( type );
5758
}
5859

59-
public <X> Class wrap(X value, WrapperOptions options) {
60+
public <X> Class<?> wrap(X value, WrapperOptions options) {
6061
if ( value == null ) {
6162
return null;
6263
}
6364
if (value instanceof Class) {
64-
return (Class) value;
65+
return (Class<?>) value;
6566
}
6667
if (value instanceof CharSequence) {
6768
return fromString( (CharSequence) value );

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ClobJavaType.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@ public <X> X unwrap(final Clob value, Class<X> type, WrapperOptions options) {
8383

8484
try {
8585
if ( CharacterStream.class.isAssignableFrom( type ) ) {
86-
if (value instanceof ClobImplementer) {
86+
if (value instanceof ClobImplementer clobImplementer) {
8787
// if the incoming Clob is a wrapper, just pass along its CharacterStream
88-
return (X) ( (ClobImplementer) value ).getUnderlyingStream();
88+
return (X) clobImplementer.getUnderlyingStream();
8989
}
9090
else {
9191
// otherwise we need to build a CharacterStream...
9292
return (X) new CharacterStreamImpl( DataHelper.extractString( value.getCharacterStream() ) );
9393
}
9494
}
9595
else if ( String.class.isAssignableFrom( type ) ) {
96-
if (value instanceof ClobImplementer) {
96+
if (value instanceof ClobImplementer clobImplementer) {
9797
// if the incoming Clob is a wrapper, just grab the bytes from its BinaryStream
98-
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString();
98+
return (X) clobImplementer.getUnderlyingStream().asString();
9999
}
100100
else {
101101
// otherwise extract the bytes from the stream manually
@@ -106,9 +106,9 @@ else if ( Clob.class.isAssignableFrom( type ) ) {
106106
return (X) getOrCreateClob( value, options );
107107
}
108108
else if ( String.class.isAssignableFrom( type ) ) {
109-
if (value instanceof ClobImplementer) {
109+
if (value instanceof ClobImplementer clobImplementer) {
110110
// if the incoming Clob is a wrapper, just get the underlying String.
111-
return (X) ( (ClobImplementer) value ).getUnderlyingStream().asString();
111+
return (X) clobImplementer.getUnderlyingStream().asString();
112112
}
113113
else {
114114
// otherwise we need to extract the String.
@@ -124,8 +124,8 @@ else if ( String.class.isAssignableFrom( type ) ) {
124124
}
125125

126126
private Clob getOrCreateClob(Clob value, WrapperOptions options) throws SQLException {
127-
if ( value instanceof WrappedClob ) {
128-
value = ( (WrappedClob) value ).getWrappedClob();
127+
if ( value instanceof WrappedClob wrappedClob ) {
128+
value = wrappedClob.getWrappedClob();
129129
}
130130
if ( options.getDialect().useConnectionToCreateLob() ) {
131131
if ( value.length() == 0 ) {

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CurrencyJavaType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public <X> Currency wrap(X value, WrapperOptions options) {
5656
if ( value == null ) {
5757
return null;
5858
}
59-
if ( value instanceof Currency ) {
60-
return (Currency) value;
59+
if ( value instanceof Currency currency ) {
60+
return currency;
6161
}
62-
if (value instanceof String) {
63-
return Currency.getInstance( (String) value );
62+
if (value instanceof String string) {
63+
return Currency.getInstance( string );
6464
}
6565
throw unknownWrap( value.getClass() );
6666
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DurationJavaType.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ public <X> Duration wrap(X value, WrapperOptions options) {
112112
return null;
113113
}
114114

115-
if (value instanceof Duration) {
116-
return (Duration) value;
115+
if (value instanceof Duration duration) {
116+
return duration;
117117
}
118118

119-
if (value instanceof BigDecimal) {
120-
final BigDecimal decimal = (BigDecimal) value;
119+
if ( value instanceof BigDecimal decimal ) {
121120
final BigDecimal[] secondsAndNanos = decimal.divideAndRemainder( BILLION );
122121
return Duration.ofSeconds(
123122
secondsAndNanos[0].longValueExact(),
@@ -129,17 +128,17 @@ public <X> Duration wrap(X value, WrapperOptions options) {
129128
);
130129
}
131130

132-
if (value instanceof Double) {
131+
if (value instanceof Double doubleValue) {
133132
// PostgreSQL returns a Double for datediff(epoch)
134-
return Duration.ofNanos( ( (Double) value ).longValue() );
133+
return Duration.ofNanos( doubleValue.longValue() );
135134
}
136135

137-
if (value instanceof Long) {
138-
return Duration.ofNanos( (Long) value );
136+
if (value instanceof Long longValue) {
137+
return Duration.ofNanos( longValue );
139138
}
140139

141-
if (value instanceof String) {
142-
return Duration.parse( (String) value );
140+
if (value instanceof String string) {
141+
return Duration.parse( string );
143142
}
144143

145144
throw unknownWrap( value.getClass() );

0 commit comments

Comments
 (0)