Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * 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.
 */

public class Main {
    /**
     * Wraps the supplied text to the specified line length.
     * 
     * @lineLength the maximum length of each line in the returned string (not
     *             including indent if specified).
     * @indent optional number of whitespace characters to prepend to each line
     *         before the text.
     * @linePrefix optional string to append to the indent (before the text).
     * @returns the supplied text wrapped so that no line exceeds the specified
     *          line length + indent, optionally with indent and prefix applied
     *          to each line.
     */
    private static String lineWrap(String s, int lineLength, Integer indent, String linePrefix) {
        if (s == null)
            return null;

        StringBuilder sb = new StringBuilder();
        int lineStartPos = 0;
        int lineEndPos;
        boolean firstLine = true;
        while (lineStartPos < s.length()) {
            if (!firstLine)
                sb.append("\n");
            else
                firstLine = false;

            if (lineStartPos + lineLength > s.length())
                lineEndPos = s.length() - 1;
            else {
                lineEndPos = lineStartPos + lineLength - 1;
                while (lineEndPos > lineStartPos && (s.charAt(lineEndPos) != ' ' && s.charAt(lineEndPos) != '\t'))
                    lineEndPos--;
            }
            sb.append(buildWhitespace(indent));
            if (linePrefix != null)
                sb.append(linePrefix);

            sb.append(s.substring(lineStartPos, lineEndPos + 1));
            lineStartPos = lineEndPos + 1;
        }
        return sb.toString();
    }

    private static String buildWhitespace(int numChars) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numChars; i++)
            sb.append(" ");
        return sb.toString();
    }
}