HHH-14208 simplify example naming strategy code in user guide
This commit is contained in:
parent
4499abd9a9
commit
97ea1d853d
|
@ -6,14 +6,15 @@
|
|||
*/
|
||||
package org.hibernate.userguide.naming;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -27,40 +28,35 @@ import org.apache.commons.lang3.StringUtils;
|
|||
* Additionally standards call for the replacement of certain words with abbreviations.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
public class AcmeCorpPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
||||
private static final Map<String,String> ABBREVIATIONS = buildAbbreviationMap();
|
||||
public class AcmeCorpPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {
|
||||
private static final Map<String, String> ABBREVIATIONS;
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||
// Acme naming standards do not apply to catalog names
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||
// Acme naming standards do not apply to schema names
|
||||
return name;
|
||||
static {
|
||||
ABBREVIATIONS = new TreeMap<>( String.CASE_INSENSITIVE_ORDER );
|
||||
ABBREVIATIONS.put( "account", "acct" );
|
||||
ABBREVIATIONS.put( "number", "num" );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||
final List<String> parts = splitAndReplace( name.getText() );
|
||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||
join( parts ),
|
||||
StringUtils.join( parts, '_' ),
|
||||
name.isQuoted()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||
final LinkedList<String> parts = splitAndReplace( name.getText() );
|
||||
final List<String> parts = splitAndReplace( name.getText() );
|
||||
// Acme Corp says all sequences should end with _seq
|
||||
if ( !"seq".equalsIgnoreCase( parts.getLast() ) ) {
|
||||
if ( !"seq".equals( parts.get( parts.size() - 1 ) ) ) {
|
||||
parts.add( "seq" );
|
||||
}
|
||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||
join( parts ),
|
||||
StringUtils.join( parts, '_' ),
|
||||
name.isQuoted()
|
||||
);
|
||||
}
|
||||
|
@ -69,50 +65,15 @@ public class AcmeCorpPhysicalNamingStrategy implements PhysicalNamingStrategy {
|
|||
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
|
||||
final List<String> parts = splitAndReplace( name.getText() );
|
||||
return jdbcEnvironment.getIdentifierHelper().toIdentifier(
|
||||
join( parts ),
|
||||
StringUtils.join( parts, '_' ),
|
||||
name.isQuoted()
|
||||
);
|
||||
}
|
||||
|
||||
private static Map<String, String> buildAbbreviationMap() {
|
||||
TreeMap<String,String> abbreviationMap = new TreeMap<> ( String.CASE_INSENSITIVE_ORDER );
|
||||
abbreviationMap.put( "account", "acct" );
|
||||
abbreviationMap.put( "number", "num" );
|
||||
return abbreviationMap;
|
||||
}
|
||||
|
||||
private LinkedList<String> splitAndReplace(String name) {
|
||||
LinkedList<String> result = new LinkedList<>();
|
||||
for ( String part : StringUtils.splitByCharacterTypeCamelCase( name ) ) {
|
||||
if ( part == null || part.trim().isEmpty() ) {
|
||||
// skip null and space
|
||||
continue;
|
||||
}
|
||||
part = applyAbbreviationReplacement( part );
|
||||
result.add( part.toLowerCase( Locale.ROOT ) );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String applyAbbreviationReplacement(String word) {
|
||||
if ( ABBREVIATIONS.containsKey( word ) ) {
|
||||
return ABBREVIATIONS.get( word );
|
||||
}
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
private String join(List<String> parts) {
|
||||
boolean firstPass = true;
|
||||
String separator = "";
|
||||
StringBuilder joined = new StringBuilder();
|
||||
for ( String part : parts ) {
|
||||
joined.append( separator ).append( part );
|
||||
if ( firstPass ) {
|
||||
firstPass = false;
|
||||
separator = "_";
|
||||
}
|
||||
}
|
||||
return joined.toString();
|
||||
private List<String> splitAndReplace(String name) {
|
||||
return Arrays.stream( StringUtils.splitByCharacterTypeCamelCase( name ) )
|
||||
.filter( StringUtils::isNotBlank )
|
||||
.map( p -> ABBREVIATIONS.getOrDefault( p, p ).toLowerCase( Locale.ROOT ) )
|
||||
.collect( Collectors.toList() );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue