-
Notifications
You must be signed in to change notification settings - Fork 272
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 46 commits
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
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
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,21 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public interface TokenFormatter { | ||
| String format(char[] prior, int tokenIndex, char[] token); | ||
| } |
130 changes: 130 additions & 0 deletions
130
src/main/java/org/apache/commons/text/TokenFormatterFactory.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,130 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public class TokenFormatterFactory { | ||
|
|
||
| /** | ||
| * Token formatter that returns the token as is. | ||
| */ | ||
| public static class NoOpFormatter implements TokenFormatter { | ||
| @Override | ||
| public String format(char[] prior, int tokenIndex, char[] token) { | ||
| return new String(token); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Token formatter that always returns a constant string, and optionally checks the passed in token | ||
| * for the constant and throws an error when found. | ||
| */ | ||
| public static class ConstantTokenFormatter implements TokenFormatter { | ||
|
|
||
| /** | ||
| * The constant to return. | ||
| */ | ||
| private char[] constant; | ||
|
|
||
| /** | ||
| * Whether or not to throw an exception if the constant is found. | ||
| */ | ||
| private boolean failOnConstantFound = true; | ||
|
|
||
| public ConstantTokenFormatter(char constant) { | ||
| this(new char[] {constant}, true); | ||
| } | ||
|
|
||
| public ConstantTokenFormatter(char constant, boolean failOnConstantFound) { | ||
| this(new char[] {constant}, failOnConstantFound); | ||
| } | ||
|
|
||
| public ConstantTokenFormatter(String constant) { | ||
| this(constant, true); | ||
| } | ||
|
|
||
| public ConstantTokenFormatter(String constant, boolean failOnConstantFound) { | ||
| this(constant.toCharArray(), failOnConstantFound); | ||
| } | ||
|
|
||
| public ConstantTokenFormatter(char[] constant, boolean failOnConstantFound) { | ||
| this.constant = constant; | ||
| this.failOnConstantFound = failOnConstantFound; | ||
| } | ||
|
|
||
| @Override | ||
| public String format(char[] prior, int tokenIndex, char[] token) { | ||
| if (failOnConstantFound) { | ||
| for (int i = 0; i < token.length; i++) { | ||
| boolean match = false; | ||
| int t = i; | ||
| for (int j = 0; j < constant.length; j++) { | ||
| if (token[t] == constant[j]) { | ||
| match = true; | ||
| } else { | ||
| match = false; | ||
| break; | ||
| } | ||
| t++; | ||
| } | ||
| if (match) { | ||
| throw new IllegalArgumentException("Token " + tokenIndex + " contains illegal character '" + new String(constant) + "' at index " + t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new String(constant); | ||
| } | ||
|
|
||
| /** | ||
| * Set whether to check the token for the constant. | ||
| * @param checkTokenForConstant whether to check. | ||
| */ | ||
| public void setFailOnConstantFound(boolean checkTokenForConstant) { | ||
| this.failOnConstantFound = checkTokenForConstant; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Reuseable NoOpFormatter instance. | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| private static final NoOpFormatter NOOP_FORMATTER = new NoOpFormatter(); | ||
|
|
||
| /** | ||
| * Reuseable Empty String formatter instance. | ||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| private static final ConstantTokenFormatter EMPTY_STRING_FORMATTER = new ConstantTokenFormatter(StringUtils.EMPTY, false); | ||
|
|
||
| public static NoOpFormatter noOpFormatter() { | ||
| return NOOP_FORMATTER; | ||
| } | ||
|
|
||
| public static ConstantTokenFormatter constantFormatter(char[] constant, boolean failOnConstant) { | ||
| return new ConstantTokenFormatter(constant, failOnConstant); | ||
| } | ||
|
|
||
| public static ConstantTokenFormatter constantFormatter(char constant, boolean failOnConstant) { | ||
| return new ConstantTokenFormatter(constant, failOnConstant); | ||
| } | ||
|
|
||
| public static ConstantTokenFormatter emptyFormatter() { | ||
| return EMPTY_STRING_FORMATTER; | ||
| } | ||
| } | ||
97 changes: 97 additions & 0 deletions
97
src/main/java/org/apache/commons/text/TokenStringifier.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,97 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * Takes a collection of String tokens and combines them into a single String. | ||
| * <p> | ||
| * This class functions as the inverse of {@link org.apache.commons.text.StringTokenizer}. All tokens are formatted | ||
| * by a {@link TokenFormatter} which allows fine grained control over the final output. | ||
| * </p> | ||
| */ | ||
| public class TokenStringifier { | ||
|
|
||
| /** | ||
| * The formatter for the delimiter. | ||
| */ | ||
| private TokenFormatter delimiterFormatter; | ||
|
|
||
| /** | ||
| * The formatter for the tokens. | ||
| */ | ||
| private TokenFormatter tokenFormatter; | ||
|
|
||
| /** | ||
| * Builder used to hold formatted tokens. | ||
| */ | ||
| private StringBuilder builder; | ||
|
|
||
| /** | ||
| * The final string. | ||
| */ | ||
| private String string; | ||
|
|
||
| /** | ||
| * The tokens to turn into a String. | ||
| */ | ||
| private Iterable<String> tokens; | ||
|
|
||
| public TokenStringifier(TokenFormatter delimiterFormatter, TokenFormatter tokenFormatter) { | ||
| super(); | ||
| this.delimiterFormatter = delimiterFormatter; | ||
| this.tokenFormatter = tokenFormatter; | ||
| } | ||
|
|
||
| public void reset(Iterable<String> tokens) { | ||
| this.tokens = tokens; | ||
| this.string = null; | ||
| this.builder = null; | ||
| } | ||
|
|
||
| public TokenStringifier() { | ||
| tokenFormatter = TokenFormatterFactory.noOpFormatter(); | ||
| delimiterFormatter = TokenFormatterFactory.noOpFormatter(); | ||
| } | ||
|
|
||
| private void stringify() { | ||
| builder = new StringBuilder(); | ||
| char[] priorToken = null; | ||
| int i = 0; | ||
| for (String token : tokens) { | ||
| char[] tokenChars = token.toCharArray(); | ||
| if (i > 0) { | ||
| String delimiter = delimiterFormatter.format(priorToken, i, tokenChars); | ||
| if (delimiter != null) { | ||
| builder.append(delimiter); | ||
| } | ||
| } | ||
| String formatted = tokenFormatter.format(priorToken, i, tokenChars); | ||
| if (formatted != null) { | ||
| builder.append(formatted); | ||
| } | ||
| i++; | ||
| } | ||
| string = builder.toString(); | ||
| } | ||
|
|
||
| public String getString() { | ||
| if (string == null) { | ||
| stringify(); | ||
| } | ||
| return string; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
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,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 that parses and formats strings of the form 'myCamelCase' | ||
| * <p> | ||
| * CamelCase is a case where tokens are delimited by upper case Unicode characters. The very first | ||
| * token should begin with a lower case character, and any subsequent tokens begin with an | ||
| * upper case character. All remaining characters will be lower case or non cased. | ||
| * </p> | ||
| */ | ||
| public final class CamelCase extends UpperCaseDelimitedCase { | ||
|
|
||
| /** Constant reusable instance of this case. */ | ||
| public static final CamelCase INSTANCE = new CamelCase(); | ||
|
|
||
| /** | ||
| * Constructs new CamelCase instance. | ||
| */ | ||
|
|
||
theshoeshiner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private CamelCase() { | ||
| super(true); | ||
| } | ||
|
|
||
| } | ||
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.