1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.utils.internal; 17 18 import static java.util.stream.Collectors.joining; 19 20 import java.util.stream.Stream; 21 import software.amazon.awssdk.annotations.SdkInternalApi; 22 import software.amazon.awssdk.utils.StringUtils; 23 24 /** 25 * Internal class used by the code generator and release scripts to produce sanitized names. 26 * 27 * In the future, we should consider adding a build-utils module for tools used at build time 28 * by multiple modules so that we don't have these at runtime when they aren't needed. 29 */ 30 @SdkInternalApi 31 public final class CodegenNamingUtils { 32 CodegenNamingUtils()33 private CodegenNamingUtils() { 34 } 35 splitOnWordBoundaries(String toSplit)36 public static String[] splitOnWordBoundaries(String toSplit) { 37 String result = toSplit; 38 39 // All non-alphanumeric characters are spaces 40 result = result.replaceAll("[^A-Za-z0-9]+", " "); // acm-success -> "acm success" 41 42 // If a number has a standalone v in front of it, separate it out (version). 43 result = result.replaceAll("([^a-z]{2,})v([0-9]+)", "$1 v$2 ") // TESTv4 -> "TEST v4 " 44 .replaceAll("([^A-Z]{2,})V([0-9]+)", "$1 V$2 "); // TestV4 -> "Test V4 " 45 46 // Add a space between camelCased words 47 result = String.join(" ", result.split("(?<=[a-z])(?=[A-Z]([a-zA-Z]|[0-9]))")); // AcmSuccess -> // "Acm Success" 48 49 // Add a space after acronyms 50 result = result.replaceAll("([A-Z]+)([A-Z][a-z])", "$1 $2"); // ACMSuccess -> "ACM Success" 51 52 // Add space after a number in the middle of a word 53 result = result.replaceAll("([0-9])([a-zA-Z])", "$1 $2"); // s3ec2 -> "s3 ec2" 54 55 // Remove extra spaces - multiple consecutive ones or those and the beginning/end of words 56 result = result.replaceAll(" +", " ") // "Foo Bar" -> "Foo Bar" 57 .trim(); // " Foo " -> Foo 58 59 return result.split(" "); 60 } 61 pascalCase(String word)62 public static String pascalCase(String word) { 63 return Stream.of(splitOnWordBoundaries(word)).map(StringUtils::lowerCase).map(StringUtils::capitalize).collect(joining()); 64 } 65 pascalCase(String... words)66 public static String pascalCase(String... words) { 67 return Stream.of(words).map(StringUtils::lowerCase).map(StringUtils::capitalize).collect(joining()); 68 } 69 lowercaseFirstChar(String word)70 public static String lowercaseFirstChar(String word) { 71 char[] chars = word.toCharArray(); 72 chars[0] = Character.toLowerCase(chars[0]); 73 return String.valueOf(chars); 74 } 75 } 76