public class Inflector extends Object
API for performing inflections (pluralization, singularization, and so on) on various strings. These inflections will be useful in code generators that convert things like database table names into Java class names.
The getInstance()
method returns a singleton instance of
this class with a default set of rules, which can then be customized.
Rules added during customization will take precedence over the standard ones.
Use the addIrregular()
, addPlural()
, addSingular()
,
and addUncountable()
methods to add additional rules ot the default
ones.
IMPLEMENTATION NOTE - The default implementation is
intended to be functionally compatible with the Inflector::inflections
class in Ruby on Rails. The gsub()
method on Ruby strings
matches regular expressions anywhere in the input. However, nearly all of
the actual patterns used in this module use $
at the end to
match the end of the input string (so that only the last word in a multiple
word phrase will be singularized or pluralized). Therefore, the Java versions
of the regular expressions have been modified to capture all text before the
interesting characters at the end, and emit them as part of the result, so
that the entire string can be matched against a pattern once.
Modifier and Type | Method and Description |
---|---|
void |
addIrregular(String singular,
String plural)
Add the addSingular and addPlural forms of words that cannot be
converted using the normal rules.
|
void |
addPlural(String match,
String rule)
Add a match pattern and replacement rule for converting addPlural
forms to addSingular forms.
|
void |
addPlural(String match,
String rule,
boolean insensitive)
Add a match pattern and replacement rule for converting addPlural
forms to addSingular forms.
|
void |
addSingular(String match,
String rule)
Add a match pattern and replacement rule for converting addSingular
forms to addPlural forms.
|
void |
addSingular(String match,
String rule,
boolean insensitive)
Add a match pattern and replacement rule for converting addSingular
forms to addPlural forms.
|
void |
addUncountable(String word)
Add a word that cannot be converted between addSingular and addPlural.
|
String |
camelize(String word)
Convert strings to
EmbeddedCamelCase . |
String |
camelize(String word,
boolean flag)
Convert word strings consisting of lower case letters and
underscore characters between words into
embeddedCamelCase
or EmbeddedCamelCase , depending on the lower
flag. |
String |
classify(String tableName)
Create and return a simple class name that corresponds to a
addPlural table name.
|
String |
dasherize(String word)
Replace underscores in the specified word with dashes.
|
String |
decapitalize(String word) |
String |
demodulize(String className)
Remove any package name from a fully qualified class name,
returning only the simple classname.
|
String |
foreignKey(String className)
Create and return a foreign key name from a class name,
separating the "id" suffix with an underscore.
|
String |
foreignKey(String className,
boolean underscore)
Create and return a foreign key name from a class name,
optionally inserting an underscore before the "id" portion.
|
static Inflector |
getInstance()
Return a fully configured
Inflector instance that can be used
for performing transformations. |
String |
humanize(String words)
Capitalize the first word in a lower cased and underscored string,
turn underscores into spaces, and string any trailing "_id".
|
String |
ordinalize(int number)
Turn a number into a corresponding ordinal string used to
denote the position in an ordered sequence.
|
String |
pluralize(String word)
Return a addPlural version of the specified (addSingular) word.
|
String |
singularize(String word)
Return a addSingular version of the specified (addPlural) word.
|
String |
tableize(String className)
Convert the simple name of a model class into the corresponding
name of a database table, by uncamelizing, inserting underscores,
and pluralizing the last word.
|
String |
titleize(String words)
Capitalize all the words, and replace some characters in the string
to create a nicer looking title.
|
String |
underscore(String word)
The reverse of
camelize() , makes an underscored form
from the expression in the string. |
public static Inflector getInstance()
Return a fully configured Inflector
instance that can be used
for performing transformations.
public String camelize(String word)
Convert strings to EmbeddedCamelCase
. Embedded
underscores will be removed.
word
- Word to be convertedpublic String camelize(String word, boolean flag)
Convert word strings consisting of lower case letters and
underscore characters between words into embeddedCamelCase
or EmbeddedCamelCase
, depending on the lower
flag. Embedded underscores will be removed. Embedded '/'
characters will be replaced by '.', making this method useful
in converting path-like names into fully qualified classnames.
IMPLEMENTATION DIFFERENCE - The Rails version of this method also converts '/' characters to '::' because that reflects the normal syntax for fully qualified names in Ruby.
Input | Output |
---|---|
"foo_bar", false | "FooBar" |
"foo_bar", true | "fooBar" |
"foo_bar/baz", false | "FooBar.Baz" |
"foo_bar/baz", true | "fooBar.Baz" |
word
- Word to be convertedflag
- Flag indicating that the initial character should
be lower cased instead of upper casedpublic String classify(String tableName)
Create and return a simple class name that corresponds to a addPlural table name. Any leading schema name will be trimmed.
Input | Output |
---|---|
"foo_bars" | "FooBar" |
"baz" | "Baz" |
tableName
- Table name to be convertedpublic String dasherize(String word)
Replace underscores in the specified word with dashes.
Input | Output |
---|---|
"foo_bar" | "foo-bar" |
"baz" | "baz" |
word
- Word to be convertedpublic String demodulize(String className)
Remove any package name from a fully qualified class name, returning only the simple classname.
Input | Output |
---|---|
"java.util.Map" | "Map" |
"String" | "String" |
className
- Fully qualified class name to be convertedpublic String foreignKey(String className)
Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.
public String foreignKey(String className, boolean underscore)
Create and return a foreign key name from a class name, optionally inserting an underscore before the "id" portion.
Input | Output |
---|---|
"com.mymodel.Order", false | "orderid" |
"com.mymodel.Order", true | "order_id" |
"Message", false | "messageid" |
"Message", true | "message_id" |
className
- Class name for which to create a foreign keyunderscore
- Flag indicating whether an underscore should
be emitted between the class name and the "id" suffixpublic String humanize(String words)
Capitalize the first word in a lower cased and underscored string,
turn underscores into spaces, and string any trailing "_id". Like
titleize()
, this is meant for creating pretty output,
and is not intended for code generation.
Input | Output |
---|---|
"employee_salary" | "Employee salary" |
"author_id" | "Author" |
words
- Word string to be convertedpublic String ordinalize(int number)
Turn a number into a corresponding ordinal string used to denote the position in an ordered sequence.
Input | Output |
---|---|
1 | "1st" |
2 | "2nd" |
3 | "3rd" |
4 | "rth" |
1002 | "1002nd" |
2012 | "2012th" |
number
- Number to be convertedpublic String pluralize(String word)
Return a addPlural version of the specified (addSingular) word.
word
- Singular word to be convertedpublic String singularize(String word)
Return a addSingular version of the specified (addPlural) word.
word
- Plural word to be convertedpublic String tableize(String className)
Convert the simple name of a model class into the corresponding name of a database table, by uncamelizing, inserting underscores, and pluralizing the last word.
Input | Output |
---|---|
"RawScaledScorer" | "raw_scaled_scorers" |
"fancyCategory" | "fancy_categories" |
className
- Class name to be convertedpublic String titleize(String words)
Capitalize all the words, and replace some characters in the string to create a nicer looking title. This is meant for creating pretty output, and is not intended for code generation.
Input | Output |
---|---|
"the honeymooners" | "The Honeymooners" |
"x-men: the last stand" | "X Men: The Last Stand" |
words
- Word string to be convertedpublic String underscore(String word)
The reverse of camelize()
, makes an underscored form
from the expression in the string. Changes "." to "/" to convert
fully qualified class names into paths.
Input | Output |
---|---|
"FooBar" | "foo_bar" |
"fooBar" | "foo_bar" |
"FooBar.Baz" | "foo_bar/baz" |
"FooBar.Baz" | "foo_bar/baz" |
word
- Camel cased word to be convertedpublic void addIrregular(String singular, String plural)
Add the addSingular and addPlural forms of words that cannot be converted using the normal rules.
singular
- Singular form of the wordplural
- Plural form of the wordpublic void addPlural(String match, String rule)
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms. By default, matches will be case insensitive.
match
- Match pattern regular expressionrule
- Replacement rulepublic void addPlural(String match, String rule, boolean insensitive)
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitivepublic void addSingular(String match, String rule)
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms. By default, matches will be case insensitive.
match
- Match pattern regular expressionrule
- Replacement rulepublic void addSingular(String match, String rule, boolean insensitive)
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms.
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitivepublic void addUncountable(String word)
Add a word that cannot be converted between addSingular and addPlural.
word
- Word to be addedCopyright © 2013 Oracle Corporation. All rights reserved.