Package org.hl7.fhir.utilities
Class Inflector
- java.lang.Object
-
- org.hl7.fhir.utilities.Inflector
-
public class Inflector extends Object
Transforms words to singular, plural, humanized (human readable), underscore, camel case, or ordinal form. This is inspired by the Inflector class in Ruby on Rails, which is distributed under the Rails license.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classInflector.Rule
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddIrregular(String singular, String plural)voidaddPluralize(String rule, String replacement)voidaddSingularize(String rule, String replacement)voidaddUncountable(String... words)StringcamelCase(String lowerCaseAndUnderscoredWord, boolean uppercaseFirstLetter, char... delimiterChars)By default, this method converts strings to UpperCamelCase.Stringcapitalize(String words)Returns a copy of the input with the first character converted to uppercase and the remainder to lowercase.voidclear()Completely remove all rules within this inflector.Inflectorclone()static InflectorgetInstance()Set<String>getUncountables()Get the set of words that are not processed by the Inflector.Stringhumanize(String lowerCaseAndUnderscoredWords, String... removableTokens)Capitalizes the first word and turns underscores into spaces and strips trailing "_id" and any supplied removable tokens.protected voidinitialize()booleanisUncountable(String word)Determine whether the supplied word is considered uncountable by thepluralizeandsingularizemethods.StringlowerCamelCase(String lowerCaseAndUnderscoredWord, char... delimiterChars)Converts strings to lowerCamelCase.Stringordinalize(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.Stringpluralize(Object word)Returns the plural form of the word in the string.Stringpluralize(Object word, int count)protected static StringreplaceAllWithUppercase(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.Stringsingularize(Object word)Returns the singular form of the word in the string.StringtitleCase(String words, String... removableTokens)Capitalizes all the words and replaces some characters in the string to create a nicer looking title.Stringunderscore(String camelCaseWord, char... delimiterChars)Makes an underscored form from the expression in the string (the reverse of thecamelCasemethod.StringupperCamelCase(String lowerCaseAndUnderscoredWord, char... delimiterChars)Converts strings to UpperCamelCase.
-
-
-
Method Detail
-
getInstance
public static final Inflector getInstance()
-
pluralize
public String pluralize(Object word)
Returns the plural form of the word in the string. 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 theObject.toString()is called on the supplied object, so this method works for non-strings, too.- Parameters:
word- the word that is to be pluralized.- Returns:
- the pluralized form of the word, or the word itself if it could not be pluralized
- See Also:
singularize(Object)
-
singularize
public String singularize(Object 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 theObject.toString()is called on the supplied object, so this method works for non-strings, too.- Parameters:
word- the word that is to be pluralized.- Returns:
- the pluralized form of the word, or the word itself if it could not be pluralized
- See Also:
pluralize(Object)
-
lowerCamelCase
public String lowerCamelCase(String lowerCaseAndUnderscoredWord, char... delimiterChars)
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"- Parameters:
lowerCaseAndUnderscoredWord- the word that is to be converted to camel casedelimiterChars- optional characters that are used to delimit word boundaries- Returns:
- the lower camel case version of the word
- See Also:
underscore(String, char[]),camelCase(String, boolean, char[]),upperCamelCase(String, char[])
-
upperCamelCase
public String upperCamelCase(String lowerCaseAndUnderscoredWord, char... 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"- Parameters:
lowerCaseAndUnderscoredWord- the word that is to be converted to camel casedelimiterChars- optional characters that are used to delimit word boundaries- Returns:
- the upper camel case version of the word
- See Also:
underscore(String, char[]),camelCase(String, boolean, char[]),lowerCamelCase(String, char[])
-
camelCase
public String camelCase(String lowerCaseAndUnderscoredWord, boolean uppercaseFirstLetter, char... delimiterChars)
By default, this method converts strings to UpperCamelCase. If theuppercaseFirstLetterargument 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"- Parameters:
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 boundaries- Returns:
- the camel case version of the word
- See Also:
underscore(String, char[]),upperCamelCase(String, char[]),lowerCamelCase(String, char[])
-
underscore
public String underscore(String camelCaseWord, char... delimiterChars)
Makes an underscored form from the expression in the string (the reverse of thecamelCasemethod. 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"- Parameters:
camelCaseWord- the camel-cased word that is to be converted;delimiterChars- optional characters that are used to delimit word boundaries (beyond capitalization)- Returns:
- a lower-cased version of the input, with separate words delimited by the underscore character.
-
capitalize
public String capitalize(String words)
Returns a copy of the input with the first character converted to uppercase and the remainder to lowercase.- Parameters:
words- the word to be capitalized- Returns:
- the string with the first character capitalized and the remaining characters lowercased
-
humanize
public String humanize(String lowerCaseAndUnderscoredWords, String... removableTokens)
Capitalizes the first word and turns underscores into spaces and strips trailing "_id" and any supplied removable tokens. LiketitleCase(String, String[]), this is meant for creating pretty output. Examples:inflector.humanize("employee_salary") #=> "Employee salary" inflector.humanize("author_id") #=> "Author"- Parameters:
lowerCaseAndUnderscoredWords- the input to be humanizedremovableTokens- optional array of tokens that are to be removed- Returns:
- the humanized string
- See Also:
titleCase(String, String[])
-
titleCase
public String titleCase(String words, String... removableTokens)
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. Likehumanize(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"- Parameters:
words- the input to be turned into title caseremovableTokens- optional array of tokens that are to be removed- Returns:
- the title-case version of the supplied words
-
ordinalize
public 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.- Parameters:
number- the non-negative number- Returns:
- the string with the number and ordinal suffix
-
isUncountable
public boolean isUncountable(String word)
Determine whether the supplied word is considered uncountable by thepluralizeandsingularizemethods.- Parameters:
word- the word- Returns:
- true if the plural and singular forms of the word are the same
-
getUncountables
public Set<String> getUncountables()
Get the set of words that are not processed by the Inflector. The resulting map is directly modifiable.- Returns:
- the set of uncountable words
-
addPluralize
public void addPluralize(String rule, String replacement)
-
addSingularize
public void addSingularize(String rule, String replacement)
-
addIrregular
public void addIrregular(String singular, String plural)
-
addUncountable
public void addUncountable(String... words)
-
replaceAllWithUppercase
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. The Javaregular expression processingdoes 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,\L1would lowercase the first backreference, and\u3would uppercase the 3rd backreference.- Parameters:
input-regex-groupNumberToUppercase-- Returns:
- the input string with the appropriate characters converted to upper-case
-
clear
public void clear()
Completely remove all rules within this inflector.
-
initialize
protected void initialize()
-
-