mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-08 20:24:46 +00:00
HHH-14190 Improve efficiency of StringHelper#isBlank
This commit is contained in:
parent
c6728684bb
commit
4fe81f22ec
@ -473,7 +473,20 @@ public static boolean isEmpty(String string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isBlank(String string) {
|
public static boolean isBlank(String string) {
|
||||||
return isEmpty( string ) || isEmpty( string.trim() );
|
//TODO use Java 11's more efficient String#isBlank - currently we still require Java 8 compatibility
|
||||||
|
if ( string == null || string.isEmpty() ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//Else: we need to check all characters, preferably without using String#trim() so to
|
||||||
|
//not allocate temporary strings
|
||||||
|
for ( int i = 0; i < string.length(); i++ ) {
|
||||||
|
if ( ! Character.isWhitespace( string.charAt( i ) ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@ -44,6 +45,18 @@ public void testPartialNameUnqualification() {
|
|||||||
assertEquals( "internal.util.StringHelper", StringHelper.partiallyUnqualify( STRING_HELPER_FQN, BASE_PACKAGE ) );
|
assertEquals( "internal.util.StringHelper", StringHelper.partiallyUnqualify( STRING_HELPER_FQN, BASE_PACKAGE ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsBlank() {
|
||||||
|
assertFalse( StringHelper.isBlank( "A" ) );
|
||||||
|
assertFalse( StringHelper.isBlank( " a" ) );
|
||||||
|
assertFalse( StringHelper.isBlank( "a " ) );
|
||||||
|
assertFalse( StringHelper.isBlank( "a\t" ) );
|
||||||
|
assertTrue( StringHelper.isBlank( "\t\n" ) );
|
||||||
|
assertTrue( StringHelper.isBlank( null ) );
|
||||||
|
assertTrue( StringHelper.isBlank( "" ) );
|
||||||
|
assertTrue( StringHelper.isBlank( " " ) );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBasePackageCollapsing() {
|
public void testBasePackageCollapsing() {
|
||||||
assertNull( StringHelper.collapseQualifierBase( null, BASE_PACKAGE ) );
|
assertNull( StringHelper.collapseQualifierBase( null, BASE_PACKAGE ) );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user