net.thucydides.core.util
Class Inflector

Package class diagram package Inflector
java.lang.Object
  extended by net.thucydides.core.util.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
protected  class Inflector.Rule
           
 
Field Summary
protected static Inflector INSTANCE
           
 
Constructor Summary
Inflector()
           
 
Method Summary
 void addIrregular(String singular, String plural)
           
 void addPluralize(String rule, String replacement)
           
 void addSingularize(String rule, String replacement)
           
 void addUncountable(String... words)
           
 String capitalize(String words)
          Returns a copy of the input with the first character converted to uppercase and the remainder to lowercase.
static Inflector getInstance()
           
 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.
 MultipleInflection of(int count)
           
 Inflection of(String word)
           
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

protected static final Inflector INSTANCE
Constructor Detail

Inflector

public Inflector()
Method Detail

getInstance

public static Inflector getInstance()

of

public Inflection of(String word)

of

public MultipleInflection of(int count)

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 the Object.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)

pluralize

public String pluralize(Object word,
                        int count)

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 the Object.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)

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. Like titleCase(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 humanized
removableTokens - optional array of tokens that are to be removed
Returns:
the humanized string
See Also:
titleCase(String, String[])

underscore

public String underscore(String camelCaseWord,
                         char... delimiterChars)
Makes an underscored form from the expression in the string (the reverse of the 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"
 

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.

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. Like 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"
 

Parameters:
words - the input to be turned into title case
removableTokens - optional array of tokens that are to be removed
Returns:
the title-case version of the supplied words

isUncountable

public boolean isUncountable(String word)
Determine whether the supplied word is considered uncountable by the pluralize and singularize methods.

Parameters:
word - the word
Returns:
true if the plural and singular forms of the word are the same

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

Returns:
the input string with the appropriate characters converted to upper-case

initialize

protected void initialize()


Copyright © 2011-2012 Wakaleo Consulting. All Rights Reserved.