-
Notifications
You must be signed in to change notification settings - Fork 279
Cases API + 4 implementations (Pascal, Camel, Kebab, Snake) #450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theshoeshiner
wants to merge
54
commits into
apache:master
Choose a base branch
from
theshoeshiner:cases
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
0de50a2
spelling
47c244f
spelling
ff8c791
private util method, spelling
31bd896
class description
f8cbd57
loop
d741a00
private/protected methods, spelling
67ccbb8
use ArrayList
b3a3368
doc formatting
f98d277
doc spelling
76de865
@throws docs, spelling
de03d81
checkstyle violations
dd0394b
doc formatting
1695051
javadoc formatting
1cb481f
implemented unicode casing logic
8833ae2
order of if/else
c328233
Revert "order of if/else"
eff0529
spelling
b32035f
grammar
0280d33
Improved implementation to better handle unicode cases
52e6441
renamed class for clarity
23912de
javadoc comment
670f84e
renamed variable for clarity
c24c802
Util class no longer needed, can place methods directly in base class
3c1d7a8
javadocs
8cb9291
grammar and spelling
cbfbad7
separate error messages
bbde93f
use list for delimiters since collection size is likely small
8f5acb0
use isempty
9aba7a3
allow null delimiters
fe070e8
allow null when parsing
f92cae9
Updated package info
6613073
javadoc formatting
d3964ee
package protected constructors
145b4e4
javadoc
d585716
utils class to hold static references
daa58d2
moved tests to correct case methods
8db51dd
javadocs
7fc8770
javadocs
89eca00
renamed class
8aa6edb
added string matcher that matches on any Unicode upper case tokens
8b2ced4
allow setting whether to omit the delimiter from the token
theshoeshiner 992ac13
test omit delimiter flag
theshoeshiner a50975c
converted cases api to use StringTokenizer and TokenStringifier logic
theshoeshiner 1406d30
throw unsupported from uppercase matcher
theshoeshiner 6747348
licenses
theshoeshiner 61bf2e5
dont need to check when there is not enough room left for constant
theshoeshiner 05422f1
tests for token factory and stringifier
theshoeshiner 58f05e3
formatting
theshoeshiner 5481f1d
spelling
theshoeshiner 25236f1
licenses
theshoeshiner 9e2bcd1
Merge branch 'apache:master' into cases
theshoeshiner ce5d2e2
Merge branch 'master' of
f91a1a2
corrected merge
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
src/main/java/org/apache/commons/text/cases/CamelCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| super(); | ||
| } | ||
|
|
||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * Parses string tokens from a Camel Case formatted string. | ||
| * <p> | ||
| * Parses each character of the string parameter and creates new tokens when uppercase ascii | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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 | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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 | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @return list of tokens parsed from the string | ||
| */ | ||
| @Override | ||
| public List<String> parse(String string) { | ||
| List<String> tokens = new LinkedList<>(); | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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(); | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * ascii letter, which will be forced uppercase in the output, except for the very first token, | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * by the parse method should abide by any restrictions present in the format method. i.e. Calling | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * 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); | ||
theshoeshiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * 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 | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| List<String> parse(String string); | ||
|
|
||
| } | ||
143 changes: 143 additions & 0 deletions
143
src/main/java/org/apache/commons/text/cases/DelimitedCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
40
src/main/java/org/apache/commons/text/cases/KebabCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m a bit concerned that Same for other sub classes of |
||
|
|
||
| /** | ||
| * Constructs a new KebabCase instance. | ||
| */ | ||
| public KebabCase() { | ||
| super(DELIMITER); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.