Class NounInflector
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 component 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Internal class that uses a regular expression matcher to both match the specified regular expression to a specified word, and (if successful) perform the appropriate substitutions. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static NounInflector
The singleton instance returned by the defaultgetInstance()
method.private final List
<NounInflector.Replacer> List ofReplacer
s for performing replacement operations on matches for plural words.private final List
<NounInflector.Replacer> List ofReplacer
s for performing replacement operations on matches for addSingular words.List of words that represent addUncountable concepts that cannot be pluralized or singularized. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Private constructor to avoid instantiation. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addIrregular
(String singular, String plural) Add the addSingular and addPlural forms of words that cannot be converted using the normal rules.void
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.void
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.Convert strings toEmbeddedCamelCase
.Convert word strings consisting of lower case letters and underscore characters between words intoembeddedCamelCase
orEmbeddedCamelCase
, depending on thelower
flag.Create and return a simple class name that corresponds to a addPlural table name.Replace underscores in the specified word with dashes.decapitalize
(String word) demodulize
(String className) Remove any package name from a fully qualified class name, returning only the simple classname.foreignKey
(String className) Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.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 NounInflector
Return a fully configuredNounInflector
instance that can be used for performing transformations.Capitalize the first word in a lower cased and underscored string, turn underscores into spaces, and string any trailing "_id".ordinalize
(int number) Turn a number into a corresponding ordinal string used to denote the position in an ordered sequence.Return a addPlural version of the specified (addSingular) word.singularize
(String word) Return a addSingular version of the specified (addPlural) word.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.Capitalize all the words, and replace some characters in the string to create a nicer looking title.underscore
(String word) The reverse ofcamelize()
, makes an underscored form from the expression in the string.
-
Field Details
-
instance
The singleton instance returned by the default
getInstance()
method. -
plurals
List of
Replacer
s for performing replacement operations on matches for plural words. -
singulars
List of
Replacer
s for performing replacement operations on matches for addSingular words. -
uncountables
List of words that represent addUncountable concepts that cannot be pluralized or singularized.
-
-
Constructor Details
-
NounInflector
private NounInflector()Private constructor to avoid instantiation.
-
-
Method Details
-
getInstance
Return a fully configured
NounInflector
instance that can be used for performing transformations. -
camelize
Convert strings to
EmbeddedCamelCase
. Embedded underscores will be removed.- Parameters:
word
- Word to be converted
-
camelize
Convert word strings consisting of lower case letters and underscore characters between words into
embeddedCamelCase
orEmbeddedCamelCase
, depending on thelower
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" - Parameters:
word
- Word to be convertedflag
- Flag indicating that the initial character should be lower cased instead of upper cased
-
classify
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" - Parameters:
tableName
- Table name to be converted
-
dasherize
Replace underscores in the specified word with dashes.
Input Output "foo_bar" "foo-bar" "baz" "baz" - Parameters:
word
- Word to be converted
-
demodulize
Remove any package name from a fully qualified class name, returning only the simple classname.
Input Output "java.util.Map" "Map" "String" "String" - Parameters:
className
- Fully qualified class name to be converted
-
foreignKey
Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.
-
foreignKey
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" - Parameters:
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" suffix
-
humanize
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" - Parameters:
words
- Word string to be converted
-
ordinalize
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" - Parameters:
number
- Number to be converted
-
pluralize
Return a addPlural version of the specified (addSingular) word.
- Parameters:
word
- Singular word to be converted
-
singularize
Return a addSingular version of the specified (addPlural) word.
- Parameters:
word
- Plural word to be converted
-
tableize
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" - Parameters:
className
- Class name to be converted
-
titleize
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" - Parameters:
words
- Word string to be converted
-
decapitalize
-
underscore
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" - Parameters:
word
- Camel cased word to be converted
-
addIrregular
Add the addSingular and addPlural forms of words that cannot be converted using the normal rules.
- Parameters:
singular
- Singular form of the wordplural
- Plural form of the word
-
addPlural
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms. By default, matches will be case insensitive.
- Parameters:
match
- Match pattern regular expressionrule
- Replacement rule
-
addPlural
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.
- Parameters:
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitive
-
addSingular
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms. By default, matches will be case insensitive.
- Parameters:
match
- Match pattern regular expressionrule
- Replacement rule
-
addSingular
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms.
- Parameters:
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitive
-
addUncountable
Add a word that cannot be converted between addSingular and addPlural.
- Parameters:
word
- Word to be added
-