content: /*
   *  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.causeway.commons.internal.base;
  
  import org.jspecify.annotations.Nullable;
  
  import lombok.experimental.UtilityClass;
  
  /**
   * package private utility for {@link _Strings}
   */
  @UtilityClass
  class _Strings_NaturalName {
  
      private static final char SPACE = ' ';
      /**
       * Returns a word spaced version of the specified name, so there are spaces
       * between the words, where each word starts with a capital letter. E.g.,
       * "NextAvailableDate" is returned as "Next Available Date".
       *
       * @param name
       * @param handleNestedClassNames whether to handle any nested class names, eg 'Foo$Bar'
       */
      String naturalName(@Nullable String name, final boolean handleNestedClassNames) {
  
          if(name==null) return null;
  
          if(handleNestedClassNames) {
              // handle any nested class names, eg 'Foo$Bar'
              final int idx = name.lastIndexOf("$");
              if(idx != -1) {
                  name = name.substring(idx+1);
              }
          }
  
          final int length = name.length();
  
          if (length <= 1) {
              return name.toUpperCase();// ensure first character is upper case
          }
  
          final StringBuilder naturalName = new StringBuilder(length);
  
          char previousCharacter;
          char character = Character.toUpperCase(name.charAt(0));// ensure first
          // character is
          // upper case
          naturalName.append(character);
          char nextCharacter = name.charAt(1);
  
          for (int pos = 2; pos < length; pos++) {
              previousCharacter = character;
              character = nextCharacter;
              nextCharacter = name.charAt(pos);
  
              if (previousCharacter != SPACE) {
                  if (Character.isUpperCase(character) && !Character.isUpperCase(previousCharacter)) {
                      naturalName.append(SPACE);
                  }
                  if (Character.isUpperCase(character) && Character.isLowerCase(nextCharacter) && Character.isUpperCase(previousCharacter)) {
                      naturalName.append(SPACE);
                  }
                  if (Character.isDigit(character) && !Character.isDigit(previousCharacter)) {
                      naturalName.append(SPACE);
                  }
              }
              naturalName.append(character);
          }
          naturalName.append(nextCharacter);
          return naturalName.toString();
      }
  }
  