HHH-17613 - don't use reduce()

This commit is contained in:
Gavin King 2024-02-20 10:10:15 +01:00
parent b17aa9674f
commit 9ba93b7060
1 changed files with 28 additions and 18 deletions

View File

@ -10,10 +10,14 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet; import java.util.TreeSet;
import org.hibernate.jpamodelgen.model.ImportContext; import org.hibernate.jpamodelgen.model.ImportContext;
import static java.lang.Character.isWhitespace;
import static java.lang.System.lineSeparator;
/** /**
* @author Max Andersen * @author Max Andersen
@ -75,7 +79,7 @@ public class ImportContextImpl implements ImportContext {
} }
else if ( result.startsWith( "?" ) ) { else if ( result.startsWith( "?" ) ) {
int index = 1; int index = 1;
while ( index < result.length() && Character.isWhitespace( result.charAt( index ) ) ) { while ( index < result.length() && isWhitespace( result.charAt( index ) ) ) {
index++; index++;
} }
if ( index < result.length() ) { if ( index < result.length() ) {
@ -86,9 +90,9 @@ public class ImportContextImpl implements ImportContext {
else if ( result.substring( index ).startsWith( "super" ) ) { else if ( result.substring( index ).startsWith( "super" ) ) {
nextIndex = index + 5; nextIndex = index + 5;
} }
if ( nextIndex > 0 && nextIndex < result.length() && Character.isWhitespace( result.charAt( nextIndex ) ) ) { if ( nextIndex > 0 && nextIndex < result.length() && isWhitespace( result.charAt( nextIndex ) ) ) {
index = nextIndex; index = nextIndex;
while ( Character.isWhitespace( result.charAt( index ) ) ) { while ( isWhitespace( result.charAt( index ) ) ) {
index++; index++;
} }
preamble = result.substring( 0, index ); preamble = result.substring( 0, index );
@ -144,26 +148,32 @@ public class ImportContextImpl implements ImportContext {
} }
private String importTypes(String originalArgList) { private String importTypes(String originalArgList) {
String[] args = originalArgList.split(",");
StringBuilder argList = new StringBuilder(); StringBuilder argList = new StringBuilder();
StringBuilder acc = new StringBuilder(); StringBuilder acc = new StringBuilder();
for ( String arg : args ) { StringTokenizer args = new StringTokenizer( originalArgList, "," );
while ( args.hasMoreTokens() ) {
if ( acc.length() > 0 ) { if ( acc.length() > 0 ) {
acc.append( ',' ); acc.append( ',' );
} }
acc.append( arg ); acc.append( args.nextToken() );
final int count = acc.chars().reduce( int nesting = 0;
0, for ( int i = 0; i<acc.length(); i++ ) {
(left, right) -> left + ( right == '<' ? 1 : right == '>' ? -1 : 0 ) switch ( acc.charAt(i) ) {
); case '<':
if ( count > 0 ) { nesting++;
continue; break;
case '>':
nesting--;
break;
}
} }
if ( argList.length() > 0 ) { if ( nesting == 0 ) {
argList.append(','); if ( argList.length() > 0 ) {
argList.append(',');
}
argList.append( importType( acc.toString() ) );
acc.setLength( 0 );
} }
argList.append( importType( acc.toString() ) );
acc.setLength( 0 );
} }
return argList.toString(); return argList.toString();
} }
@ -204,10 +214,10 @@ public class ImportContextImpl implements ImportContext {
// don't add automatically "imported" stuff // don't add automatically "imported" stuff
if ( !isAutoImported( next ) ) { if ( !isAutoImported( next ) ) {
if ( staticImports.contains( next ) ) { if ( staticImports.contains( next ) ) {
builder.append( "import static " ).append( next ).append( ";" ).append( System.lineSeparator() ); builder.append( "import static " ).append( next ).append( ";" ).append( lineSeparator() );
} }
else { else {
builder.append( "import " ).append( next ).append( ";" ).append( System.lineSeparator() ); builder.append( "import " ).append( next ).append( ";" ).append( lineSeparator() );
} }
} }
} }