Skip to content

Commit ed6fb2c

Browse files
committed
Replace bulky instanceof casts&assertions with a new more compact MongoAssertions.assertInstanceOf method
Addresses #95 (comment).
1 parent dbb00b9 commit ed6fb2c

File tree

5 files changed

+32
-48
lines changed

5 files changed

+32
-48
lines changed

src/main/java/com/mongodb/hibernate/internal/MongoAssertions.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private MongoAssertions() {}
3333
*/
3434
public static <T> T assertNotNull(@Nullable T value) throws AssertionError {
3535
if (value == null) {
36-
throw new AssertionError();
36+
throw fail();
3737
}
3838
return value;
3939
}
@@ -69,7 +69,7 @@ public static AssertionError fail() throws AssertionError {
6969
*/
7070
public static void assertNull(@Nullable Object value) throws AssertionError {
7171
if (value != null) {
72-
throw new AssertionError();
72+
throw fail();
7373
}
7474
}
7575

@@ -82,7 +82,7 @@ public static void assertNull(@Nullable Object value) throws AssertionError {
8282
*/
8383
public static boolean assertTrue(boolean value) throws AssertionError {
8484
if (!value) {
85-
throw new AssertionError();
85+
throw fail();
8686
}
8787
return true;
8888
}
@@ -96,8 +96,15 @@ public static boolean assertTrue(boolean value) throws AssertionError {
9696
*/
9797
public static boolean assertFalse(boolean value) throws AssertionError {
9898
if (value) {
99-
throw new AssertionError();
99+
throw fail();
100100
}
101101
return false;
102102
}
103+
104+
public static <T> T assertInstanceOf(@Nullable Object value, Class<? extends T> type) {
105+
if (!type.isInstance(value)) {
106+
throw fail();
107+
}
108+
return type.cast(value);
109+
}
103110
}

src/main/java/com/mongodb/hibernate/internal/cfg/MongoConfigurationBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ private static final class ConfigPropertiesParser {
7373
var jdbcUrl = configurationValues.get(JAKARTA_JDBC_URL);
7474
if (jdbcUrl == null) {
7575
return null;
76-
}
77-
if (jdbcUrl instanceof String jdbcUrlText) {
76+
} else if (jdbcUrl instanceof String jdbcUrlText) {
7877
return parseConnectionString(JAKARTA_JDBC_URL, jdbcUrlText);
7978
} else if (jdbcUrl instanceof ConnectionString jdbcUrlConnectionString) {
8079
return jdbcUrlConnectionString;
81-
} else {
82-
throw MongoConfigurationBuilder.ConfigPropertiesParser.Exceptions.unsupportedType(
83-
JAKARTA_JDBC_URL, jdbcUrl, String.class, ConnectionString.class);
8480
}
81+
throw MongoConfigurationBuilder.ConfigPropertiesParser.Exceptions.unsupportedType(
82+
JAKARTA_JDBC_URL, jdbcUrl, String.class, ConnectionString.class);
8583
}
8684

8785
private static ConnectionString parseConnectionString(String propertyName, String propertyValue) {

src/main/java/com/mongodb/hibernate/internal/type/MongoStructJdbcType.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.hibernate.internal.type;
1818

1919
import static com.mongodb.hibernate.internal.MongoAssertions.assertFalse;
20+
import static com.mongodb.hibernate.internal.MongoAssertions.assertInstanceOf;
2021
import static com.mongodb.hibernate.internal.MongoAssertions.assertNotNull;
2122
import static com.mongodb.hibernate.internal.MongoAssertions.assertTrue;
2223
import static com.mongodb.hibernate.internal.MongoAssertions.fail;
@@ -137,9 +138,7 @@ private BsonValue createBsonValue(@Nullable Object domainValue, WrapperOptions o
137138
var jdbcMapping = jdbcValueSelectable.getJdbcMapping();
138139
var jdbcTypeCode = jdbcMapping.getJdbcType().getJdbcTypeCode();
139140
if (jdbcTypeCode == getJdbcTypeCode()) {
140-
if (!(jdbcMapping.getJdbcValueBinder() instanceof Binder<?> structValueBinder)) {
141-
throw fail();
142-
}
141+
var structValueBinder = assertInstanceOf(jdbcMapping.getJdbcValueBinder(), Binder.class);
143142
bsonValue = structValueBinder.getJdbcType().createBsonValue(value, options);
144143
} else if (jdbcTypeCode == MongoArrayJdbcType.JDBC_TYPE.getVendorTypeNumber()) {
145144
@SuppressWarnings("unchecked")
@@ -166,9 +165,7 @@ private BsonValue createBsonValue(@Nullable Object domainValue, WrapperOptions o
166165
throw new FeatureNotSupportedException(
167166
"TODO-HIBERNATE-48 https://jira.mongodb.org/browse/HIBERNATE-48 return null");
168167
}
169-
if (!(rawJdbcValue instanceof BsonDocument bsonDocument)) {
170-
throw fail();
171-
}
168+
var bsonDocument = assertInstanceOf(rawJdbcValue, BsonDocument.class);
172169
var embeddableMappingType = getEmbeddableMappingType();
173170
var result = new Object[bsonDocument.size()];
174171
var elementIdx = 0;
@@ -181,17 +178,12 @@ private BsonValue createBsonValue(@Nullable Object domainValue, WrapperOptions o
181178
throw new FeatureNotSupportedException(
182179
"TODO-HIBERNATE-48 https://jira.mongodb.org/browse/HIBERNATE-48 domainValue = null");
183180
} else if (jdbcTypeCode == getJdbcTypeCode()) {
184-
if (!(jdbcMapping.getJdbcValueExtractor() instanceof Extractor<?> structValueExtractor)) {
185-
throw fail();
186-
}
181+
var structValueExtractor = assertInstanceOf(jdbcMapping.getJdbcValueExtractor(), Extractor.class);
187182
domainValue = structValueExtractor.getJdbcType().extractJdbcValues(value, options);
188183
} else if (jdbcTypeCode == MongoArrayJdbcType.JDBC_TYPE.getVendorTypeNumber()) {
189-
if (!(jdbcMapping.getJdbcType() instanceof MongoArrayJdbcType arrayJdbcType)) {
190-
throw fail();
191-
}
192-
if (!(jdbcMapping.getJdbcValueExtractor() instanceof BasicExtractor<?> jdbcValueExtractor)) {
193-
throw fail();
194-
}
184+
var arrayJdbcType = assertInstanceOf(jdbcMapping.getJdbcType(), MongoArrayJdbcType.class);
185+
BasicExtractor<?> jdbcValueExtractor =
186+
assertInstanceOf(jdbcMapping.getJdbcValueExtractor(), BasicExtractor.class);
195187
domainValue = arrayJdbcType.getArray(jdbcValueExtractor, toArrayDomainValue(value), options);
196188
} else {
197189
domainValue =
@@ -223,10 +215,7 @@ private final class Binder<X> extends BasicBinder<X> {
223215

224216
@Override
225217
public MongoStructJdbcType getJdbcType() {
226-
if (!(super.getJdbcType() instanceof MongoStructJdbcType structJdbcType)) {
227-
throw fail();
228-
}
229-
return structJdbcType;
218+
return assertInstanceOf(super.getJdbcType(), MongoStructJdbcType.class);
230219
}
231220

232221
@Override
@@ -256,10 +245,7 @@ private final class Extractor<X> extends BasicExtractor<X> {
256245

257246
@Override
258247
public MongoStructJdbcType getJdbcType() {
259-
if (!(super.getJdbcType() instanceof MongoStructJdbcType structJdbcType)) {
260-
throw fail();
261-
}
262-
return structJdbcType;
248+
return assertInstanceOf(super.getJdbcType(), MongoStructJdbcType.class);
263249
}
264250

265251
@Override

src/main/java/com/mongodb/hibernate/internal/type/ValueConversions.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ public static BsonValue toBsonValue(@Nullable Object value) throws SQLFeatureNot
8282
return toBsonValue(v);
8383
} else if (value instanceof Object[] v) {
8484
return arrayToBsonValue(v);
85-
} else {
86-
throw new SQLFeatureNotSupportedException(format(
87-
"Value [%s] of type [%s] is not supported",
88-
value, value.getClass().getTypeName()));
8985
}
86+
throw new SQLFeatureNotSupportedException(format(
87+
"Value [%s] of type [%s] is not supported",
88+
value, value.getClass().getTypeName()));
9089
}
9190

9291
public static BsonBoolean toBsonValue(boolean value) {
@@ -199,11 +198,10 @@ private static BsonArray arrayToBsonValue(Object value) throws SQLFeatureNotSupp
199198
return toDomainValue(v);
200199
} else if (value instanceof BsonArray v && domainType.isArray()) {
201200
return toDomainValue(v, assertNotNull(domainType.getComponentType()));
202-
} else {
203-
throw new SQLFeatureNotSupportedException(format(
204-
"Value [%s] of type [%s] is not supported for the domain type [%s]",
205-
value, assertNotNull(value).getClass().getTypeName(), domainType));
206201
}
202+
throw new SQLFeatureNotSupportedException(format(
203+
"Value [%s] of type [%s] is not supported for the domain type [%s]",
204+
value, assertNotNull(value).getClass().getTypeName(), domainType));
207205
}
208206

209207
public static boolean isNull(@Nullable Object value) {

src/main/java/com/mongodb/hibernate/jdbc/MongoPreparedStatement.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.hibernate.jdbc;
1818

19+
import static com.mongodb.hibernate.internal.MongoAssertions.assertInstanceOf;
1920
import static com.mongodb.hibernate.internal.MongoAssertions.fail;
2021
import static com.mongodb.hibernate.internal.type.ValueConversions.toBsonValue;
2122
import static java.lang.String.format;
@@ -187,15 +188,9 @@ public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQ
187188
checkParameterIndex(parameterIndex);
188189
BsonValue value;
189190
if (targetSqlType == ObjectIdJdbcType.MQL_TYPE.getVendorTypeNumber()) {
190-
if (!(x instanceof ObjectId v)) {
191-
throw fail();
192-
}
193-
value = toBsonValue(v);
191+
value = toBsonValue(assertInstanceOf(x, ObjectId.class));
194192
} else if (targetSqlType == MongoStructJdbcType.JDBC_TYPE.getVendorTypeNumber()) {
195-
if (!(x instanceof BsonDocument v)) {
196-
throw fail();
197-
}
198-
value = v;
193+
value = assertInstanceOf(x, BsonDocument.class);
199194
} else {
200195
throw new SQLFeatureNotSupportedException(format(
201196
"Parameter value [%s] of SQL type [%d] with index [%d] is not supported",

0 commit comments

Comments
 (0)