Skip to content

Commit 6c573e9

Browse files
committed
clean up code for conversions between IntegralDataTypeHolders
1 parent 554ce4f commit 6c573e9

File tree

2 files changed

+87
-74
lines changed

2 files changed

+87
-74
lines changed

hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java

Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -83,56 +83,6 @@ else if ( integralType == BigDecimal.class ) {
8383
}
8484
}
8585

86-
public static long extractLong(IntegralDataTypeHolder holder) {
87-
if ( holder.getClass() == BasicHolder.class ) {
88-
( (BasicHolder) holder ).checkInitialized();
89-
return ( (BasicHolder) holder ).value;
90-
}
91-
else if ( holder.getClass() == BigIntegerHolder.class ) {
92-
( (BigIntegerHolder) holder ).checkInitialized();
93-
return ( (BigIntegerHolder) holder ).value.longValue();
94-
}
95-
else if ( holder.getClass() == BigDecimalHolder.class ) {
96-
( (BigDecimalHolder) holder ).checkInitialized();
97-
return ( (BigDecimalHolder) holder ).value.longValue();
98-
}
99-
throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" );
100-
}
101-
102-
public static BigInteger extractBigInteger(IntegralDataTypeHolder holder) {
103-
if ( holder.getClass() == BasicHolder.class ) {
104-
( (BasicHolder) holder ).checkInitialized();
105-
return BigInteger.valueOf( ( (BasicHolder) holder ).value );
106-
}
107-
else if ( holder.getClass() == BigIntegerHolder.class ) {
108-
( (BigIntegerHolder) holder ).checkInitialized();
109-
return ( (BigIntegerHolder) holder ).value;
110-
}
111-
else if ( holder.getClass() == BigDecimalHolder.class ) {
112-
( (BigDecimalHolder) holder ).checkInitialized();
113-
// scale should already be set...
114-
return ( (BigDecimalHolder) holder ).value.toBigInteger();
115-
}
116-
throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" );
117-
}
118-
119-
public static BigDecimal extractBigDecimal(IntegralDataTypeHolder holder) {
120-
if ( holder.getClass() == BasicHolder.class ) {
121-
( (BasicHolder) holder ).checkInitialized();
122-
return BigDecimal.valueOf( ( (BasicHolder) holder ).value );
123-
}
124-
else if ( holder.getClass() == BigIntegerHolder.class ) {
125-
( (BigIntegerHolder) holder ).checkInitialized();
126-
return new BigDecimal( ( (BigIntegerHolder) holder ).value );
127-
}
128-
else if ( holder.getClass() == BigDecimalHolder.class ) {
129-
( (BigDecimalHolder) holder ).checkInitialized();
130-
// scale should already be set...
131-
return ( (BigDecimalHolder) holder ).value;
132-
}
133-
throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" );
134-
}
135-
13686
public static Object getForeignId(
13787
String entityName, String propertyName, SharedSessionContractImplementor sessionImplementor, Object object) {
13888
final EntityPersister entityDescriptor =
@@ -261,7 +211,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) {
261211
}
262212

263213
public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) {
264-
return multiplyBy( extractLong( factor ) );
214+
return multiplyBy( factor.toLong() );
265215
}
266216

267217
public IntegralDataTypeHolder multiplyBy(long factor) {
@@ -271,7 +221,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) {
271221
}
272222

273223
public boolean eq(IntegralDataTypeHolder other) {
274-
return eq( extractLong( other ) );
224+
return eq( other.toLong() );
275225
}
276226

