ottlinger commented on code in PR #257: URL: https://github.com/apache/creadur-rat/pull/257#discussion_r1614871363
########## apache-rat-core/src/main/java/org/apache/rat/utils/CasedString.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.rat.utils; + +import org.apache.commons.text.WordUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Handles converting from one string case to another (e.g. camel case to snake case). + * @since 1.13.0 + */ +public class CasedString { + /** the string of the cased format. */ + private final String string; + /** the case of the string. */ + private final StringCase stringCase; + + /** + * A method to join camel string fragments together. + */ + private static final Function<String[], String> CAMEL_JOINER = a -> { + StringBuilder sb = new StringBuilder(a[0].toLowerCase(Locale.ROOT)); + + for (int i = 1; i < a.length; i++) { + sb.append(WordUtils.capitalize(a[i].toLowerCase(Locale.ROOT))); + } + return sb.toString(); + }; + + /** + * An enumeration of supported string cases. These cases tag strings as having a specific format. + */ + public enum StringCase { + /** + * Camel case tags strings like 'CamelCase' or 'camelCase'. This conversion forces the first character to + * lower case. If specific capitalization rules are required use {@code WordUtils.capitalize()} to set the first + * character of the string. + */ + CAMEL(Character::isUpperCase, true, CAMEL_JOINER), + /** + * Snake case tags strings like 'Snake_Case'. This conversion does not change the capitalization of any characters + * in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase}, + * {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required. + */ + SNAKE(c -> c == '_', false, a -> String.join("_", a)), + /** + * Kebab case tags strings like 'kebab-case'. This conversion does not change the capitalization of any characters + * in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase}, + * {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required. + */ + KEBAB(c -> c == '-', false, a -> String.join("-", a)), + + /** + * Phrase case tags phrases of words like 'phrase case'. This conversion does not change the capitalization of any characters + * in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase}, + * {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required. + */ + PHRASE(Character::isWhitespace, false, a -> String.join(" ", a)), + + /** + * Dot case tags phrases of words like 'phrase.case'. This conversion does not change the capitalization of any characters + * in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase}, Review Comment: same as above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@creadur.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org