Java String Pluralize pluraliseNoun(String noun, long count, boolean forceAppendingSByDefault)

Here you can find the source of pluraliseNoun(String noun, long count, boolean forceAppendingSByDefault)

Description

Performs naive pluralisation of the supplied noun.

License

Apache License

Parameter

Parameter Description
noun Noun in a singular form.
count Number of occurrences of the item, for which the noun is provided.
forceAppendingSByDefault <code>true</code> to make sure that "y" -> "ies" substitution is <b>not made</b>, but instead "s" is appended to unmodified root of the noun.

Return

Pluralised noun: with appended -s or -y replaced with -ies.

Declaration

public static String pluraliseNoun(String noun, long count, boolean forceAppendingSByDefault) 

Method Source Code

//package com.java2s;
/*/*from w ww.jav  a2 s.  c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * This is a convenience method for calling
     * {@link Util#pluraliseNoun(String, long, boolean)}
     * with <code>false</code> as a value for third parameter.
     */
    public static String pluraliseNoun(String noun, long count) {
        return (pluraliseNoun(noun, count, false));
    }

    /**
     * Performs naive pluralisation of the supplied noun.
     * 
     * @param noun Noun in a singular form.
     * @param count Number of occurrences of the item, for which the noun is provided.
     * @param forceAppendingSByDefault <code>true</code> to make sure that "y" -> "ies"
     *                                 substitution is <b>not made</b>, but instead "s" is appended
     *                                 to unmodified root of the noun.
     * @return Pluralised <code>noun</code>: with appended -s or -y replaced with -ies.
     */
    public static String pluraliseNoun(String noun, long count, boolean forceAppendingSByDefault) {
        if (count % 10 != 1 || count == 11) {
            if (!forceAppendingSByDefault && noun.endsWith("y")) {
                return (noun.substring(0, noun.length() - 1) + "ies"); // e.g. ENTRY -> ENTRIES
            } else {
                return (noun + "s"); // e.g. SHIP -> SHIPS
            }
        } else {
            // no need to pluralise - count is of the type 21, 31, etc.. 
            return noun;
        }
    }
}

Related

  1. pluralise(int num, String word)
  2. pluralise(String name)
  3. pluralise(String str)
  4. pluralise(String string, double size)
  5. pluraliseInc(int num, String word)
  6. pluralize(final int count, final String singular, final String plural)
  7. pluralize(final String s)
  8. pluralize(final String typeName)
  9. pluralize(int count, final String singular, final String plural)