001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.sizes;
021
022import java.io.File;
023import java.util.List;
024
025import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
026
027/**
028 * <p>
029 * Checks for long source files.
030 * </p>
031 * <p>
032 * Rationale: If a source file becomes very long it is hard to understand.
033 * Therefore long classes should usually be refactored into several
034 * individual classes that focus on a specific task.
035 * </p>
036 * <p>
037 * The default maximum file length is 2000 lines. To change the maximum
038 * number of lines, set property max.
039 * </p>
040 * <p>
041 * An example of how to configure the check is:
042 * </p>
043 * <pre>
044 * &lt;module name="FileLength"/&gt;
045 * </pre>
046 * <p>
047 * An example of how to configure the check so that it accepts files with at
048 * most 1500 lines is:
049 * </p>
050 * <pre>
051 * &lt;module name="FileLength"&gt;
052 *    &lt;property name="max" value="1500"/&gt;
053 * &lt;/module&gt;
054 * </pre>
055 * @author Lars Kühne
056 */
057public class FileLengthCheck extends AbstractFileSetCheck {
058
059    /**
060     * A key is pointing to the warning message text in "messages.properties"
061     * file.
062     */
063    public static final String MSG_KEY = "maxLen.file";
064
065    /** Default maximum number of lines. */
066    private static final int DEFAULT_MAX_LINES = 2000;
067
068    /** The maximum number of lines. */
069    private int max = DEFAULT_MAX_LINES;
070
071    @Override
072    protected void processFiltered(File file, List<String> lines) {
073        if (lines.size() > max) {
074            log(1, MSG_KEY, lines.size(), max);
075        }
076    }
077
078    /**
079     * Sets the maximum length of a Java source file.
080     * @param length the maximum length of a Java source file
081     */
082    public void setMax(int length) {
083        max = length;
084    }
085
086}