-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Identifier.java
199 lines (180 loc) · 5.26 KB
/
Identifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.boot.model.naming;
import java.util.Locale;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
/**
* Models an identifier (name), which may or may not be quoted.
*
* @author Steve Ebersole
*/
public class Identifier implements Comparable<Identifier> {
private final String text;
private final boolean isQuoted;
/**
* Means to generate an {@link Identifier} instance from its simple text form.
* <p/>
* If passed text is {@code null}, {@code null} is returned.
* <p/>
* If passed text is surrounded in quote markers, the generated Identifier
* is considered quoted. Quote markers include back-ticks (`), and
* double-quotes (").
*
* @param text The text form
*
* @return The identifier form, or {@code null} if text was {@code null}
*/
public static Identifier toIdentifier(String text) {
if ( StringHelper.isEmpty( text ) ) {
return null;
}
final String trimmedText = text.trim();
if ( isQuoted( trimmedText ) ) {
final String bareName = trimmedText.substring( 1, trimmedText.length() - 1 );
return new Identifier( bareName, true );
}
else {
return new Identifier( trimmedText, false );
}
}
/**
* Means to generate an {@link Identifier} instance from its simple text form.
* <p/>
* If passed text is {@code null}, {@code null} is returned.
* <p/>
* If passed text is surrounded in quote markers, the generated Identifier
* is considered quoted. Quote markers include back-ticks (`), and
* double-quotes (").
*
* @param text The text form
* @param quote Whether to quote unquoted text forms
*
* @return The identifier form, or {@code null} if text was {@code null}
*/
public static Identifier toIdentifier(String text, boolean quote) {
if ( StringHelper.isEmpty( text ) ) {
return null;
}
final String trimmedText = text.trim();
if ( isQuoted( trimmedText ) ) {
final String bareName = trimmedText.substring( 1, trimmedText.length() - 1 );
return new Identifier( bareName, true );
}
else {
return new Identifier( trimmedText, quote );
}
}
/**
* Is the given identifier text considered quoted. The following patterns are
* recognized as quoted:<ul>
* <li>{@code `name`}</li>
* <li>{@code [name]}</li>
* <li>{@code "name"}</li>
* </ul>
* <p/>
* That final form using double-quote (") is the JPA-defined quoting pattern. Although
* it is the standard, it makes for ugly declarations.
*
* @param name
*
* @return
*/
public static boolean isQuoted(String name) {
return ( name.startsWith( "`" ) && name.endsWith( "`" ) )
|| ( name.startsWith( "[" ) && name.endsWith( "]" ) )
|| ( name.startsWith( "\"" ) && name.endsWith( "\"" ) );
}
/**
* Constructs an identifier instance.
*
* @param text The identifier text.
* @param quoted Is this a quoted identifier?
*/
public Identifier(String text, boolean quoted) {
if ( StringHelper.isEmpty( text ) ) {
throw new IllegalIdentifierException( "Identifier text cannot be null" );
}
if ( isQuoted( text ) ) {
throw new IllegalIdentifierException( "Identifier text should not contain quote markers (` or \")" );
}
this.text = text;
this.isQuoted = quoted;
}
/**
* Get the identifiers name (text)
*
* @return The name
*/
public String getText() {
return text;
}
/**
* Is this a quoted identifier>
*
* @return True if this is a quote identifier; false otherwise.
*/
public boolean isQuoted() {
return isQuoted;
}
/**
* If this is a quoted identifier, then return the identifier name
* enclosed in dialect-specific open- and end-quotes; otherwise,
* simply return the unquoted identifier.
*
* @param dialect The dialect whose dialect-specific quoting should be used.
*
* @return if quoted, identifier name enclosed in dialect-specific open- and
* end-quotes; otherwise, the unquoted identifier.
*/
public String render(Dialect dialect) {
return isQuoted
? String.valueOf( dialect.openQuote() ) + getText() + dialect.closeQuote()
: getText();
}
public String render() {
return isQuoted
? '`' + getText() + '`'
: getText();
}
public String getCanonicalName() {
return isQuoted ? text : text.toLowerCase( Locale.ENGLISH );
}
@Override
public String toString() {
return render();
}
@Override
public boolean equals(Object o) {
if ( !(o instanceof Identifier) ) {
return false;
}
final Identifier that = (Identifier) o;
return getCanonicalName().equals( that.getCanonicalName() );
}
@Override
public int hashCode() {
return isQuoted ? text.hashCode() : text.toLowerCase( Locale.ENGLISH ).hashCode();
}
public static boolean areEqual(Identifier id1, Identifier id2) {
if ( id1 == null ) {
return id2 == null;
}
else {
return id1.equals( id2 );
}
}
public static Identifier quote(Identifier identifier) {
return identifier.isQuoted()
? identifier
: Identifier.toIdentifier( identifier.getText(), true );
}
@Override
public int compareTo(Identifier o) {
return getCanonicalName().compareTo( o.getCanonicalName() );
}
}