Java String Pluralize pluralize(int count, String singular)

Here you can find the source of pluralize(int count, String singular)

Description

Pluralize the singular word, unless count == 1, and concatenate it to the count.

License

Open Source License

Return

a String with the result of pluralization; never null.

Declaration

public static String pluralize(int count, String singular) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oobium, Inc./*from w ww.  j a v  a2  s .co  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Jeremy Dowdall <jeremy@oobium.com> - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Pluralize the singular word, unless count == 1, and concatenate it to the count.
     * <dl>
     * <dt>For example:</dt>
     * <dd>pluralize(1, "person") -> "1 person"</dd>
     * <dd>pluralize(2, "person") -> "2 people"</dd>
     * </dl>
     * @return a String with the result of pluralization; never null.
     */
    public static String pluralize(int count, String singular) {
        return count + " " + pluralize(singular, count);
    }

    /**
     * Pluralize the singular word, unless count == 1, and concatenate it to the count.
     * Uses the provided plural word rather than calling plural(singular).
     * <dl>
     * <dt>For example:</dt>
     * <dd>pluralize(1, "person", "users") -> "1 person"</dd>
     * <dd>pluralize(2, "person", "users") -> "2 users"</dd>
     * </dl>
     * @return a String with the result of pluralization; never null.
     */
    public static String pluralize(int count, String singular, String plural) {
        return count + " " + pluralize(singular, plural, count);
    }

    /**
     * Pluralize the singular word, unless count == 1, and return it.
     * The result does not include the count in case you want to do something different.
     * <dl>
     * <dt>For example:</dt>
     * <dd>pluralize("person", 1) -> "person"</dd>
     * <dd>pluralize("person", 2) -> "people"</dd>
     * </dl>
     * @return a String with the result of pluralization; never null.
     */
    public static String pluralize(String singular, int count) {
        return (count == 1) ? singular : plural(singular);
    }

    /**
     * Pluralize the singular word, unless count == 1, and return it.
     * Uses the provided plural word rather than calling plural(singular).
     * The result does not include the count in case you want to do something different.
     * <dl>
     * <dt>For example:</dt>
     * <dd>pluralize("person", "users", 1) -> "person"</dd>
     * <dd>pluralize("person", "users", 2) -> "users"</dd>
     * </dl>
     * @return a String with the result of pluralization; never null.
     */
    public static String pluralize(String singular, String plural, int count) {
        return (count == 1) ? singular : plural;
    }

    public static String plural(String singular) {
        if (singular == null || singular.length() == 0) {
            return singular;
        }
        if (singular.equalsIgnoreCase("person")) {
            return singular.charAt(0) + "eople";
        } else if (singular.equalsIgnoreCase("child")) {
            return singular.charAt(0) + "hildren";
        } else if (singular.equalsIgnoreCase("alumnus")) {
            return singular.charAt(0) + "lumni";
        } else if ('y' == singular.charAt(singular.length() - 1)) {
            if (singular.length() > 1) {
                switch (singular.charAt(singular.length() - 2)) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    break;
                default:
                    return singular.substring(0, singular.length() - 1) + "ies";
                }
            }
        } else if ('s' == singular.charAt(singular.length() - 1)) {
            return singular + "es";
        } else if (singular.endsWith("ch")) {
            return singular + "es";
        }
        return singular + "s";
    }
}

Related

  1. pluralize(final int count, final String singular, final String plural)
  2. pluralize(final String s)
  3. pluralize(final String typeName)
  4. pluralize(int count, final String singular, final String plural)
  5. pluralize(int count, String single)
  6. pluralize(int count, String singular, String plural)
  7. pluralize(int count, String singular, String plural)
  8. pluralize(int count, String word)
  9. Pluralize(int quantity, String units)