clean up OptimisticLockStyle enum

This commit is contained in:
Gavin 2022-12-22 13:54:18 +01:00 committed by Gavin King
parent c754dfacdf
commit bc79368cd6
2 changed files with 41 additions and 27 deletions

View File

@ -6,8 +6,14 @@
*/ */
package org.hibernate.engine; package org.hibernate.engine;
import org.hibernate.AssertionFailure;
import org.hibernate.annotations.OptimisticLockType;
/** /**
* Describes how an entity should be optimistically locked. * Describes how an entity should be optimistically locked.
* <p>
* This enumeration is mainly for internal use, since it
* is isomorphic to {@link OptimisticLockType}.
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
@ -15,29 +21,19 @@ public enum OptimisticLockStyle {
/** /**
* no optimistic locking * no optimistic locking
*/ */
NONE( -1 ), NONE,
/** /**
* use a dedicated version column * use a dedicated version column
*/ */
VERSION( 0 ), VERSION,
/** /**
* dirty columns are compared * dirty columns are compared
*/ */
DIRTY( 1 ), DIRTY,
/** /**
* all columns are compared * all columns are compared
*/ */
ALL( 2 ); ALL;
private final int oldCode;
private OptimisticLockStyle(int oldCode) {
this.oldCode = oldCode;
}
public int getOldCode() {
return oldCode;
}
public boolean isAllOrDirty() { public boolean isAllOrDirty() {
return isAll() || isDirty(); return isAll() || isDirty();
@ -60,31 +56,49 @@ public enum OptimisticLockStyle {
} }
/** /**
* Given an old code (one of the int constants from Cascade), interpret it as one of the new enums. * @deprecated these integer codes have not been used for a long time
*/
@Deprecated(since = "6.2", forRemoval = true)
public int getOldCode() {
switch (this) {
case NONE:
return -1;
case VERSION:
return 0;
case DIRTY:
return 1;
case ALL:
return 2;
default:
throw new AssertionFailure("Unknown OptimisticLockStyle");
}
}
/**
* Given an old code (one of the int constants from Cascade),
* interpret it as one of the new enums.
* *
* @param oldCode The old int constant code * @param oldCode The old int constant code
* *
* @return The interpreted enum value * @return The interpreted enum value
* *
* @throws IllegalArgumentException If the code did not match any legacy constant. * @throws IllegalArgumentException If the code did not match any legacy constant.
*
* @deprecated these integer codes have not been used for a long time
*/ */
@Deprecated(since = "6.2", forRemoval = true)
public static OptimisticLockStyle interpretOldCode(int oldCode) { public static OptimisticLockStyle interpretOldCode(int oldCode) {
switch ( oldCode ) { switch ( oldCode ) {
case -1: { case -1:
return NONE; return NONE;
} case 0:
case 0: {
return VERSION; return VERSION;
} case 1:
case 1: {
return DIRTY; return DIRTY;
} case 2:
case 2: {
return ALL; return ALL;
} default:
default: {
throw new IllegalArgumentException( "Illegal legacy optimistic lock style code : " + oldCode ); throw new IllegalArgumentException( "Illegal legacy optimistic lock style code : " + oldCode );
} }
} }
} }
}

View File

@ -697,7 +697,7 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
/** /**
* @deprecated prefer {@link #getOptimisticLockStyle} * @deprecated prefer {@link #getOptimisticLockStyle}
*/ */
@Deprecated @Deprecated(forRemoval = true)
public int getOptimisticLockMode() { public int getOptimisticLockMode() {
return getOptimisticLockStyle().getOldCode(); return getOptimisticLockStyle().getOldCode();
} }
@ -705,7 +705,7 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
/** /**
* @deprecated prefer {@link #setOptimisticLockStyle} * @deprecated prefer {@link #setOptimisticLockStyle}
*/ */
@Deprecated @Deprecated(forRemoval = true)
public void setOptimisticLockMode(int optimisticLockMode) { public void setOptimisticLockMode(int optimisticLockMode) {
setOptimisticLockStyle( OptimisticLockStyle.interpretOldCode( optimisticLockMode ) ); setOptimisticLockStyle( OptimisticLockStyle.interpretOldCode( optimisticLockMode ) );
} }