minor code cleanups to Identifier

This commit is contained in:
Gavin King 2024-11-29 12:18:50 +01:00
parent 38fb68a57c
commit 817f62963e
2 changed files with 14 additions and 13 deletions

View File

@ -4,7 +4,7 @@
*/ */
package org.hibernate.boot.model.naming; package org.hibernate.boot.model.naming;
import org.hibernate.internal.util.StringHelper; import static org.hibernate.internal.util.StringHelper.isEmpty;
/** /**
* Models an identifier (name), retrieved from the database. * Models an identifier (name), retrieved from the database.
@ -24,13 +24,13 @@ public class DatabaseIdentifier extends Identifier {
} }
public static DatabaseIdentifier toIdentifier(String text) { public static DatabaseIdentifier toIdentifier(String text) {
if ( StringHelper.isEmpty( text ) ) { if ( isEmpty( text ) ) {
return null; return null;
} }
else if ( isQuoted( text ) ) { else if ( isQuoted( text ) ) {
// exclude the quotes from text // exclude the quotes from text
final String unquotedtext = text.substring( 1, text.length() - 1 ); final String unquoted = text.substring( 1, text.length() - 1 );
return new DatabaseIdentifier( unquotedtext ); return new DatabaseIdentifier( unquoted );
} }
else { else {
return new DatabaseIdentifier( text ); return new DatabaseIdentifier( text );

View File

@ -7,9 +7,12 @@ package org.hibernate.boot.model.naming;
import java.util.Locale; import java.util.Locale;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
import static java.lang.Character.isLetter;
import static java.lang.Character.isLetterOrDigit;
import static java.lang.Character.isWhitespace;
import static org.hibernate.internal.util.StringHelper.isBlank; import static org.hibernate.internal.util.StringHelper.isBlank;
import static org.hibernate.internal.util.StringHelper.isEmpty;
/** /**
* Models an identifier (name), which may or may not be quoted. * Models an identifier (name), which may or may not be quoted.
@ -83,13 +86,13 @@ public class Identifier implements Comparable<Identifier> {
int start = 0; int start = 0;
int end = text.length(); int end = text.length();
while ( start < end ) { while ( start < end ) {
if ( !Character.isWhitespace( text.charAt( start ) ) ) { if ( !isWhitespace( text.charAt( start ) ) ) {
break; break;
} }
start++; start++;
} }
while ( start < end ) { while ( start < end ) {
if ( !Character.isWhitespace( text.charAt( end - 1 ) ) ) { if ( !isWhitespace( text.charAt( end - 1 ) ) ) {
break; break;
} }
end--; end--;
@ -102,14 +105,14 @@ public class Identifier implements Comparable<Identifier> {
else if ( quoteOnNonIdentifierChar && !quote ) { else if ( quoteOnNonIdentifierChar && !quote ) {
// Check the letters to determine if we must quote the text // Check the letters to determine if we must quote the text
char c = text.charAt( start ); char c = text.charAt( start );
if ( !Character.isLetter( c ) && c != '_' ) { if ( !isLetter( c ) && c != '_' ) {
// SQL identifiers must begin with a letter or underscore // SQL identifiers must begin with a letter or underscore
quote = true; quote = true;
} }
else { else {
for ( int i = start + 1; i < end; i++ ) { for ( int i = start + 1; i < end; i++ ) {
c = text.charAt( i ); c = text.charAt( i );
if ( !Character.isLetterOrDigit( c ) && c != '_' ) { if ( !isLetterOrDigit( c ) && c != '_' ) {
quote = true; quote = true;
break; break;
} }
@ -163,7 +166,7 @@ public class Identifier implements Comparable<Identifier> {
* @param quoted Is this a quoted identifier? * @param quoted Is this a quoted identifier?
*/ */
public Identifier(String text, boolean quoted) { public Identifier(String text, boolean quoted) {
if ( StringHelper.isEmpty( text ) ) { if ( isEmpty( text ) ) {
throw new IllegalIdentifierException( "Identifier text cannot be null" ); throw new IllegalIdentifierException( "Identifier text cannot be null" );
} }
if ( isQuoted( text ) ) { if ( isQuoted( text ) ) {
@ -234,11 +237,9 @@ public class Identifier implements Comparable<Identifier> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if ( !(o instanceof Identifier) ) { if ( !(o instanceof Identifier that) ) {
return false; return false;
} }
final Identifier that = (Identifier) o;
return getCanonicalName().equals( that.getCanonicalName() ); return getCanonicalName().equals( that.getCanonicalName() );
} }