|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sun.jersey.core.impl.provider.entity.Inflector
public class Inflector
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.
Method Summary | |
---|---|
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 to EmbeddedCamelCase . |
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
or EmbeddedCamelCase , depending on the lower
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 Inflector |
getInstance()
Return a fully configured Inflector 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 of camelize() , makes an underscored form
from the expression in the string. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static Inflector getInstance()
Return a fully configured Inflector
instance that can be used
for performing transformations.
public java.lang.String camelize(java.lang.String word)
Convert strings to EmbeddedCamelCase
. Embedded
underscores will be removed.
word
- Word to be convertedpublic 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
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 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" |
tableName
- Table name to be convertedpublic java.lang.String dasherize(java.lang.String word)
Replace underscores in the specified word with dashes.
Input | Output |
---|---|
"foo_bar" | "foo-bar" |
"baz" | "baz" |
word
- Word to be convertedpublic 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" |
className
- Fully qualified class name to be convertedpublic 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.
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" |
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 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" |
words
- Word string to be convertedpublic 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" |
number
- Number to be convertedpublic java.lang.String pluralize(java.lang.String word)
Return a addPlural version of the specified (addSingular) word.
word
- Singular word to be convertedpublic java.lang.String singularize(java.lang.String word)
Return a addSingular version of the specified (addPlural) word.
word
- Plural word to be convertedpublic 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" |
className
- Class name to be convertedpublic 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" |
words
- Word string to be convertedpublic java.lang.String decapitalize(java.lang.String word)
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" |
word
- Camel cased word to be convertedpublic 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.
singular
- Singular form of the wordplural
- Plural form of the wordpublic 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.
match
- Match pattern regular expressionrule
- Replacement rulepublic 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.
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitivepublic 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.
match
- Match pattern regular expressionrule
- Replacement rulepublic 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.
match
- Match pattern regular expressionrule
- Replacement ruleinsensitive
- Flag indicating this match should be case insensitivepublic void addUncountable(java.lang.String word)
Add a word that cannot be converted between addSingular and addPlural.
word
- Word to be added
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |