Java List Min getMinWhitespace(final List lines)

Here you can find the source of getMinWhitespace(final List lines)

Description

Gets the minimum amount of leading whitespace in a text block.

License

Open Source License

Parameter

Parameter Description
lines list where each element represents an individual line of sample text

Return

minimum amount of leading whitespace

Declaration

public static int getMinWhitespace(final List<String> lines) 

Method Source Code

//package com.java2s;
/*/*  w ww  . jav  a  2 s  .com*/
 * Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.
 */

import java.util.List;

public class Main {
    /**
     * Gets the minimum amount of leading whitespace in a text block.
     *
     * @param lines
     *            list where each element represents an individual line of
     *            sample text
     *
     * @return minimum amount of leading whitespace
     */
    public static int getMinWhitespace(final List<String> lines) {
        int minWhitespace = 0;
        for (String line : lines) {
            int lineLeadingWhitespace = getLeadingWhitespace(line);
            if (lineLeadingWhitespace < minWhitespace || minWhitespace == 0) {
                minWhitespace = lineLeadingWhitespace;
            }
        }
        return minWhitespace;
    }

    /**
     * Returns the number of leading whitespace characters in a given string.
     *
     * @param str
     *            string to check
     * @return number of leading whitespace characters
     */
    public static int getLeadingWhitespace(final String str) {
        int pos = 0;
        while ((pos < str.length()) && (str.charAt(pos) == ' ')) {
            pos++;
        }
        return pos;
    }
}

Related

  1. getMin(List d)
  2. getMin(List numbers)
  3. getMinId(List ids)
  4. getMinimums(List data)
  5. getMinValue(List values)
  6. min(final List aList, final Double aDefalut)
  7. min(List values)
  8. min(List a, List b)
  9. min(List booleans)