277227
public boolean eq(long value) {
@@ -280,7 +230,7 @@ public boolean eq(long value) {
280230
}
281231

282232
public boolean lt(IntegralDataTypeHolder other) {
283-
return lt( extractLong( other ) );
233+
return lt( other.toLong() );
284234
}
285235

286236
public boolean lt(long value) {
@@ -289,7 +239,7 @@ public boolean lt(long value) {
289239
}
290240

291241
public boolean gt(IntegralDataTypeHolder other) {
292-
return gt( extractLong( other ) );
242+
return gt( other.toLong() );
293243
}
294244

295245
public boolean gt(long value) {
@@ -329,6 +279,24 @@ public Number makeValueThenAdd(long addend) {
329279
return result;
330280
}
331281

282+
@Override
283+
public long toLong() {
284+
checkInitialized();
285+
return value;
286+
}
287+
288+
@Override
289+
public BigDecimal toBigDecimal() {
290+
checkInitialized();
291+
return BigDecimal.valueOf( value );
292+
}
293+
294+
@Override
295+
public BigInteger toBigInteger() {
296+
checkInitialized();
297+
return BigInteger.valueOf( value );
298+
}
299+
332300
@Override
333301
public String toString() {
334302
return "BasicHolder[" + exactType.getName() + "[" + value + "]]";
@@ -339,12 +307,9 @@ public boolean equals(Object o) {
339307
if ( this == o ) {
340308
return true;
341309
}
342-
if ( o == null || getClass() != o.getClass() ) {
310+
if ( !(o instanceof BasicHolder that) ) {
343311
return false;
344312
}
345-
346-
BasicHolder that = (BasicHolder) o;
347-
348313
return value == that.value;
349314
}
350315

@@ -407,7 +372,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) {
407372

408373
public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) {
409374
checkInitialized();
410-
value = value.multiply( extractBigInteger( factor ) );
375+
value = value.multiply( factor.toBigInteger() );
411376
return this;
412377
}
413378

@@ -419,7 +384,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) {
419384

420385
public boolean eq(IntegralDataTypeHolder other) {
421386
checkInitialized();
422-
return value.compareTo( extractBigInteger( other ) ) == 0;
387+
return value.compareTo( other.toBigInteger() ) == 0;
423388
}
424389

425390
public boolean eq(long value) {
@@ -429,7 +394,7 @@ public boolean eq(long value) {
429394

430395
public boolean lt(IntegralDataTypeHolder other) {
431396
checkInitialized();
432-
return value.compareTo( extractBigInteger( other ) ) < 0;
397+
return value.compareTo( other.toBigInteger() ) < 0;
433398
}
434399

435400
public boolean lt(long value) {
@@ -439,7 +404,7 @@ public boolean lt(long value) {
439404

440405
public boolean gt(IntegralDataTypeHolder other) {
441406
checkInitialized();
442-
return value.compareTo( extractBigInteger( other ) ) > 0;
407+
return value.compareTo( other.toBigInteger() ) > 0;
443408
}
444409

445410
public boolean gt(long value) {
@@ -470,6 +435,24 @@ public Number makeValueThenAdd(long addend) {
470435
return result;
471436
}
472437

438+
@Override
439+
public long toLong() {
440+
checkInitialized();
441+
return value.longValue();
442+
}
443+
444+
@Override
445+
public BigInteger toBigInteger() {
446+
checkInitialized();
447+
return value;
448+
}
449+
450+
@Override
451+
public BigDecimal toBigDecimal() {
452+
checkInitialized();
453+
return new BigDecimal( value );
454+
}
455+
473456
@Override
474457
public String toString() {
475458
return "BigIntegerHolder[" + value + "]";
@@ -480,12 +463,9 @@ public boolean equals(Object o) {
480463
if ( this == o ) {
481464
return true;
482465
}
483-
if ( o == null || getClass() != o.getClass() ) {
466+
if ( !(o instanceof BigIntegerHolder that) ) {
484467
return false;
485468
}
486-
487-
BigIntegerHolder that = (BigIntegerHolder) o;
488-
489469
return Objects.equals( value, that.value );
490470
}
491471

@@ -548,7 +528,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) {
548528

549529
public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) {
550530
checkInitialized();
551-
value = value.multiply( extractBigDecimal( factor ) );
531+
value = value.multiply( factor.toBigDecimal() );
552532
return this;
553533
}
554534

@@ -560,7 +540,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) {
560540

561541
public boolean eq(IntegralDataTypeHolder other) {
562542
checkInitialized();
563-
return value.compareTo( extractBigDecimal( other ) ) == 0;
543+
return value.compareTo( other.toBigDecimal() ) == 0;
564544
}
565545

566546
public boolean eq(long value) {
@@ -570,7 +550,7 @@ public boolean eq(long value) {
570550

571551
public boolean lt(IntegralDataTypeHolder other) {
572552
checkInitialized();
573-
return value.compareTo( extractBigDecimal( other ) ) < 0;
553+
return value.compareTo( other.toBigDecimal() ) < 0;
574554
}
575555

576556
public boolean lt(long value) {
@@ -580,7 +560,7 @@ public boolean lt(long value) {
580560

581561
public boolean gt(IntegralDataTypeHolder other) {
582562
checkInitialized();
583-
return value.compareTo( extractBigDecimal( other ) ) > 0;
563+
return value.compareTo( other.toBigDecimal() ) > 0;
584564
}
585565

586566
public boolean gt(long value) {
@@ -611,6 +591,24 @@ public Number makeValueThenAdd(long addend) {
611591
return result;
612592
}
613593

594+
@Override
595+
public long toLong() {
596+
checkInitialized();
597+
return value.longValue();
598+
}
599+
600+
@Override
601+
public BigInteger toBigInteger() {
602+
checkInitialized();
603+
return value.toBigInteger();
604+
}
605+
606+
@Override
607+
public BigDecimal toBigDecimal() {
608+
checkInitialized();
609+
return value;
610+
}
611+
614612
@Override
615613
public String toString() {
616614
return "BigDecimalHolder[" + value + "]";
@@ -621,12 +619,9 @@ public boolean equals(Object o) {
621619
if ( this == o ) {
622620
return true;
623621
}
624-
if ( o == null || getClass() != o.getClass() ) {
622+
if ( !(o instanceof BigDecimalHolder that) ) {
625623
return false;
626624
}
627-
628-
BigDecimalHolder that = (BigDecimalHolder) o;
629-
630625
return Objects.equals( this.value, that.value );
631626
}
632627

hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
55
package org.hibernate.id;
6+
67
import java.io.Serializable;
8+
import java.math.BigDecimal;
9+
import java.math.BigInteger;
710
import java.sql.PreparedStatement;
811
import java.sql.ResultSet;
912
import java.sql.SQLException;
@@ -183,4 +186,19 @@ public interface IntegralDataTypeHolder extends Serializable {
183186
* @return The pre-incremented internal value
184187
*/
185188
Number makeValueThenAdd(long addend);
189+
190+
/**
191+
* Convert the internal value to {@code long}.
192+
*/
193+
long toLong();
194+
195+
/**
196+
* Convert the internal value to {@link BigInteger}.
197+
*/
198+
BigInteger toBigInteger();
199+
200+
/**
201+
* Convert the internal value to {@link BigDecimal}.
202+
*/
203+
BigDecimal toBigDecimal();
186204
}

0 commit comments

Comments
 (0)