From bc79368cd66959b6ac6068630a42371151f98e7e Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 22 Dec 2022 13:54:18 +0100 Subject: [PATCH] clean up OptimisticLockStyle enum --- .../hibernate/engine/OptimisticLockStyle.java | 64 +++++++++++-------- .../hibernate/mapping/PersistentClass.java | 4 +- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/OptimisticLockStyle.java b/hibernate-core/src/main/java/org/hibernate/engine/OptimisticLockStyle.java index d2401d6da9..89f78a878b 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/OptimisticLockStyle.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/OptimisticLockStyle.java @@ -6,8 +6,14 @@ */ package org.hibernate.engine; +import org.hibernate.AssertionFailure; +import org.hibernate.annotations.OptimisticLockType; + /** * Describes how an entity should be optimistically locked. + *

+ * This enumeration is mainly for internal use, since it + * is isomorphic to {@link OptimisticLockType}. * * @author Steve Ebersole */ @@ -15,29 +21,19 @@ public enum OptimisticLockStyle { /** * no optimistic locking */ - NONE( -1 ), + NONE, /** * use a dedicated version column */ - VERSION( 0 ), + VERSION, /** * dirty columns are compared */ - DIRTY( 1 ), + DIRTY, /** * all columns are compared */ - ALL( 2 ); - - private final int oldCode; - - private OptimisticLockStyle(int oldCode) { - this.oldCode = oldCode; - } - - public int getOldCode() { - return oldCode; - } + ALL; public boolean isAllOrDirty() { 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 * * @return The interpreted enum value * * @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) { switch ( oldCode ) { - case -1: { + case -1: return NONE; - } - case 0: { + case 0: return VERSION; - } - case 1: { + case 1: return DIRTY; - } - case 2: { + case 2: return ALL; - } - default: { + default: throw new IllegalArgumentException( "Illegal legacy optimistic lock style code : " + oldCode ); - } } } } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java index d163dfdd04..5a1015a877 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java @@ -697,7 +697,7 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl /** * @deprecated prefer {@link #getOptimisticLockStyle} */ - @Deprecated + @Deprecated(forRemoval = true) public int getOptimisticLockMode() { return getOptimisticLockStyle().getOldCode(); } @@ -705,7 +705,7 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl /** * @deprecated prefer {@link #setOptimisticLockStyle} */ - @Deprecated + @Deprecated(forRemoval = true) public void setOptimisticLockMode(int optimisticLockMode) { setOptimisticLockStyle( OptimisticLockStyle.interpretOldCode( optimisticLockMode ) ); }