Java String Camel Case camelCasedWord(String s)

Here you can find the source of camelCasedWord(String s)

Description

camel Cased Word

License

Open Source License

Declaration

public static final boolean camelCasedWord(String s) 

Method Source Code

//package com.java2s;
/*//  ww w.j  a  v  a2 s. c o  m
  Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
  This file is part of the Semantic Discovery Toolkit.
    
  The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  The Semantic Discovery Toolkit 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 Lesser General Public License for more details.
    
  You should have received a copy of the GNU Lesser General Public License
  along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */

public class Main {
    public static final boolean camelCasedWord(String s) {
        return camelCasedWord(s, 0, s.length());
    }

    public static final boolean camelCasedWord(String s, int startPos, int endPos) {
        boolean result = false;
        int capPos = capitalPos(s, startPos, endPos);
        boolean foundLowers = false;
        if (capPos >= 0) {
            result = true;
            while (result && capPos >= 0) {
                final int nextCapPos = capitalPos(s, capPos + 1, endPos);
                final int stopAt = (nextCapPos >= 0) ? nextCapPos : endPos;
                if (stopAt > capPos + 1) {
                    result = allLowerCase(s, capPos + 1, stopAt) && isWord(s, capPos + 1, stopAt);
                    if (!foundLowers)
                        foundLowers = result;
                }
                capPos = nextCapPos;
            }
        }
        return result && foundLowers;
    }

    private static final int capitalPos(String s, int startPos, int endPos) {
        for (int i = startPos; i < endPos; ++i) {
            int c = s.codePointAt(i);
            if (isCapital(c))
                return i;
            if (c > 65535)
                ++i;
        }
        return -1;
    }

    public static final boolean allLowerCase(String s) {
        return allLowerCase(s, 0, s.length());
    }

    /**
       * @return true if all letters in the string are lower case and there is at least one letter.
       */
    public static final boolean allLowerCase(String s, int startPos, int endPos) {
        boolean allLower = true;
        boolean foundOne = false;
        for (int i = startPos; i < endPos; ++i) {
            final int c = s.codePointAt(i);
            if (c > 65535)
                ++i;
            if (Character.isLetter(c)) {
                foundOne = true;
                if (!Character.isLowerCase(c)) {
                    allLower = false;
                    break;
                }
            }
        }
        return allLower && foundOne;
    }

    public static final boolean isWord(String s) {
        return isWord(s, 0, s.length());
    }

    public static final boolean isWord(String s, int startPos, int endPos) {
        boolean isWord = true;
        boolean foundOne = false;
        for (int i = startPos; i < endPos; ++i) {
            final int c = s.codePointAt(i);
            if (c > 65535)
                ++i;
            if (!Character.isLetter(c)) {
                isWord = false;
                break;
            } else {
                if (!foundOne)
                    foundOne = true;
            }
        }
        return isWord && foundOne;
    }

    public static final boolean isCapital(String s, int pos) {
        return Character.isUpperCase(s.codePointAt(pos));
    }

    public static final boolean isCapital(int codePoint) {
        //Character.isLetter(c) && (Character.isUpperCase(c) || Character.isTitleCase(c)))
        return Character.isUpperCase(codePoint);
    }
}

Related

  1. camelCase(String text)
  2. camelCase(String text)
  3. camelCase(String text)
  4. camelCase(String text)
  5. camelCased(String str)
  6. camelCaseWord(String word)
  7. cameliza(String str)
  8. cameliza(String str)
  9. camelize(String s)