@Override public String toString() { int count = this.getCount(); String samples = Inflector.getInstance().pluralize("sample", count); return StringUtil.createString("{0} {1}: min={2}; avg={3}; max={4}", count, samples, this.minimum, this.mean, this.maximum); }
@Override public Inflector clone() { return new Inflector(this); }
/** * Converts strings to lowerCamelCase. This method will also use any extra delimiter characters to identify word boundaries. * <p> * Examples: * * <pre> * inflector.lowerCamelCase("active_record") #=> "activeRecord" * inflector.lowerCamelCase("first_name") #=> "firstName" * inflector.lowerCamelCase("name") #=> "name" * inflector.lowerCamelCase("the-first_name",'-') #=> "theFirstName" * </pre> * * </p> * * @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); }
/** * Set the human-readable label for the property. If null, this will be set to the * {@link Inflector#humanize(String, String...) humanized} form of the {@link #getName() property name}. * * @param label the new label for the property; may be null */ public void setLabel( String label ) { if (label == null && name != null) { label = INFLECTOR.titleCase(INFLECTOR.humanize(INFLECTOR.underscore(name))); } this.label = label; }
/** * 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. * <p> * Examples: * * <pre> * inflector.titleCase("man from the boondocks") #=> "Man From The Boondocks" * inflector.titleCase("x-men: the last stand") #=> "X Men: The Last Stand" * </pre> * * </p> * * @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; }
/** * {@inheritDoc} * * @see java.lang.Object#toString() */ @Override public String toString() { Inflector inflector = Inflector.getInstance(); if (count() == 1) { return "read " + inflector.ordinalize(startingAtIndex()) + " thru " + inflector.ordinalize(endingBefore() - 1) + " children of " + of() + " in the \"" + workspaceName + "\" workspace"; } return "read " + inflector.ordinalize(startingAtIndex()) + " child of " + of() + " in the \"" + workspaceName + "\" workspace"; }
/** * Get a Field intance for a given class and property. Iterate over super classes of a class when a <@link * NoSuchFieldException> occurs until no more super classes are found then re-throw the <@link NoSuchFieldException>. * * @param targetClass * @param propertyName * @return Field * @throws NoSuchFieldException */ protected Field getField( Class<?> targetClass, String propertyName ) throws NoSuchFieldException { Field field = null; try { field = targetClass.getDeclaredField(Inflector.getInstance().lowerCamelCase(propertyName)); } catch (NoSuchFieldException e) { Class<?> clazz = targetClass.getSuperclass(); if (clazz != null) { field = getField(clazz, propertyName); } else { throw e; } } return field; }
public void lowerCamelCase( String word, String expectedCamelized, char... delimiterChars ) { // Test lowercasing the string String actualCamelized = inflector.camelCase(word, false, delimiterChars); assertEquals(expectedCamelized, actualCamelized); assertEquals(expectedCamelized, inflector.lowerCamelCase(word, delimiterChars)); if (delimiterChars == null || delimiterChars.length == 0) { // Test underscoring the camelized word ... String actualUnderscored = inflector.underscore(expectedCamelized); assertEquals(word, actualUnderscored); } }
/** * Find the property names with getter methods on the target class. This method returns the property names for the methods * returned by {@link #findGetterMethods()}. * * @return the Java Bean property names for the getters; never null but possibly empty */ public String[] findGetterPropertyNames() { final Method[] getters = findGetterMethods(); final List<String> result = new ArrayList<String>(); for (int i = 0; i < getters.length; i++) { final Method m = getters[i]; String name = m.getName(); String propertyName = null; if (name.startsWith("get") && name.length() > 3) { propertyName = name.substring(3); } else if (name.startsWith("is") && name.length() > 2) { propertyName = name.substring(2); } else if (name.startsWith("are") && name.length() > 3) { propertyName = name.substring(3); } if (propertyName != null) { propertyName = INFLECTOR.camelCase(INFLECTOR.underscore(propertyName), false); result.add(propertyName); } } return result.toArray(new String[result.size()]); }
return replaceAllWithUppercase(result, "(^|_)(.)", 2); + camelCase(lowerCaseAndUnderscoredWord, true, delimiterChars).substring(1);
public void singularToPlural( Object singular, String expectedPlural ) { // Test pluralizing the singular string String actualPlural = inflector.pluralize(singular); assertEquals(expectedPlural, actualPlural); // Test singularizing the given (expected) pluralized form String actualSingular = inflector.singularize(expectedPlural); assertEquals(singular, actualSingular); // Test singularizing the already singular form (should not change) assertEquals(singular, inflector.singularize(singular)); // Test pluralizing the already plural form (should not change) assertEquals(expectedPlural, inflector.pluralize(expectedPlural)); }
public void upperCamelCase( String word, String expectedCamelized, char... delimiterChars ) { // Test uppercasing the string String actualCamelized = inflector.camelCase(word, true, delimiterChars); assertEquals(expectedCamelized, actualCamelized); assertEquals(expectedCamelized, inflector.upperCamelCase(word, delimiterChars)); if (delimiterChars == null || delimiterChars.length == 0) { // Test underscoring the camelized word ... String actualUnderscored = inflector.underscore(expectedCamelized); assertEquals(word, actualUnderscored); } }
public String pluralize( Object word, int count ) { if (word == null) return null; if (count == 1 || count == -1) { return word.toString(); } return pluralize(word); }
return capitalize(result);
public void ordinalize( int number, String expectedValue ) { // Test underscoring the camelized word String actualValue = inflector.ordinalize(number); assertEquals(expectedValue, actualValue); }
public void titleCase( String word, String expectedValue, String... removableTokens ) { // Test title casing the word String actualValue = inflector.titleCase(word, removableTokens); assertEquals(expectedValue, actualValue); }
public void humanize( String word, String expectedValue, String... removableTokens ) { // Test humanizing the word String actualValue = inflector.humanize(word, removableTokens); assertEquals(expectedValue, actualValue); }
@Test public void shouldReplaceAllWithUppercase() { assertEquals("hEllO", Inflector.replaceAllWithUppercase("hello", "([aeiou])", 1)); assertEquals("hLlo", Inflector.replaceAllWithUppercase("hello", "([aeiou])(l)", 2)); }
/** * {@inheritDoc} * * @see org.modeshape.graph.property.Path#getAncestor(int) */ public Path getAncestor( int degree ) { CheckArg.isNonNegative(degree, "degree"); if (degree == 0) { return this; } String msg = GraphI18n.pathAncestorDegreeIsInvalid.text(this.getString(), Inflector.getInstance().ordinalize(degree)); throw new InvalidPathException(msg); }
/** * Get a Field intance for a given class and property. Iterate over super classes of a class when a <@link * NoSuchFieldException> occurs until no more super classes are found then re-throw the <@link NoSuchFieldException>. * * @param targetClass * @param propertyName * @return Field * @throws NoSuchFieldException */ protected Field getField( Class<?> targetClass, String propertyName ) throws NoSuchFieldException { Field field = null; try { field = targetClass.getDeclaredField(Inflector.getInstance().lowerCamelCase(propertyName)); } catch (NoSuchFieldException e) { Class<?> clazz = targetClass.getSuperclass(); if (clazz != null) { field = getField(clazz, propertyName); } else { throw e; } } return field; }