com.sun.jersey.core.impl.provider.entity
Class Inflector

java.lang.Object
  extended by com.sun.jersey.core.impl.provider.entity.Inflector

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.

Author:
Florian Rosenberg

Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Inflector getInstance()

Return a fully configured Inflector instance that can be used for performing transformations.


camelize

public String camelize(String word)

Convert strings to EmbeddedCamelCase. Embedded underscores will be removed.

Parameters:
word - Word to be converted

camelize

public 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"

Parameters:
word - Word to be converted
flag - Flag indicating that the initial character should be lower cased instead of upper cased

classify

public 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"

Parameters:
tableName - Table name to be converted

dasherize

public String dasherize(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 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"

Parameters:
className - Fully qualified class name to be converted

foreignKey

public String foreignKey(String className)

Create and return a foreign key name from a class name, separating the "id" suffix with an underscore.


foreignKey

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"

Parameters:
className - Class name for which to create a foreign key
underscore - Flag indicating whether an underscore should be emitted between the class name and the "id" suffix

humanize

public 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"

Parameters:
words - Word string to be converted

ordinalize

public 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 String pluralize(String word)

Return a addPlural version of the specified (addSingular) word.

Parameters:
word - Singular word to be converted

singularize

public String singularize(String word)

Return a addSingular version of the specified (addPlural) word.

Parameters:
word - Plural word to be converted

tableize

public 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"

Parameters:
className - Class name to be converted

titleize

public 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"

Parameters:
words - Word string to be converted

decapitalize

public String decapitalize(String word)

underscore

public 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"

Parameters:
word - Camel cased word to be converted

addIrregular

public void addIrregular(String singular,
                         String plural)

Add the addSingular and addPlural forms of words that cannot be converted using the normal rules.

Parameters:
singular - Singular form of the word
plural - Plural form of the word

addPlural

public 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.

Parameters:
match - Match pattern regular expression
rule - Replacement rule

addPlural

public void addPlural(String match,
                      String rule,
                      boolean insensitive)

Add a match pattern and replacement rule for converting addPlural forms to addSingular forms.

Parameters:
match - Match pattern regular expression
rule - Replacement rule
insensitive - Flag indicating this match should be case insensitive

addSingular

public 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.

Parameters:
match - Match pattern regular expression
rule - Replacement rule

addSingular

public void addSingular(String match,
                        String rule,
                        boolean insensitive)

Add a match pattern and replacement rule for converting addSingular forms to addPlural forms.

Parameters:
match - Match pattern regular expression
rule - Replacement rule
insensitive - Flag indicating this match should be case insensitive

addUncountable

public void addUncountable(String word)

Add a word that cannot be converted between addSingular and addPlural.

Parameters:
word - Word to be added


Copyright © 2011 Oracle Corporation. All Rights Reserved.