Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
b64a0e0
initial implementation of Cases api
Aug 15, 2023
0de50a2
spelling
Aug 21, 2023
47c244f
spelling
Aug 21, 2023
ff8c791
private util method, spelling
Aug 21, 2023
31bd896
class description
Aug 21, 2023
f8cbd57
loop
Aug 21, 2023
d741a00
private/protected methods, spelling
Aug 21, 2023
67ccbb8
use ArrayList
Aug 21, 2023
b3a3368
doc formatting
Aug 21, 2023
f98d277
doc spelling
Aug 21, 2023
76de865
@throws docs, spelling
Aug 21, 2023
de03d81
checkstyle violations
Aug 21, 2023
dd0394b
doc formatting
Aug 21, 2023
1695051
javadoc formatting
Sep 11, 2023
1cb481f
implemented unicode casing logic
Oct 2, 2023
8833ae2
order of if/else
Oct 2, 2023
c328233
Revert "order of if/else"
Oct 2, 2023
eff0529
spelling
Oct 3, 2023
b32035f
grammar
Oct 4, 2023
0280d33
Improved implementation to better handle unicode cases
Oct 19, 2023
52e6441
renamed class for clarity
Oct 19, 2023
23912de
javadoc comment
Oct 19, 2023
670f84e
renamed variable for clarity
Oct 19, 2023
c24c802
Util class no longer needed, can place methods directly in base class
Oct 19, 2023
3c1d7a8
javadocs
Oct 19, 2023
8cb9291
grammar and spelling
Oct 20, 2023
cbfbad7
separate error messages
Oct 20, 2023
bbde93f
use list for delimiters since collection size is likely small
Oct 24, 2023
8f5acb0
use isempty
Oct 24, 2023
9aba7a3
allow null delimiters
Oct 24, 2023
fe070e8
allow null when parsing
Oct 24, 2023
f92cae9
Updated package info
Oct 24, 2023
6613073
javadoc formatting
Oct 24, 2023
d3964ee
package protected constructors
Oct 24, 2023
145b4e4
javadoc
Oct 24, 2023
d585716
utils class to hold static references
Oct 30, 2023
daa58d2
moved tests to correct case methods
Oct 31, 2023
8db51dd
javadocs
Oct 31, 2023
7fc8770
javadocs
Oct 31, 2023
89eca00
renamed class
Oct 31, 2023
8aa6edb
added string matcher that matches on any Unicode upper case tokens
Nov 1, 2023
8b2ced4
allow setting whether to omit the delimiter from the token
theshoeshiner Nov 28, 2023
992ac13
test omit delimiter flag
theshoeshiner Nov 28, 2023
a50975c
converted cases api to use StringTokenizer and TokenStringifier logic
theshoeshiner Nov 28, 2023
1406d30
throw unsupported from uppercase matcher
theshoeshiner Nov 28, 2023
6747348
licenses
theshoeshiner Nov 28, 2023
61bf2e5
dont need to check when there is not enough room left for constant
theshoeshiner Nov 29, 2023
05422f1
tests for token factory and stringifier
theshoeshiner Nov 29, 2023
58f05e3
formatting
theshoeshiner Nov 29, 2023
5481f1d
spelling
theshoeshiner Nov 29, 2023
25236f1
licenses
theshoeshiner Nov 29, 2023
9e2bcd1
Merge branch 'apache:master' into cases
theshoeshiner Nov 29, 2023
ce5d2e2
Merge branch 'master' of
Mar 20, 2024
f91a1a2
corrected merge
Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions src/main/java/org/apache/commons/text/cases/CamelCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.cases;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;

