Java String Pluralize plural(String word, Number cardinality)

Here you can find the source of plural(String word, Number cardinality)

Description

Returns the plural form of the specified word if cardinality != 1.

License

Open Source License

Declaration

public static String plural(String word, Number cardinality) 

Method Source Code

//package com.java2s;
/*/*  www .j av  a  2s  .  c  o m*/
 * This file is part of ConfigHub.
 *
 * ConfigHub is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ConfigHub is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.??See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ConfigHub.??If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns the plural form of the specified word if cardinality
     * != 1.  The plural form is is generated by concatenating the letter
     * 's' to the end of the word, unless the word ends in an h, s, or x.
     * In these cases, 'es' is added to form the plural.
     * <p>
     * This method assumes that the word is not already plural.
     */
    public static String plural(String word, Number cardinality) {
        String suffix = "s";
        char lastChar = word.charAt(word.length() - 1);

        if (lastChar == 'h' || lastChar == 's' || lastChar == 'x') {
            suffix = "es";
        }

        return plural(word, suffix, cardinality);
    }

    public static String plural(String word, String suffix, Number cardinality) {
        if (cardinality.doubleValue() == 1) {
            return word;
        }
        return word + suffix;
    }
}

Related

  1. plural(String str, String suffix, int count)
  2. plural(String strSingle, String strPlural, int amount)
  3. plural(String txt)
  4. plural(String type)
  5. plural(String word)
  6. plural(StringBuffer buffer, int i, String s1, String s2)
  7. pluralForm(final String string, final int number)
  8. pluralify(String term, int value)
  9. pluralise(int count, String singular, String plural)