Clean up use of generic types in EnumValueConverters

Use the "self type" idiom correctly.
This commit is contained in:
gavinking 2020-01-30 18:30:33 +01:00 committed by Steve Ebersole
parent d5096fc660
commit 1989635bc2
5 changed files with 53 additions and 41 deletions

View File

@ -28,7 +28,7 @@
*
* @author Steve Ebersole
*/
public class NamedEnumValueConverter<E extends Enum> implements EnumValueConverter<E,String>, Serializable {
public class NamedEnumValueConverter<E extends Enum<E>> implements EnumValueConverter<E,String>, Serializable {
private final EnumJavaTypeDescriptor<E> domainTypeDescriptor;
private final SqlTypeDescriptor sqlTypeDescriptor;
private final JavaTypeDescriptor<String> relationalTypeDescriptor;
@ -74,9 +74,9 @@ public int getJdbcTypeCode() {
}
@Override
@SuppressWarnings("unchecked")
public String toSqlLiteral(Object value) {
return String.format( Locale.ROOT, "'%s'", ( (E) value ).name() );
//noinspection rawtypes
return String.format( Locale.ROOT, "'%s'", ( (Enum) value ).name() );
}
private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
@ -89,7 +89,7 @@ private void readObject(ObjectInputStream stream) throws ClassNotFoundException,
@Override
public void writeValue(
PreparedStatement statement,
Enum value,
E value,
int position,
SharedSessionContractImplementor session) throws SQLException {
final String jdbcValue = value == null ? null : value.name();

View File

@ -27,7 +27,7 @@
*
* @author Steve Ebersole
*/
public class OrdinalEnumValueConverter<E extends Enum> implements EnumValueConverter<E,Integer>, Serializable {
public class OrdinalEnumValueConverter<E extends Enum<E>> implements EnumValueConverter<E,Integer>, Serializable {
private final EnumJavaTypeDescriptor<E> enumJavaDescriptor;
private final SqlTypeDescriptor sqlTypeDescriptor;
@ -60,6 +60,9 @@ public Integer toRelationalValue(E domainForm) {
@Override
public int getJdbcTypeCode() {
// note, even though we convert the enum to
// an Integer here, we actually map it to a
// database column of type TINYINT by default
return Types.TINYINT;
}
@ -74,9 +77,9 @@ public JavaTypeDescriptor<Integer> getRelationalJavaDescriptor() {
}
@Override
@SuppressWarnings("unchecked")
public String toSqlLiteral(Object value) {
return Integer.toString( ( (E) value ).ordinal() );
//noinspection rawtypes
return Integer.toString( ( (Enum) value ).ordinal() );
}
private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
@ -88,9 +91,8 @@ private void readObject(ObjectInputStream stream) throws ClassNotFoundException,
@Override
public void writeValue(
PreparedStatement statement, Enum value, int position, SharedSessionContractImplementor session)
PreparedStatement statement, E value, int position, SharedSessionContractImplementor session)
throws SQLException {
final Integer jdbcValue = value == null ? null : value.ordinal();
valueBinder.bind( statement, jdbcValue, position, session );
valueBinder.bind( statement, toRelationalValue( value ), position, session );
}
}

View File

@ -20,7 +20,7 @@
*
* @author Steve Ebersole
*/
public interface EnumValueConverter<O extends Enum, R> extends BasicValueConverter<O,R> {
public interface EnumValueConverter<O extends Enum<O>, R> extends BasicValueConverter<O,R> {
@Override
EnumJavaTypeDescriptor<O> getDomainJavaDescriptor();
@ -38,7 +38,7 @@ public interface EnumValueConverter<O extends Enum, R> extends BasicValueConvert
@Deprecated
void writeValue(
PreparedStatement statement,
Enum value,
O value,
int position,
SharedSessionContractImplementor session) throws SQLException;
}

View File

@ -31,17 +31,17 @@
*
* @author Steve Ebersole
*/
public class SqmEnumLiteral implements SqmExpression<Enum>, SqmExpressable<Enum>, SemanticPathPart {
private final Enum enumValue;
private final EnumJavaTypeDescriptor<Enum> referencedEnumTypeDescriptor;
public class SqmEnumLiteral<E extends Enum<E>> implements SqmExpression<E>, SqmExpressable<E>, SemanticPathPart {
private final E enumValue;
private final EnumJavaTypeDescriptor<E> referencedEnumTypeDescriptor;
private final String enumValueName;
private final NodeBuilder nodeBuilder;
private SqmExpressable<Enum> expressable;
private SqmExpressable<E> expressable;
public SqmEnumLiteral(
Enum enumValue,
EnumJavaTypeDescriptor<Enum> referencedEnumTypeDescriptor,
E enumValue,
EnumJavaTypeDescriptor<E> referencedEnumTypeDescriptor,
String enumValueName,
NodeBuilder nodeBuilder) {
this.enumValue = enumValue;
@ -61,7 +61,7 @@ public String getEnumValueName() {
}
@Override
public EnumJavaTypeDescriptor<Enum> getExpressableJavaTypeDescriptor() {
public EnumJavaTypeDescriptor<E> getExpressableJavaTypeDescriptor() {
return referencedEnumTypeDescriptor;
}
@ -100,7 +100,7 @@ public SqmPath resolveIndexedAccess(
}
@Override
public SqmExpressable<Enum> getNodeType() {
public SqmExpressable<E> getNodeType() {
return expressable;
}
@ -186,7 +186,7 @@ public <X> X accept(SemanticQueryWalker<X> walker) {
}
@Override
public JavaTypeDescriptor<Enum> getJavaTypeDescriptor() {
public JavaTypeDescriptor<E> getJavaTypeDescriptor() {
return getExpressableJavaTypeDescriptor();
}
@ -207,7 +207,7 @@ public boolean isCompoundSelection() {
}
@Override
public JpaSelection<Enum> alias(String name) {
public JpaSelection<E> alias(String name) {
return null;
}

View File

@ -22,7 +22,7 @@
*
* @author Steve Ebersole
*/
public class EnumJavaTypeDescriptor<T extends Enum> extends AbstractTypeDescriptor<T> {
public class EnumJavaTypeDescriptor<T extends Enum<T>> extends AbstractTypeDescriptor<T> {
@SuppressWarnings("unchecked")
public EnumJavaTypeDescriptor(Class<T> type) {
super( type, ImmutableMutabilityPlan.INSTANCE );
@ -46,7 +46,6 @@ public String toString(T value) {
}
@Override
@SuppressWarnings("unchecked")
public T fromString(String string) {
return string == null ? null : (T) Enum.valueOf( getJavaType(), string );
}
@ -58,7 +57,7 @@ public <X> X unwrap(T value, Class<X> type, WrapperOptions options) {
return (X) toName( value );
}
else if ( Integer.class.equals( type ) ) {
return (X) toOrdinal( value );
return (X) toInteger( value );
}
else if ( Byte.class.equals( type ) ) {
return (X) toByte( value );
@ -76,7 +75,7 @@ else if ( value instanceof String ) {
return fromName( (String) value );
}
else if ( value instanceof Integer ) {
return fromOrdinal( (Integer) value );
return fromInteger( (Integer) value );
}
else if ( value instanceof Byte ) {
return fromByte( (Byte) value );
@ -88,7 +87,7 @@ else if ( value instanceof Byte ) {
/**
* Convert a value of the enum type to its ordinal value
*/
public <E extends Enum> Byte toByte(E domainForm) {
public Byte toByte(T domainForm) {
if ( domainForm == null ) {
return null;
}
@ -98,7 +97,7 @@ public <E extends Enum> Byte toByte(E domainForm) {
/**
* Convert a value of the enum type to its ordinal value
*/
public <E extends Enum> Integer toOrdinal(E domainForm) {
public Integer toInteger(T domainForm) {
if ( domainForm == null ) {
return null;
}
@ -106,25 +105,37 @@ public <E extends Enum> Integer toOrdinal(E domainForm) {
}
/**
* Interpret a numeric value as the ordinal of the enum type
* Convert a value of the enum type to its ordinal value
*/
@SuppressWarnings("unchecked")
public <E extends Enum> E fromByte(Byte relationalForm) {
if ( relationalForm == null ) {
return null;
}
return (E) getJavaType().getEnumConstants()[ relationalForm ];
public Integer toOrdinal(T domainForm) {
return toInteger( domainForm );
}
/**
* Interpret a numeric value as the ordinal of the enum type
*/
@SuppressWarnings("unchecked")
public <E extends Enum> E fromOrdinal(Integer relationalForm) {
public T fromByte(Byte relationalForm) {
if ( relationalForm == null ) {
return null;
}
return (E) getJavaType().getEnumConstants()[ relationalForm ];
return getJavaType().getEnumConstants()[ relationalForm ];
}
/**
* Interpret a numeric value as the ordinal of the enum type
*/
public T fromInteger(Integer relationalForm) {
if ( relationalForm == null ) {
return null;
}
return getJavaType().getEnumConstants()[ relationalForm ];
}
/**
* Interpret a numeric value as the ordinal of the enum type
*/
public T fromOrdinal(Integer relationalForm) {
return fromInteger( relationalForm );
}
/**
@ -140,12 +151,11 @@ public String toName(T domainForm) {
/**
* Interpret a String value as the named value of the enum type
*/
@SuppressWarnings("unchecked")
public T fromName(String relationalForm) {
if ( relationalForm == null ) {
return null;
}
return (T) Enum.valueOf( getJavaType(), relationalForm.trim() );
return Enum.valueOf( getJavaType(), relationalForm.trim() );
}
@Override
@ -157,7 +167,7 @@ public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDesc
}
else if (sqlTypeDescriptor instanceof VarcharTypeDescriptor) {
StringBuilder types = new StringBuilder();
for ( Enum value : getJavaType().getEnumConstants() ) {
for ( Enum<T> value : getJavaType().getEnumConstants() ) {
if (types.length() != 0) {
types.append(", ");
}