/**
* Case implementation that parses and formats strings of the form 'myCamelCase'
* <p>
* This case separates tokens on uppercase ascii alpha characters, with the exception
* that the first token begin with a lowercase ascii alpha character.
* </p>
*/
public class CamelCase implements Case {

/** constant reuseable instance of this case. */
public static final CamelCase INSTANCE = new CamelCase();

/**
* Constructs new CamelCase instance.
*/
public CamelCase() {
super();
}

/**
* Parses string tokens from a Camel Case formatted string.
* <p>
* Parses each character of the string parameter and creates new tokens when uppercase ascii
* letters are encountered. The upppercase letter is considered part of the new token. The very
* first character of the string is an exception to this rule and must be a lowercase ascii
* character. This method places no other restrictions on the content of the string. <br>
* Note: This method should never produce empty tokens.
* </p>
* @param string Camel Case formatted string to parse
* @return list of tokens parsed from the string
*/
@Override
public List<String> parse(String string) {
List<String> tokens = new LinkedList<>();
if (string.length() == 0) {
return tokens;
}
if (!CharUtils.isAsciiAlphaLower(string.charAt(0))) {
throw new IllegalArgumentException("Character '" + string.charAt(0) + "' at index 0 must be an ascii lowercase letter");
}
/*StringBuilder tokenBuilder = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (CharUtils.isAsciiAlphaUpper(c)) {
tokens.add(tokenBuilder.toString());
tokenBuilder.setLength(0);
}
tokenBuilder.append(c);
}
tokens.add(tokenBuilder.toString());*/
int strLen = string.length();
int[] tokenCodePoints = new int[strLen];
int tokenCodePointsOffset = 0;
for (int i = 0; i < string.length();) {
final int codePoint = string.codePointAt(i);
if (CharUtils.isAsciiAlphaUpper((char) codePoint)) {
if (tokenCodePointsOffset > 0) {
tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset));
tokenCodePoints = new int[strLen];
tokenCodePointsOffset = 0;
}
tokenCodePoints[tokenCodePointsOffset++] = codePoint;
i += Character.charCount(codePoint);
} else {
tokenCodePoints[tokenCodePointsOffset++] = codePoint;
i += Character.charCount(codePoint);
}
}
tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset));
return tokens;
}

/**
* Formats tokens into a Camel Case string.
* <p>
* Iterates each token and creates a camel case formatted string. Each token must begin with an
* ascii letter, which will be forced uppercase in the output, except for the very first token,
* which will have a lowercase first character. The remaining characters in all tokens will be
* forced lowercase. This Case does not support empty tokens.<br>
* No other restrictions are placed on token contents.
* </p>
* @param tokens String tokens to format into CamelCase
* @return Camel Case formatted string
*/
@Override
public String format(Iterable<String> tokens) {
StringBuilder formattedString = new StringBuilder();
int i = 0;
for (String token : tokens) {
if (token.length() == 0) {
throw new IllegalArgumentException("Unsupported empty token at index " + i);
}
if (!CharUtils.isAsciiAlpha(token.charAt(0))) {
throw new IllegalArgumentException("First character '" + token.charAt(0) + "' in token " + i + " must be an ascii letter");
}
String formattedToken = (i == 0 ? token.substring(0, 1).toLowerCase() : token.substring(0, 1).toUpperCase())
+ (token.length() > 1 ? token.substring(1).toLowerCase() : StringUtils.EMPTY);
i++;
formattedString.append(formattedToken);
}
return formattedString.toString();
}

}
50 changes: 50 additions & 0 deletions src/main/java/org/apache/commons/text/cases/Case.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.cases;

import java.util.List;

/**
* Handles formatting and parsing tokens to/from a String. For most implementations tokens returned
* by the parse method should abide by any restrictions present in the format method. i.e. Calling
* format() with the results of a call to parse() on the same Case instance should return a
* matching String.
*
* @since 1.11
*/
public interface Case {

/**
* Formats a set of tokens into a string. The tokens do not necessarily have to meet the syntax
* requirements of the Case. The documentation for each implementation should specify what input
* is supported.
*
* @param tokens string tokens to be formatted by this Case
* @return the formatted string
*/
String format(Iterable<String> tokens);

/**
* Parses a string into a series of tokens. The string must abide by certain restrictions,
* dependent on each Case implementation.
*
* @param string The string to be parsed by the Case into a list of tokens
* @return The list of parsed tokens
*/
List<String> parse(String string);

}
143 changes: 143 additions & 0 deletions src/main/java/org/apache/commons/text/cases/DelimitedCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.cases;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.CharUtils;

