@ThreadSafe public class Inflector extends Object
Modifier and Type | Class and Description |
---|---|
protected class |
Inflector.Rule |
Modifier | Constructor and Description |
---|---|
|
Inflector() |
protected |
Inflector(Inflector original) |
Modifier and Type | Method and Description |
---|---|
void |
addIrregular(String singular,
String plural) |
void |
addPluralize(String rule,
String replacement) |
void |
addSingularize(String rule,
String replacement) |
void |
addUncountable(String... words) |
String |
camelCase(String lowerCaseAndUnderscoredWord,
boolean uppercaseFirstLetter,
char... delimiterChars)
By default, this method converts strings to UpperCamelCase.
|
String |
capitalize(String words)
Returns a copy of the input with the first character converted to uppercase and the remainder to lowercase.
|
void |
clear()
Completely remove all rules within this inflector.
|
Inflector |
clone() |
static Inflector |
getInstance() |
Set<String> |
getUncountables()
Get the set of words that are not processed by the Inflector.
|
String |
humanize(String lowerCaseAndUnderscoredWords,
String... removableTokens)
Capitalizes the first word and turns underscores into spaces and strips trailing "_id" and any supplied removable tokens.
|
protected void |
initialize() |
boolean |
isUncountable(String word)
Determine whether the supplied word is considered uncountable by the
pluralize and
singularize methods. |
String |
lowerCamelCase(String lowerCaseAndUnderscoredWord,
char... delimiterChars)
Converts strings to lowerCamelCase.
|
String |
ordinalize(int number)
Turns a non-negative number into an ordinal string used to denote the position in an ordered sequence, such as 1st, 2nd,
3rd, 4th.
|
String |
pluralize(Object word)
Returns the plural form of the word in the string.
|
String |
pluralize(Object word,
int count) |
protected static String |
replaceAllWithUppercase(String input,
String regex,
int groupNumberToUppercase)
Utility method to replace all occurrences given by the specific backreference with its uppercased form, and remove all
other backreferences.
|
String |
singularize(Object word)
Returns the singular form of the word in the string.
|
String |
titleCase(String words,
String... removableTokens)
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
|
String |
underscore(String camelCaseWord,
char... delimiterChars)
Makes an underscored form from the expression in the string (the reverse of the
camelCase method. |
String |
upperCamelCase(String lowerCaseAndUnderscoredWord,
char... delimiterChars)
Converts strings to UpperCamelCase.
|
protected static final Inflector INSTANCE
public Inflector()
protected Inflector(Inflector original)
public static final Inflector getInstance()
public String pluralize(Object word)
Examples:
inflector.pluralize("post") #=> "posts" inflector.pluralize("octopus") #=> "octopi" inflector.pluralize("sheep") #=> "sheep" inflector.pluralize("words") #=> "words" inflector.pluralize("the blue mailman") #=> "the blue mailmen" inflector.pluralize("CamelOctopus") #=> "CamelOctopi"
Note that if the Object.toString()
is called on the supplied object, so this method works for non-strings, too.
word
- the word that is to be pluralized.singularize(Object)
public String singularize(Object word)
Examples:
inflector.singularize("posts") #=> "post" inflector.singularize("octopi") #=> "octopus" inflector.singularize("sheep") #=> "sheep" inflector.singularize("words") #=> "word" inflector.singularize("the blue mailmen") #=> "the blue mailman" inflector.singularize("CamelOctopi") #=> "CamelOctopus"
Note that if the Object.toString()
is called on the supplied object, so this method works for non-strings, too.
word
- the word that is to be pluralized.pluralize(Object)
public String lowerCamelCase(String lowerCaseAndUnderscoredWord, char... delimiterChars)
Examples:
inflector.lowerCamelCase("active_record") #=> "activeRecord" inflector.lowerCamelCase("first_name") #=> "firstName" inflector.lowerCamelCase("name") #=> "name" inflector.lowerCamelCase("the-first_name",'-') #=> "theFirstName"
lowerCaseAndUnderscoredWord
- the word that is to be converted to camel casedelimiterChars
- optional characters that are used to delimit word boundariesunderscore(String, char[])
,
camelCase(String, boolean, char[])
,
upperCamelCase(String, char[])
public String upperCamelCase(String lowerCaseAndUnderscoredWord, char... delimiterChars)
Examples:
inflector.upperCamelCase("active_record") #=> "SctiveRecord" inflector.upperCamelCase("first_name") #=> "FirstName" inflector.upperCamelCase("name") #=> "Name" inflector.lowerCamelCase("the-first_name",'-') #=> "TheFirstName"
lowerCaseAndUnderscoredWord
- the word that is to be converted to camel casedelimiterChars
- optional characters that are used to delimit word boundariesunderscore(String, char[])
,
camelCase(String, boolean, char[])
,
lowerCamelCase(String, char[])
public String camelCase(String lowerCaseAndUnderscoredWord, boolean uppercaseFirstLetter, char... delimiterChars)
uppercaseFirstLetter
argument to false,
then this method produces lowerCamelCase. This method will also use any extra delimiter characters to identify word
boundaries.
Examples:
inflector.camelCase("active_record",false) #=> "activeRecord" inflector.camelCase("active_record",true) #=> "ActiveRecord" inflector.camelCase("first_name",false) #=> "firstName" inflector.camelCase("first_name",true) #=> "FirstName" inflector.camelCase("name",false) #=> "name" inflector.camelCase("name",true) #=> "Name"
lowerCaseAndUnderscoredWord
- the word that is to be converted to camel caseuppercaseFirstLetter
- true if the first character is to be uppercased, or false if the first character is to be
lowercaseddelimiterChars
- optional characters that are used to delimit word boundariesunderscore(String, char[])
,
upperCamelCase(String, char[])
,
lowerCamelCase(String, char[])
public String underscore(String camelCaseWord, char... delimiterChars)
camelCase
method. Also changes any characters that match the supplied delimiters into underscore.
Examples:
inflector.underscore("activeRecord") #=> "active_record" inflector.underscore("ActiveRecord") #=> "active_record" inflector.underscore("firstName") #=> "first_name" inflector.underscore("FirstName") #=> "first_name" inflector.underscore("name") #=> "name" inflector.underscore("The.firstName") #=> "the_first_name"
camelCaseWord
- the camel-cased word that is to be converted;delimiterChars
- optional characters that are used to delimit word boundaries (beyond capitalization)public String capitalize(String words)
words
- the word to be capitalizedpublic String humanize(String lowerCaseAndUnderscoredWords, String... removableTokens)
titleCase(String, String[])
, this is meant for creating pretty output.
Examples:
inflector.humanize("employee_salary") #=> "Employee salary" inflector.humanize("author_id") #=> "Author"
lowerCaseAndUnderscoredWords
- the input to be humanizedremovableTokens
- optional array of tokens that are to be removedtitleCase(String, String[])
public String titleCase(String words, String... removableTokens)
humanize(String, String[])
, this is meant for creating pretty output.
Examples:
inflector.titleCase("man from the boondocks") #=> "Man From The Boondocks" inflector.titleCase("x-men: the last stand") #=> "X Men: The Last Stand"
words
- the input to be turned into title caseremovableTokens
- optional array of tokens that are to be removedpublic String ordinalize(int number)
number
- the non-negative numberpublic boolean isUncountable(String word)
pluralize
and
singularize
methods.word
- the wordpublic Set<String> getUncountables()
public void addUncountable(String... words)
protected static String replaceAllWithUppercase(String input, String regex, int groupNumberToUppercase)
The Java regular expression processing
does not use the preprocessing directives \l
,
\u
, \L
, and \U
. If so, such directives could be used in the replacement string
to uppercase or lowercase the backreferences. For example, \L1
would lowercase the first backreference, and
\u3
would uppercase the 3rd backreference.
input
- regex
- groupNumberToUppercase
- public void clear()
protected void initialize()
Copyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.