11
11
import java .security .MessageDigest ;
12
12
import java .security .NoSuchAlgorithmException ;
13
13
import java .util .Arrays ;
14
- import java .util .Comparator ;
15
14
import java .util .List ;
16
15
17
16
import org .hibernate .HibernateException ;
@@ -28,7 +27,7 @@ public class NamingHelper {
28
27
public static final NamingHelper INSTANCE = new NamingHelper ();
29
28
30
29
public static NamingHelper withCharset (String charset ) {
31
- return new NamingHelper (charset );
30
+ return new NamingHelper ( charset );
32
31
}
33
32
34
33
private final String charset ;
@@ -69,28 +68,17 @@ public String generateHashedFkName(
69
68
Identifier tableName ,
70
69
Identifier referencedTableName ,
71
70
Identifier ... columnNames ) {
72
- // Use a concatenation that guarantees uniqueness, even if identical names
73
- // exist between all table and column identifiers.
74
-
75
- StringBuilder sb = new StringBuilder ()
71
+ // Use a concatenation that guarantees uniqueness, even if identical
72
+ // names exist between all table and column identifiers.
73
+ final StringBuilder sb = new StringBuilder ()
76
74
.append ( "table`" ).append ( tableName ).append ( "`" )
77
75
.append ( "references`" ).append ( referencedTableName ).append ( "`" );
78
-
79
76
// Ensure a consistent ordering of columns, regardless of the order
80
77
// they were bound.
81
78
// Clone the list, as sometimes a set of order-dependent Column
82
79
// bindings are given.
83
- Identifier [] alphabeticalColumns = columnNames .clone ();
84
- Arrays .sort (
85
- alphabeticalColumns ,
86
- new Comparator <Identifier >() {
87
- @ Override
88
- public int compare (Identifier o1 , Identifier o2 ) {
89
- return o1 .getCanonicalName ().compareTo ( o2 .getCanonicalName () );
90
- }
91
- }
92
- );
93
-
80
+ final Identifier [] alphabeticalColumns = columnNames .clone ();
81
+ Arrays .sort ( alphabeticalColumns , comparing ( Identifier ::getCanonicalName ) );
94
82
for ( Identifier columnName : alphabeticalColumns ) {
95
83
sb .append ( "column`" ).append ( columnName ).append ( "`" );
96
84
}
@@ -103,17 +91,16 @@ public int compare(Identifier o1, Identifier o2) {
103
91
*
104
92
* @return String The generated name
105
93
*/
106
- public String generateHashedConstraintName (String prefix , Identifier tableName , Identifier ... columnNames ) {
107
- // Use a concatenation that guarantees uniqueness, even if identical names
108
- // exist between all table and column identifiers.
109
-
110
- StringBuilder sb = new StringBuilder ( "table`" + tableName + "`" );
111
-
94
+ public String generateHashedConstraintName (
95
+ String prefix , Identifier tableName , Identifier ... columnNames ) {
96
+ // Use a concatenation that guarantees uniqueness, even if identical
97
+ // names exist between all table and column identifiers.
98
+ final StringBuilder sb = new StringBuilder ( "table`" + tableName + "`" );
112
99
// Ensure a consistent ordering of columns, regardless of the order
113
100
// they were bound.
114
101
// Clone the list, as sometimes a set of order-dependent Column
115
102
// bindings are given.
116
- Identifier [] alphabeticalColumns = columnNames .clone ();
103
+ final Identifier [] alphabeticalColumns = columnNames .clone ();
117
104
Arrays .sort ( alphabeticalColumns , comparing (Identifier ::getCanonicalName ) );
118
105
for ( Identifier columnName : alphabeticalColumns ) {
119
106
sb .append ( "column`" ).append ( columnName ).append ( "`" );
@@ -127,8 +114,9 @@ public String generateHashedConstraintName(String prefix, Identifier tableName,
127
114
*
128
115
* @return String The generated name
129
116
*/
130
- public String generateHashedConstraintName (String prefix , Identifier tableName , List <Identifier > columnNames ) {
131
- Identifier [] columnNamesArray = new Identifier [columnNames .size ()];
117
+ public String generateHashedConstraintName (
118
+ String prefix , Identifier tableName , List <Identifier > columnNames ) {
119
+ final Identifier [] columnNamesArray = new Identifier [columnNames .size ()];
132
120
for ( int i = 0 ; i < columnNames .size (); i ++ ) {
133
121
columnNamesArray [i ] = columnNames .get ( i );
134
122
}
@@ -141,23 +129,22 @@ public String generateHashedConstraintName(String prefix, Identifier tableName,
141
129
* that the length of the name will always be smaller than the 30
142
130
* character identifier restriction enforced by a few dialects.
143
131
*
144
- * @param s The name to be hashed.
132
+ * @param name The name to be hashed.
145
133
*
146
134
* @return String The hashed name.
147
135
*/
148
- public String hashedName (String s ) {
136
+ public String hashedName (String name ) {
149
137
try {
150
- MessageDigest md = MessageDigest .getInstance ( "MD5" );
151
- md .reset ();
152
- md .update ( charset != null ? s .getBytes ( charset ) : s .getBytes () );
153
- byte [] digest = md .digest ();
154
- BigInteger bigInt = new BigInteger ( 1 , digest );
138
+ final MessageDigest md5 = MessageDigest .getInstance ( "MD5" );
139
+ md5 .reset ();
140
+ md5 .update ( charset != null ? name .getBytes ( charset ) : name .getBytes () );
141
+ final BigInteger bigInt = new BigInteger ( 1 , md5 .digest () );
155
142
// By converting to base 35 (full alphanumeric), we guarantee
156
143
// that the length of the name will always be smaller than the 30
157
144
// character identifier restriction enforced by a few dialects.
158
145
return bigInt .toString ( 35 );
159
146
}
160
- catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
147
+ catch ( NoSuchAlgorithmException | UnsupportedEncodingException e ) {
161
148
throw new HibernateException ( "Unable to generate a hashed name" , e );
162
149
}
163
150
}
0 commit comments