| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultConfiguration |
|
| 1.3;1.3 |
| 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; | |
| 20 | ||
| 21 | import com.google.common.collect.ImmutableMap; | |
| 22 | ||
| 23 | import com.google.common.collect.Lists; | |
| 24 | import com.google.common.collect.Maps; | |
| 25 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | |
| 26 | import com.puppycrawl.tools.checkstyle.api.Configuration; | |
| 27 | ||
| 28 | import java.util.List; | |
| 29 | import java.util.Map; | |
| 30 | import java.util.Set; | |
| 31 | ||
| 32 | /** | |
| 33 | * Default implementation of the Configuration interface. | |
| 34 | * @author lkuehne | |
| 35 | */ | |
| 36 | public final class DefaultConfiguration implements Configuration | |
| 37 | { | |
| 38 | /** Required for serialization. */ | |
| 39 | private static final long serialVersionUID = 1157875385356127169L; | |
| 40 | ||
| 41 | /** The name of this configuration */ | |
| 42 | private final String mName; | |
| 43 | ||
| 44 | /** the list of child Configurations */ | |
| 45 | 1852 | private final List<Configuration> mChildren = Lists.newArrayList(); |
| 46 | ||
| 47 | /** the map from attribute names to attribute values */ | |
| 48 | 1852 | private final Map<String, String> mAttributeMap = Maps.newHashMap(); |
| 49 | ||
| 50 | /** the map containing custom messages. */ | |
| 51 | 1852 | private final Map<String, String> mMessages = Maps.newHashMap(); |
| 52 | ||
| 53 | /** | |
| 54 | * Instantiates a DefaultConfiguration. | |
| 55 | * @param aName the name for this DefaultConfiguration. | |
| 56 | */ | |
| 57 | public DefaultConfiguration(String aName) | |
| 58 | 1852 | { |
| 59 | 1852 | mName = aName; |
| 60 | 1852 | } |
| 61 | ||
| 62 | /** {@inheritDoc} */ | |
| 63 | public String[] getAttributeNames() | |
| 64 | { | |
| 65 | 1824 | final Set<String> keySet = mAttributeMap.keySet(); |
| 66 | 1824 | return keySet.toArray(new String[keySet.size()]); |
| 67 | } | |
| 68 | ||
| 69 | /** {@inheritDoc} */ | |
| 70 | public String getAttribute(String aName) throws CheckstyleException | |
| 71 | { | |
| 72 | 1204 | if (!mAttributeMap.containsKey(aName)) { |
| 73 | // TODO: i18n | |
| 74 | 37 | throw new CheckstyleException( |
| 75 | "missing key '" + aName + "' in " + getName()); | |
| 76 | } | |
| 77 | 1167 | return mAttributeMap.get(aName); |
| 78 | } | |
| 79 | ||
| 80 | /** {@inheritDoc} */ | |
| 81 | public Configuration[] getChildren() | |
| 82 | { | |
| 83 | 1817 | return mChildren.toArray( |
| 84 | new Configuration[mChildren.size()]); | |
| 85 | } | |
| 86 | ||
| 87 | /** {@inheritDoc} */ | |
| 88 | public String getName() | |
| 89 | { | |
| 90 | 1273 | return mName; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Makes a configuration a child of this configuration. | |
| 95 | * @param aConfiguration the child configuration. | |
| 96 | */ | |
| 97 | public void addChild(Configuration aConfiguration) | |
| 98 | { | |
| 99 | 1244 | mChildren.add(aConfiguration); |
| 100 | 1244 | } |
| 101 | ||
| 102 | /** | |
| 103 | * Removes a child of this configuration. | |
| 104 | * @param aConfiguration the child configuration to remove. | |
| 105 | */ | |
| 106 | public void removeChild(final Configuration aConfiguration) | |
| 107 | { | |
| 108 | 0 | mChildren.remove(aConfiguration); |
| 109 | 0 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Adds an attribute to this configuration. | |
| 113 | * @param aName the name of the attribute. | |
| 114 | * @param aValue the value of the attribute. | |
| 115 | */ | |
| 116 | public void addAttribute(String aName, String aValue) | |
| 117 | { | |
| 118 | 1179 | final String current = mAttributeMap.put(aName, aValue); |
| 119 | 1179 | if (null == current) { |
| 120 | 1177 | mAttributeMap.put(aName, aValue); |
| 121 | } | |
| 122 | else { | |
| 123 | 2 | mAttributeMap.put(aName, current + "," + aValue); |
| 124 | } | |
| 125 | 1179 | } |
| 126 | ||
| 127 | /** | |
| 128 | * Adds a custom message to this configuration. | |
| 129 | * @param aKey the message key | |
| 130 | * @param aValue the custom message pattern | |
| 131 | */ | |
| 132 | public void addMessage(String aKey, String aValue) | |
| 133 | { | |
| 134 | 6 | mMessages.put(aKey, aValue); |
| 135 | 6 | } |
| 136 | ||
| 137 | /** | |
| 138 | * Returns an unmodifiable map instance containing the custom messages | |
| 139 | * for this configuration. | |
| 140 | * @return unmodifiable map containing custom messages | |
| 141 | */ | |
| 142 | public ImmutableMap<String, String> getMessages() | |
| 143 | { | |
| 144 | 3429 | return ImmutableMap.copyOf(mMessages); |
| 145 | } | |
| 146 | } |