001/* 002 * Copyright 2023 the original author or authors. 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.valueobjects.util; 017 018import static de.cuioss.tools.string.MoreStrings.isEmpty; 019 020import de.cuioss.test.generator.Generators; 021import lombok.experimental.UtilityClass; 022 023/** 024 * Simple Helper that shuffle lower / uppercase for strings. 025 * 026 * @author Oliver Wolff 027 * 028 */ 029@UtilityClass 030public class StringCaseShuffler { 031 032 /** 033 * Shuffles the case of a given string. Shuffling is done for every Character 034 * that is a {@link Character#isAlphabetic(int)} 035 * 036 * @param toShuffle if {@code null} or empty the given String will be returned 037 * @return the shuffled string 038 */ 039 public static String shuffleCase(String toShuffle) { 040 if (isEmpty(toShuffle)) { 041 return toShuffle; 042 } 043 var result = new StringBuilder(); 044 for (char c : toShuffle.toCharArray()) { 045 result.append(handleSingleCharacter(c)); 046 } 047 return result.toString(); 048 } 049 050 private static char handleSingleCharacter(char c) { 051 if (!Character.isAlphabetic(c)) { 052 return c; 053 } 054 if (Generators.booleans().next().booleanValue()) { 055 return Character.toUpperCase(c); 056 } 057 return Character.toLowerCase(c); 058 } 059}