| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IndentLevel |
|
| 1.375;1.375 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.indentation; | |
| 20 | ||
| 21 | import com.google.common.collect.Sets; | |
| 22 | import java.util.SortedSet; | |
| 23 | ||
| 24 | /** | |
| 25 | * Encapsulates representation of notion of expected indentation levels. | |
| 26 | * Provide a way to have multiple accaptable levels. | |
| 27 | * | |
| 28 | * @author o_sukhodolsky | |
| 29 | */ | |
| 30 | public class IndentLevel | |
| 31 | { | |
| 32 | /** set of acceptable indentation levels. */ | |
| 33 | 4103 | private final SortedSet<Integer> mLevels = Sets.newTreeSet(); |
| 34 | ||
| 35 | /** | |
| 36 | * Creates new instance with one accaptable indentation level. | |
| 37 | * @param aIndent accaptable indentation level. | |
| 38 | */ | |
| 39 | public IndentLevel(int aIndent) | |
| 40 | 266 | { |
| 41 | 266 | mLevels.add(aIndent); |
| 42 | 266 | } |
| 43 | ||
| 44 | /** | |
| 45 | * Creates new instance for nested structure. | |
| 46 | * @param aBase parent's level | |
| 47 | * @param aOffset offset from parent's level. | |
| 48 | */ | |
| 49 | public IndentLevel(IndentLevel aBase, int aOffset) | |
| 50 | 3837 | { |
| 51 | 3837 | for (Integer base : aBase.mLevels) { |
| 52 | 3876 | mLevels.add(base + aOffset); |
| 53 | } | |
| 54 | 3837 | } |
| 55 | ||
| 56 | /** | |
| 57 | * Checks wether we have more than one level. | |
| 58 | * @return wether we have more than one level. | |
| 59 | */ | |
| 60 | public final boolean isMultiLevel() | |
| 61 | { | |
| 62 | 1555 | return mLevels.size() > 1; |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Checks if given indentation is accaptable. | |
| 67 | * @param aIndent indentation to check. | |
| 68 | * @return true if givent indentation is acceptable, | |
| 69 | * false otherwise. | |
| 70 | */ | |
| 71 | public boolean accept(int aIndent) | |
| 72 | { | |
| 73 | 3215 | return (mLevels.contains(aIndent)); |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * @param aIndent indentation to check. | |
| 78 | * @return true if <code>aIndent</code> less then minimal of | |
| 79 | * accaptable indentation levels, false otherwise. | |
| 80 | */ | |
| 81 | public boolean gt(int aIndent) | |
| 82 | { | |
| 83 | 524 | return ((mLevels.first()).intValue() > aIndent); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Adds one more acceptable indentation level. | |
| 88 | * @param aIndent new acceptable indentation. | |
| 89 | */ | |
| 90 | public void addAcceptedIndent(int aIndent) | |
| 91 | { | |
| 92 | 3 | mLevels.add(aIndent); |
| 93 | 3 | } |
| 94 | ||
| 95 | /** | |
| 96 | * Adds one more acceptable indentation level. | |
| 97 | * @param aIndent new acceptable indentation. | |
| 98 | */ | |
| 99 | public void addAcceptedIndent(IndentLevel aIndent) | |
| 100 | { | |
| 101 | 13 | mLevels.addAll(aIndent.mLevels); |
| 102 | 13 | } |
| 103 | ||
| 104 | @Override | |
| 105 | public String toString() | |
| 106 | { | |
| 107 | 455 | if (mLevels.size() == 1) { |
| 108 | 449 | return mLevels.first().toString(); |
| 109 | } | |
| 110 | ||
| 111 | 6 | return mLevels.toString(); |
| 112 | } | |
| 113 | } |