Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E213E200C92 for ; Mon, 12 Jun 2017 14:44:23 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id E0BB1160BD6; Mon, 12 Jun 2017 12:44:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3125F160BD9 for ; Mon, 12 Jun 2017 14:44:23 +0200 (CEST) Received: (qmail 60461 invoked by uid 500); 12 Jun 2017 12:44:20 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 59074 invoked by uid 99); 12 Jun 2017 12:44:18 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Jun 2017 12:44:18 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 57F22E7E99; Mon, 12 Jun 2017 12:44:14 +0000 (UTC) From: chtompki To: dev@commons.apache.org Reply-To: dev@commons.apache.org References: In-Reply-To: Subject: [GitHub] commons-text pull request #46: TEXT-85:Added CaseUtils class with camel case... Content-Type: text/plain Message-Id: <20170612124416.57F22E7E99@git1-us-west.apache.org> Date: Mon, 12 Jun 2017 12:44:14 +0000 (UTC) archived-at: Mon, 12 Jun 2017 12:44:24 -0000 Github user chtompki commented on a diff in the pull request: https://github.com/apache/commons-text/pull/46#discussion_r121380327 --- Diff: src/main/java/org/apache/commons/text/CaseUtils.java --- @@ -0,0 +1,140 @@ +/* + * 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. + */ +package org.apache.commons.text; + +import org.apache.commons.lang3.StringUtils; + +import java.util.HashSet; +import java.util.Set; + +/** + *

Case manipulation operations on Strings that contain words.

+ * + *

This class tries to handle null input gracefully. + * An exception will not be thrown for a null input. + * Each method documents its behaviour in more detail.

+ * + * @since 1.0 + */ +public class CaseUtils { + + /** + *

CaseUtils instances should NOT be constructed in + * standard programming. Instead, the class should be used as + * CaseUtils.toCamelCase("foo bar", true, new char[]{'-'});.

+ *

+ *

This constructor is public to permit tools that require a JavaBean + * instance to operate.

+ */ + public CaseUtils() { + super(); + } + + // Camel Case + //----------------------------------------------------------------------- + /** + *

Converts all the delimiter separated words in a String into camelCase, + * that is each word is made up of a titlecase character and then a series of + * lowercase characters. The

+ * + *

The delimiters represent a set of characters understood to separate words. + * The first non-delimiter character after a delimiter will be capitalized. The first String + * character may or may not be capitalized and it's determined by the user input for capitalizeFirstLetter + * variable.

+ * + *

A null input String returns null. + * Capitalization uses the Unicode title case, normally equivalent to + * upper case.

+ * + *
    +     * CaseUtils.toCamelCase(null, false)                                 = null
    +     * CaseUtils.toCamelCase("", false, *)                                = ""
    +     * CaseUtils.toCamelCase(*, false, null)                              = *
    +     * CaseUtils.toCamelCase(*, true, new char[0])                        = *
    +     * CaseUtils.toCamelCase("To.Camel.Case", false, new char[]{'.'})     = "toCamelCase"
    +     * CaseUtils.toCamelCase(" to @ Camel case", true, new char[]{'@'})   = "toCamelCase"
    +     * CaseUtils.toCamelCase(" @to @ Camel case", false, new char[]{'@'}) = "toCamelCase"
    +     * 
+ * + * @param str the String to be converted to camelCase, may be null + * @param capitalizeFirstLetter boolean that determines if the first character of first word should be title case. + * @param delimiters set of characters to determine capitalization, null and/or empty array means whitespace + * @return camelCase of String, null if null String input + */ + public static String toCamelCase(String str, boolean capitalizeFirstLetter, final char... delimiters) { + if (StringUtils.isEmpty(str)) { + return str; + } + str = str.toLowerCase(); + int strLen = str.length(); + int [] newCodePoints = new int[strLen]; + int outOffset = 0; + Set delimiterSet = generateDelimiterSet(delimiters); + boolean capitalizeNext = false; + if (capitalizeFirstLetter) { + capitalizeNext = true; + } + for (int index = 0; index < strLen;) { + final int codePoint = str.codePointAt(index); + + if (delimiterSet.contains(codePoint)) { + capitalizeNext = true; + if (outOffset == 0) { + capitalizeNext = false; + } + index += Character.charCount(codePoint); + } else if (capitalizeNext || (outOffset == 0 && capitalizeFirstLetter)) { + int titleCaseCodePoint = Character.toTitleCase(codePoint); --- End diff -- +1 on operating on `int`s. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org