* 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 {@link Object#toString()} is called on the supplied object, so this method works for non-strings, too. *
* * @param word the word that is to be pluralized. * @return the pluralized form of the word, or the word itself if it could not be pluralized * @see #singularize(Object) */ public String pluralize( Object word ) { if (word == null) return null; String wordStr = word.toString().trim(); if (wordStr.length() == 0) return wordStr; if (isUncountable(wordStr)) return wordStr; for (Rule rule : this.plurals) { String result = rule.apply(wordStr); if (result != null) return result; } return wordStr; } public String pluralize( Object word, int count ) { if (word == null) return null; if (count == 1 || count == -1) { return word.toString(); } return pluralize(word); } /** * Returns the singular form of the word in the string. ** 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 {@link Object#toString()} is called on the supplied object, so this method works for non-strings, too. *
* * @param word the word that is to be pluralized. * @return the pluralized form of the word, or the word itself if it could not be pluralized * @see #pluralize(Object) */ public String singularize( Object word ) { if (word == null) return null; String wordStr = word.toString().trim(); if (wordStr.length() == 0) return wordStr; if (isUncountable(wordStr)) return wordStr; for (Rule rule : this.singulars) { String result = rule.apply(wordStr); if (result != null) return result; } return wordStr; } /** * Converts strings to lowerCamelCase. This method will also use any extra delimiter characters to identify word boundaries. ** Examples: * *
* inflector.lowerCamelCase("active_record") #=> "activeRecord" * inflector.lowerCamelCase("first_name") #=> "firstName" * inflector.lowerCamelCase("name") #=> "name" * inflector.lowerCamelCase("the-first_name",'-') #=> "theFirstName" ** * * * @param lowerCaseAndUnderscoredWord the word that is to be converted to camel case * @param delimiterChars optional characters that are used to delimit word boundaries * @return the lower camel case version of the word * @see #underscore(String, char[]) * @see #camelCase(String, boolean, char[]) * @see #upperCamelCase(String, char[]) */ public String lowerCamelCase( String lowerCaseAndUnderscoredWord, char... delimiterChars ) { return camelCase(lowerCaseAndUnderscoredWord, false, delimiterChars); } /** * Converts strings to UpperCamelCase. This method will also use any extra delimiter characters to identify word boundaries. *
* Examples: * *
* inflector.upperCamelCase("active_record") #=> "SctiveRecord" * inflector.upperCamelCase("first_name") #=> "FirstName" * inflector.upperCamelCase("name") #=> "Name" * inflector.lowerCamelCase("the-first_name",'-') #=> "TheFirstName" ** * * * @param lowerCaseAndUnderscoredWord the word that is to be converted to camel case * @param delimiterChars optional characters that are used to delimit word boundaries * @return the upper camel case version of the word * @see #underscore(String, char[]) * @see #camelCase(String, boolean, char[]) * @see #lowerCamelCase(String, char[]) */ public String upperCamelCase( String lowerCaseAndUnderscoredWord, char... delimiterChars ) { return camelCase(lowerCaseAndUnderscoredWord, true, delimiterChars); } /** * By default, this method converts strings to UpperCamelCase. If the
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" ** * * * @param lowerCaseAndUnderscoredWord the word that is to be converted to camel case * @param uppercaseFirstLetter true if the first character is to be uppercased, or false if the first character is to be * lowercased * @param delimiterChars optional characters that are used to delimit word boundaries * @return the camel case version of the word * @see #underscore(String, char[]) * @see #upperCamelCase(String, char[]) * @see #lowerCamelCase(String, char[]) */ public String camelCase( String lowerCaseAndUnderscoredWord, boolean uppercaseFirstLetter, char... delimiterChars ) { if (lowerCaseAndUnderscoredWord == null) return null; lowerCaseAndUnderscoredWord = lowerCaseAndUnderscoredWord.trim(); if (lowerCaseAndUnderscoredWord.length() == 0) return ""; if (uppercaseFirstLetter) { String result = lowerCaseAndUnderscoredWord; // Replace any extra delimiters with underscores (before the underscores are converted in the next step)... if (delimiterChars != null) { for (char delimiterChar : delimiterChars) { result = result.replace(delimiterChar, '_'); } } // Change the case at the beginning at after each underscore ... return replaceAllWithUppercase(result, "(^|_)(.)", 2); } if (lowerCaseAndUnderscoredWord.length() < 2) return lowerCaseAndUnderscoredWord; return "" + Character.toLowerCase(lowerCaseAndUnderscoredWord.charAt(0)) + camelCase(lowerCaseAndUnderscoredWord, true, delimiterChars).substring(1); } /** * Makes an underscored form from the expression in the string (the reverse of the {@link #camelCase(String, boolean, char[]) * 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" ** * * * @param camelCaseWord the camel-cased word that is to be converted; * @param delimiterChars optional characters that are used to delimit word boundaries (beyond capitalization) * @return a lower-cased version of the input, with separate words delimited by the underscore character. */ public String underscore( String camelCaseWord, char... delimiterChars ) { if (camelCaseWord == null) return null; String result = camelCaseWord.trim(); if (result.length() == 0) return ""; result = result.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2"); result = result.replaceAll("([a-z\\d])([A-Z])", "$1_$2"); result = result.replace('-', '_'); if (delimiterChars != null) { for (char delimiterChar : delimiterChars) { result = result.replace(delimiterChar, '_'); } } return result.toLowerCase(); } /** * Returns a copy of the input with the first character converted to uppercase and the remainder to lowercase. * * @param words the word to be capitalized * @return the string with the first character capitalized and the remaining characters lowercased */ public String capitalize( String words ) { if (words == null) return null; String result = words.trim(); if (result.length() == 0) return ""; if (result.length() == 1) return result.toUpperCase(); return "" + Character.toUpperCase(result.charAt(0)) + result.substring(1).toLowerCase(); } /** * Capitalizes the first word and turns underscores into spaces and strips trailing "_id" and any supplied removable tokens. * Like {@link #titleCase(String, String[])}, this is meant for creating pretty output. *
* Examples: * *
* inflector.humanize("employee_salary") #=> "Employee salary" * inflector.humanize("author_id") #=> "Author" ** * * * @param lowerCaseAndUnderscoredWords the input to be humanized * @param removableTokens optional array of tokens that are to be removed * @return the humanized string * @see #titleCase(String, String[]) */ public String humanize( String lowerCaseAndUnderscoredWords, String... removableTokens ) { if (lowerCaseAndUnderscoredWords == null) return null; String result = lowerCaseAndUnderscoredWords.trim(); if (result.length() == 0) return ""; // Remove a trailing "_id" token result = result.replaceAll("_id$", ""); // Remove all of the tokens that should be removed if (removableTokens != null) { for (String removableToken : removableTokens) { result = result.replaceAll(removableToken, ""); } } result = result.replaceAll("_+", " "); // replace all adjacent underscores with a single space return capitalize(result); } /** * Capitalizes all the words and replaces some characters in the string to create a nicer looking title. Underscores are * changed to spaces, a trailing "_id" is removed, and any of the supplied tokens are removed. Like * {@link #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" ** * * * @param words the input to be turned into title case * @param removableTokens optional array of tokens that are to be removed * @return the title-case version of the supplied words */ public String titleCase( String words, String... removableTokens ) { String result = humanize(words, removableTokens); result = replaceAllWithUppercase(result, "\\b([a-z])", 1); // change first char of each word to uppercase return result; } /** * Turns a non-negative number into an ordinal string used to denote the position in an ordered sequence, such as 1st, 2nd, * 3rd, 4th. * * @param number the non-negative number * @return the string with the number and ordinal suffix */ public String ordinalize( int number ) { int remainder = number % 100; String numberStr = Integer.toString(number); if (11 <= number && number <= 13) return numberStr + "th"; remainder = number % 10; if (remainder == 1) return numberStr + "st"; if (remainder == 2) return numberStr + "nd"; if (remainder == 3) return numberStr + "rd"; return numberStr + "th"; } // ------------------------------------------------------------------------------------------------ // Management methods // ------------------------------------------------------------------------------------------------ /** * Determine whether the supplied word is considered uncountable by the {@link #pluralize(Object) pluralize} and * {@link #singularize(Object) singularize} methods. * * @param word the word * @return true if the plural and singular forms of the word are the same */ public boolean isUncountable( String word ) { if (word == null) return false; String trimmedLower = word.trim().toLowerCase(); return this.uncountables.contains(trimmedLower); } /** * Get the set of words that are not processed by the Inflector. The resulting map is directly modifiable. * * @return the set of uncountable words */ public Set
* The Java {@link Pattern 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.
*