Here you can find the source of pluralize(int count, String singular)
public static String pluralize(int count, String singular)
//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"; } }