Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 + "]]";
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 + "]";
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 + "]";
Expand All @@ -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 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}