Class NounInflector
- java.lang.Object
-
- org.glassfish.jersey.jaxb.internal.NounInflector
-
final class NounInflector extends java.lang.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 theaddIrregular()
,addPlural()
,addSingular()
, andaddUncountable()
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. Thegsub()
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 Classes Modifier and Type Class Description private static class
NounInflector.Replacer
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
Fields Modifier and Type Field Description private static NounInflector
instance
The singleton instance returned by the defaultgetInstance()
method.private java.util.List<NounInflector.Replacer>
plurals
List ofReplacer
s for performing replacement operations on matches for plural words.private java.util.List<NounInflector.Replacer>
singulars
List ofReplacer
s for performing replacement operations on matches for addSingular words.private java.util.List<java.lang.String>
uncountables
List of words that represent addUncountable concepts that cannot be pluralized or singularized.
-
Constructor Summary
Constructors Modifier Constructor Description private
NounInflector()
Private constructor to avoid instantiation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIrregular(java.lang.String singular, java.lang.String plural)
Add the addSingular and addPlural forms of words that cannot be converted using the normal rules.void
addPlural(java.lang.String match, java.lang.String rule)
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.void
addPlural(java.lang.String match, java.lang.String rule, boolean insensitive)
Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.void
addSingular(java.lang.String match, java.lang.String rule)
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms.void
addSingular(java.lang.String match, java.lang.String rule, boolean insensitive)
Add a match pattern and replacement rule for converting addSingular forms to addPlural forms.void
addUncountable(java.lang.String word)
Add a word that cannot be converted between addSingular and addPlural.java.lang.String
camelize(java.lang.String word)
Convert strings toEmbeddedCamelCase
.java.lang.String
camelize(java.lang.String word, boolean flag)
Convert word strings consisting of lower case letters and underscore characters between words intoembeddedCamelCase
orEmbeddedCamelCase
, depending on thelower
flag.java.lang.String
classify(java.lang.String tableName)
Create and return a simple class name that corresponds to a addPlural table name.java.lang.String
dasherize(java.lang.String word)
Replace underscores in the specified word with dashes.java.lang.String
decapitalize(java.lang.String word)
java.lang.String
demodulize(java.lang.String className)
Remove any package name from a fully qualified class name, returning only the simple classname.java.lang.String
foreignKey(java.lang.String className)
Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.java.lang.String
foreignKey(java.lang.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
getInstance()
Return a fully configuredNounInflector
instance that can be used for performing transformations.java.lang.String
humanize(java.lang.String words)
Capitalize the first word in a lower cased and underscored string, turn underscores into spaces, and string any trailing "_id".java.lang.String
ordinalize(int number)
Turn a number into a corresponding ordinal string used to denote the position in an ordered sequence.java.lang.String
pluralize(java.lang.String word)
Return a addPlural version of the specified (addSingular) word.java.lang.String
singularize(java.lang.String word)
Return a addSingular version of the specified (addPlural) word.java.lang.String
tableize(java.lang.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.java.lang.String
titleize(java.lang.String words)
Capitalize all the words, and replace some characters in the string to create a nicer looking title.java.lang.String
underscore(java.lang.String word)
The reverse ofcamelize()
, makes an underscored form from the expression in the string.
-
-
-
Field Detail
-
instance
private static transient NounInflector instance
The singleton instance returned by the default
getInstance()
method.
-
plurals
private final java.util.List<NounInflector.Replacer> plurals
List of
Replacer
s for performing replacement operations on matches for plural words.
-
singulars
private final java.util.List<NounInflector.Replacer> singulars
List of
Replacer
s for performing replacement operations on matches for addSingular words.
-
uncountables
private final java.util.List<java.lang.String> uncountables
List of words that represent addUncountable concepts that cannot be pluralized or singularized.
-
-
Method Detail
-
getInstance
public static NounInflector getInstance()
Return a fully configured
NounInflector
instance that can be used for performing transformations.
-
camelize
public java.lang.String camelize(java.lang.String word)
Convert strings to
EmbeddedCamelCase
. Embedded underscores will be removed.- Parameters:
word
- Word to be converted
-
camelize
public java.lang.String camelize(java.lang.String word, boolean flag)
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
public java.lang.String classify(java.lang.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" - Parameters:
tableName
- Table name to be converted
-
dasherize
public java.lang.String dasherize(java.lang.String word)
Replace underscores in the specified word with dashes.
Input Output "foo_bar" "foo-bar" "baz" "baz" - Parameters:
word
- Word to be converted
-
demodulize
public java.lang.String demodulize(java.lang.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" - Parameters:
className
- Fully qualified class name to be converted
-
foreignKey
public java.lang.String foreignKey(java.lang.String className)
Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.
-
foreignKey
public java.lang.String foreignKey(java.lang.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" - 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
public java.lang.String humanize(java.lang.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" - Parameters:
words
- Word string to be converted
-
ordinalize
public java.lang.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" - Parameters:
number
- Number to be converted
-
pluralize
public java.lang.String pluralize(java.lang.String word)
Return a addPlural version of the specified (addSingular) word.
- Parameters:
word
- Singular word to be converted
-
singularize
public java.lang.String singularize(java.lang.String word)
Return a addSingular version of the specified (addPlural) word.
- Parameters:
word
- Plural word to be converted
-
tableize
public java.lang.String tableize(java.lang.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" - Parameters:
className
- Class name to be converted
-
titleize
public java.lang.String titleize(java.lang.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" - Parameters:
words
- Word string to be converted
-
decapitalize
public java.lang.String decapitalize(java.lang.String word)
-
underscore
public java.lang.String underscore(java.lang.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" - Parameters:
word
- Camel cased word to be converted
-
addIrregular
public void addIrregular(java.lang.String singular, java.lang.String plural)
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
public void addPlural(java.lang.String match, java.lang.String rule)
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
public void addPlural(java.lang.String match, java.lang.String rule, boolean insensitive)
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
public void addSingular(java.lang.String match, java.lang.String rule)
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
public void addSingular(java.lang.String match, java.lang.String rule, boolean insensitive)
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
public void addUncountable(java.lang.String word)
Add a word that cannot be converted between addSingular and addPlural.
- Parameters:
word
- Word to be added
-
-