/**
* DelimitedCase is a case in which the true alphabetic case of the characters is ignored by default
* and tokens themselves are determined by the presence of a delimiter between each token.
*/
public class DelimitedCase implements Case {

/** delimiters to be used when parsing. */
private Set<Integer> parseDelimiters;

/** delimited to be used when formatting. */
private String formatDelimiter;

/**
* Constructs a new Delimited Case.
* @param delimiter the character to use as both the parse and format delimiter
*/
public DelimitedCase(char delimiter) {
this(new char[] { delimiter }, CharUtils.toString(delimiter));
}

/**
* Constructs a new delimited case.
* @param parseDelimiters The array of delimiters to use when parsing
* @param formatDelimiter The delimiter to use when formatting
*/
public DelimitedCase(char[] parseDelimiters, String formatDelimiter) {
super();
if (parseDelimiters == null || parseDelimiters.length == 0) {
throw new IllegalArgumentException("Parse Delimiters cannot be null or empty");
}
if (formatDelimiter == null || formatDelimiter.length() == 0) {
throw new IllegalArgumentException("Format Delimiters cannot be null or empty");
}
this.parseDelimiters = generateDelimiterSet(parseDelimiters);
this.formatDelimiter = formatDelimiter;
}

/**
* Formats tokens into Delimited Case.
* <p>
* Tokens are iterated on and appended to an output stream, with an instance of a
* delimiter character between them. This method validates that the delimiter character is not
* part of the token. If it is found within the token an exception is thrown.<br>
* No other restrictions are placed on the contents of the tokens.
* Note: This Case does support empty tokens.<br>
* </p>
* @param tokens the tokens to be formatted into a delimited string
* @return The delimited string
*/
@Override
public String format(Iterable<String> tokens) {
StringBuilder formattedString = new StringBuilder();
int i = 0;
for (String token : tokens) {
int delimiterFoundIndex = token.indexOf(formatDelimiter);
if (delimiterFoundIndex > -1) {
throw new IllegalArgumentException("Token " + i + " contains delimiter character '" + formatDelimiter + "' at index " + delimiterFoundIndex);
}
if (i > 0) {
formattedString.append(formatDelimiter);
}
i++;
formattedString.append(token);
}
return formattedString.toString();
}

/**
* Parses delimited string into tokens.
* <p>
* Input string is parsed one character at a time until a delimiter character is reached.
* When a delimiter character is reached a new token begins. The delimiter character is
* considered reserved, and is omitted from the returned parsed tokens.<br>
* No other restrictions are placed on the contents of the input string. <br>
* </p>
* @param string The delimited string to be parsed
* @return The list of tokens found in the string
*/
@Override
public List<String> parse(String string) {
List<String> tokens = new LinkedList<>();
if (string.length() == 0) {
return tokens;
}
int strLen = string.length();
int[] tokenCodePoints = new int[strLen];
int tokenCodePointsOffset = 0;
for (int i = 0; i < string.length();) {
final int codePoint = string.codePointAt(i);
if (parseDelimiters.contains(codePoint)) {
tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset));
tokenCodePoints = new int[strLen];
tokenCodePointsOffset = 0;
i++;
} else {
tokenCodePoints[tokenCodePointsOffset++] = codePoint;
i += Character.charCount(codePoint);
}
}
tokens.add(new String(tokenCodePoints, 0, tokenCodePointsOffset));
return tokens;
}

/**
* Converts an array of delimiters to a hash set of code points. The generated hash set provides O(1) lookup time.
*
* @param delimiters set of characters to determine capitalization, null means whitespace
* @return Set<Integer>
*/
private static Set<Integer> generateDelimiterSet(final char[] delimiters) {
final Set<Integer> delimiterHashSet = new HashSet<>();
for (int index = 0; index < delimiters.length; index++) {
delimiterHashSet.add(Character.codePointAt(delimiters, index));
}
return delimiterHashSet;
}

}
40 changes: 40 additions & 0 deletions src/main/java/org/apache/commons/text/cases/KebabCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.cases;

/**
* Case implementation which parses and formats strings of the form 'my-kebab-string'
* <p>
* KebabCase is a delimited case where the delimiter is a hyphen character '-'.
* </p>
*/
public class KebabCase extends DelimitedCase {

/** constant for delimiter. */
public static final char DELIMITER = '-';

/** constant reuseable instance of this case. */
public static final KebabCase INSTANCE = new KebabCase();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a bit concerned that CharacterDelimitedCase may not be thread-safe, so having a single static instance could potentially cause issues. Would it be safer to create instances as needed instead?

Same for other sub classes of CharacterDelimitedCase


/**
* Constructs a new KebabCase instance.
*/
public KebabCase() {
super(DELIMITER);
}

}
Loading