/*
 *  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.isis.applib.fixturescripts;


/**
 * Copied in from Isis' own <tt>StringExtensions</tt> class.
 */
final class StringUtil {

    private StringUtil(){}
    
    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".
     */
    static String asNaturalName2(final String name) {
    
        final int length = name.length();
    
        if (length <= 1) {
            return name.toUpperCase();// ensure first character is upper case
        }
    
        final StringBuffer naturalName = new StringBuffer(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();
    }
    
    static String asLowerDashed(String extendee) {
        return extendee.toLowerCase().replaceAll("\\s+", "-");
    }

}
