diff --git a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java index 3a8075fb531b..015dc6008e9d 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java @@ -83,56 +83,6 @@ else if ( integralType == BigDecimal.class ) { } } - public static long extractLong(IntegralDataTypeHolder holder) { - if ( holder.getClass() == BasicHolder.class ) { - ( (BasicHolder) holder ).checkInitialized(); - return ( (BasicHolder) holder ).value; - } - else if ( holder.getClass() == BigIntegerHolder.class ) { - ( (BigIntegerHolder) holder ).checkInitialized(); - return ( (BigIntegerHolder) holder ).value.longValue(); - } - else if ( holder.getClass() == BigDecimalHolder.class ) { - ( (BigDecimalHolder) holder ).checkInitialized(); - return ( (BigDecimalHolder) holder ).value.longValue(); - } - throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" ); - } - - public static BigInteger extractBigInteger(IntegralDataTypeHolder holder) { - if ( holder.getClass() == BasicHolder.class ) { - ( (BasicHolder) holder ).checkInitialized(); - return BigInteger.valueOf( ( (BasicHolder) holder ).value ); - } - else if ( holder.getClass() == BigIntegerHolder.class ) { - ( (BigIntegerHolder) holder ).checkInitialized(); - return ( (BigIntegerHolder) holder ).value; - } - else if ( holder.getClass() == BigDecimalHolder.class ) { - ( (BigDecimalHolder) holder ).checkInitialized(); - // scale should already be set... - return ( (BigDecimalHolder) holder ).value.toBigInteger(); - } - throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" ); - } - - public static BigDecimal extractBigDecimal(IntegralDataTypeHolder holder) { - if ( holder.getClass() == BasicHolder.class ) { - ( (BasicHolder) holder ).checkInitialized(); - return BigDecimal.valueOf( ( (BasicHolder) holder ).value ); - } - else if ( holder.getClass() == BigIntegerHolder.class ) { - ( (BigIntegerHolder) holder ).checkInitialized(); - return new BigDecimal( ( (BigIntegerHolder) holder ).value ); - } - else if ( holder.getClass() == BigDecimalHolder.class ) { - ( (BigDecimalHolder) holder ).checkInitialized(); - // scale should already be set... - return ( (BigDecimalHolder) holder ).value; - } - throw new IdentifierGenerationException( "Unknown IntegralDataTypeHolder impl [" + holder + "]" ); - } - public static Object getForeignId( String entityName, String propertyName, SharedSessionContractImplementor sessionImplementor, Object object) { final EntityPersister entityDescriptor = @@ -261,7 +211,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) { } public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) { - return multiplyBy( extractLong( factor ) ); + return multiplyBy( factor.toLong() ); } public IntegralDataTypeHolder multiplyBy(long factor) { @@ -271,7 +221,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) { } public boolean eq(IntegralDataTypeHolder other) { - return eq( extractLong( other ) ); + return eq( other.toLong() ); } public boolean eq(long value) { @@ -280,7 +230,7 @@ public boolean eq(long value) { } public boolean lt(IntegralDataTypeHolder other) { - return lt( extractLong( other ) ); + return lt( other.toLong() ); } public boolean lt(long value) { @@ -289,7 +239,7 @@ public boolean lt(long value) { } public boolean gt(IntegralDataTypeHolder other) { - return gt( extractLong( other ) ); + return gt( other.toLong() ); } public boolean gt(long value) { @@ -329,6 +279,24 @@ public Number makeValueThenAdd(long addend) { return result; } + @Override + public long toLong() { + checkInitialized(); + return value; + } + + @Override + public BigDecimal toBigDecimal() { + checkInitialized(); + return BigDecimal.valueOf( value ); + } + + @Override + public BigInteger toBigInteger() { + checkInitialized(); + return BigInteger.valueOf( value ); + } + @Override public String toString() { return "BasicHolder[" + exactType.getName() + "[" + value + "]]"; @@ -339,12 +307,9 @@ public boolean equals(Object o) { if ( this == o ) { return true; } - if ( o == null || getClass() != o.getClass() ) { + if ( !(o instanceof BasicHolder that) ) { return false; } - - BasicHolder that = (BasicHolder) o; - return value == that.value; } @@ -407,7 +372,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) { public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) { checkInitialized(); - value = value.multiply( extractBigInteger( factor ) ); + value = value.multiply( factor.toBigInteger() ); return this; } @@ -419,7 +384,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) { public boolean eq(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigInteger( other ) ) == 0; + return value.compareTo( other.toBigInteger() ) == 0; } public boolean eq(long value) { @@ -429,7 +394,7 @@ public boolean eq(long value) { public boolean lt(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigInteger( other ) ) < 0; + return value.compareTo( other.toBigInteger() ) < 0; } public boolean lt(long value) { @@ -439,7 +404,7 @@ public boolean lt(long value) { public boolean gt(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigInteger( other ) ) > 0; + return value.compareTo( other.toBigInteger() ) > 0; } public boolean gt(long value) { @@ -470,6 +435,24 @@ public Number makeValueThenAdd(long addend) { return result; } + @Override + public long toLong() { + checkInitialized(); + return value.longValue(); + } + + @Override + public BigInteger toBigInteger() { + checkInitialized(); + return value; + } + + @Override + public BigDecimal toBigDecimal() { + checkInitialized(); + return new BigDecimal( value ); + } + @Override public String toString() { return "BigIntegerHolder[" + value + "]"; @@ -480,12 +463,9 @@ public boolean equals(Object o) { if ( this == o ) { return true; } - if ( o == null || getClass() != o.getClass() ) { + if ( !(o instanceof BigIntegerHolder that) ) { return false; } - - BigIntegerHolder that = (BigIntegerHolder) o; - return Objects.equals( value, that.value ); } @@ -548,7 +528,7 @@ public IntegralDataTypeHolder subtract(long subtrahend) { public IntegralDataTypeHolder multiplyBy(IntegralDataTypeHolder factor) { checkInitialized(); - value = value.multiply( extractBigDecimal( factor ) ); + value = value.multiply( factor.toBigDecimal() ); return this; } @@ -560,7 +540,7 @@ public IntegralDataTypeHolder multiplyBy(long factor) { public boolean eq(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigDecimal( other ) ) == 0; + return value.compareTo( other.toBigDecimal() ) == 0; } public boolean eq(long value) { @@ -570,7 +550,7 @@ public boolean eq(long value) { public boolean lt(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigDecimal( other ) ) < 0; + return value.compareTo( other.toBigDecimal() ) < 0; } public boolean lt(long value) { @@ -580,7 +560,7 @@ public boolean lt(long value) { public boolean gt(IntegralDataTypeHolder other) { checkInitialized(); - return value.compareTo( extractBigDecimal( other ) ) > 0; + return value.compareTo( other.toBigDecimal() ) > 0; } public boolean gt(long value) { @@ -611,6 +591,24 @@ public Number makeValueThenAdd(long addend) { return result; } + @Override + public long toLong() { + checkInitialized(); + return value.longValue(); + } + + @Override + public BigInteger toBigInteger() { + checkInitialized(); + return value.toBigInteger(); + } + + @Override + public BigDecimal toBigDecimal() { + checkInitialized(); + return value; + } + @Override public String toString() { return "BigDecimalHolder[" + value + "]"; @@ -621,12 +619,9 @@ public boolean equals(Object o) { if ( this == o ) { return true; } - if ( o == null || getClass() != o.getClass() ) { + if ( !(o instanceof BigDecimalHolder that) ) { return false; } - - BigDecimalHolder that = (BigDecimalHolder) o; - return Objects.equals( this.value, that.value ); } diff --git a/hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java b/hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java index 6653ec2fdfd2..b3c2d7d92332 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java @@ -3,7 +3,10 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.id; + import java.io.Serializable; +import java.math.BigDecimal; +import java.math.BigInteger; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -183,4 +186,19 @@ public interface IntegralDataTypeHolder extends Serializable { * @return The pre-incremented internal value */ Number makeValueThenAdd(long addend); + + /** + * Convert the internal value to {@code long}. + */ + long toLong(); + + /** + * Convert the internal value to {@link BigInteger}. + */ + BigInteger toBigInteger(); + + /** + * Convert the internal value to {@link BigDecimal}. + */ + BigDecimal toBigDecimal(); }