Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

/*
 *  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. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author <a href="mailto:dev@labs.apache.org">Dungeon Project</a>
 */
public class Main {
    /**
     * A deep trim of a string remove whitespace from the ends as well as
     * excessive whitespace within the inside of the string between
     * non-whitespace characters. A deep trim reduces internal whitespace down
     * to a single space to perserve the whitespace separated tokenization order
     * of the String.
     * 
     * @param string
     *            the string to deep trim.
     * @return the trimmed string.
     */
    public static final String deepTrim(String string) {
        return deepTrim(string, false);
    }

    /**
     * This does the same thing as a trim but we also lowercase the string while
     * performing the deep trim within the same buffer. This saves us from
     * having to create multiple String and StringBuffer objects and is much
     * more efficient.
     * 
     * @see StringTools#deepTrim( String )
     */
    public static final String deepTrimToLower(String string) {
        return deepTrim(string, true);
    }

    /**
     * Put common code to deepTrim(String) and deepTrimToLower here.
     * 
     * @param str
     *            the string to deep trim
     * @param toLowerCase
     *            how to normalize for case: upper or lower
     * @return the deep trimmed string
     * @see StringTools#deepTrim( String )
     * 
     * TODO Replace the toCharArray() by substring manipulations
     */
    public static final String deepTrim(String str, boolean toLowerCase) {
        if ((null == str) || (str.length() == 0)) {
            return "";
        }

        char ch;
        char[] buf = str.toCharArray();
        char[] newbuf = new char[buf.length];
        boolean wsSeen = false;
        boolean isStart = true;
        int pos = 0;

        for (int i = 0; i < str.length(); i++) {
            ch = buf[i];

            // filter out all uppercase characters
            if (toLowerCase) {
                if (Character.isUpperCase(ch)) {
                    ch = Character.toLowerCase(ch);
                }
            }

            // Check to see if we should add space
            if (Character.isWhitespace(ch)) {
                // If the buffer has had characters added already check last
                // added character. Only append a spc if last character was
                // not whitespace.
                if (wsSeen) {
                    continue;
                } else {
                    wsSeen = true;

                    if (isStart) {
                        isStart = false;
                    } else {
                        newbuf[pos++] = ch;
                    }
                }
            } else {
                // Add all non-whitespace
                wsSeen = false;
                isStart = false;
                newbuf[pos++] = ch;
            }
        }

        return (pos == 0 ? "" : new String(newbuf, 0, (wsSeen ? pos - 1 : pos)));
    }

}