Java String Pluralize pluralise(String name)

Here you can find the source of pluralise(String name)

Description

Utility methods used to convert DB object names to appropriate Java type and field name

License

Mozilla Public License

Declaration

public static String pluralise(String name) 

Method Source Code

//package com.java2s;
/**/*from  w ww.  ja va  2 s. c  o  m*/
 SpagoBI, the Open Source Business Intelligence suite
    
 Copyright (C) 2012 Engineering Ingegneria Informatica S.p.A. - SpagoBI Competency Center
 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0. If a copy of the MPL was not distributed with this file,
 You can obtain one at http://mozilla.org/MPL/2.0/.
     
**/

public class Main {
    /**
     * Utility methods used to convert DB object names to  
     * appropriate Java type and field name 
     */
    public static String pluralise(String name) {
        if (name == null || "".equals(name))
            return "";
        String result = name;
        if (name.length() == 1) {
            result += 's';
        } else if (!seemsPluralised(name)) {
            String lower = name.toLowerCase();
            if (!lower.endsWith("data")) { //orderData --> orderDatas is dumb
                char secondLast = lower.charAt(name.length() - 2);
                if (!isVowel(secondLast) && lower.endsWith("y")) {
                    // city, body etc --> cities, bodies
                    result = name.substring(0, name.length() - 1) + "ies";
                } else if (lower.endsWith("ch") || lower.endsWith("s")) {
                    // switch --> switches  or bus --> buses
                    result = name + "es";
                } else {
                    result = name + "s";
                }
            }
        }
        return result;
    }

    private static boolean seemsPluralised(String name) {
        name = name.toLowerCase();
        boolean pluralised = false;
        pluralised |= name.endsWith("es");
        pluralised |= name.endsWith("s");
        pluralised &= !(name.endsWith("ss") || name.endsWith("us"));
        return pluralised;
    }

    private final static boolean isVowel(char c) {
        boolean vowel = false;
        vowel |= c == 'a';
        vowel |= c == 'e';
        vowel |= c == 'i';
        vowel |= c == 'o';
        vowel |= c == 'u';
        vowel |= c == 'y';
        return vowel;
    }
}

Related

  1. plural(StringBuffer buffer, int i, String s1, String s2)
  2. pluralForm(final String string, final int number)
  3. pluralify(String term, int value)
  4. pluralise(int count, String singular, String plural)
  5. pluralise(int num, String word)
  6. pluralise(String str)
  7. pluralise(String string, double size)
  8. pluraliseInc(int num, String word)
  9. pluraliseNoun(String noun, long count, boolean forceAppendingSByDefault)