1 /* 2 * Copyright 2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.maven.plugin.jboss.modules.descriptor; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 import org.apache.commons.lang.builder.ToStringBuilder; 22 23 import de.smartics.util.lang.Arg; 24 25 /** 26 * Descriptor to define the rules for matching modules to be matched to have 27 * additional information applied. 28 */ 29 public final class ModuleMatcher 30 { 31 // ********************************* Fields ********************************* 32 33 // --- constants ------------------------------------------------------------ 34 35 // --- members -------------------------------------------------------------- 36 37 /** 38 * The list of inclusions. 39 */ 40 private final List<ModuleClusion> includes; 41 42 /** 43 * The list of exclusions. 44 */ 45 private final List<ModuleClusion> excludes; 46 47 // ****************************** Initializer ******************************* 48 49 // ****************************** Constructors ****************************** 50 51 private ModuleMatcher(final Builder builder) 52 { 53 includes = builder.includes; 54 excludes = builder.excludes; 55 } 56 57 // ****************************** Inner Classes ***************************** 58 59 /** 60 * Builds instances of {@link ModuleMatcher}. 61 */ 62 public static final class Builder 63 { 64 // ******************************** Fields ******************************** 65 66 // --- constants ---------------------------------------------------------- 67 68 // --- members ------------------------------------------------------------ 69 70 /** 71 * The list of inclusions. 72 */ 73 private final List<ModuleClusion> includes = new ArrayList<ModuleClusion>(); 74 75 /** 76 * The list of exclusions. 77 */ 78 private final List<ModuleClusion> excludes = new ArrayList<ModuleClusion>(); 79 80 // ***************************** Initializer ****************************** 81 82 // ***************************** Constructors ***************************** 83 84 // ***************************** Inner Classes **************************** 85 86 // ******************************** Methods ******************************* 87 88 // --- init --------------------------------------------------------------- 89 90 // --- get&set ------------------------------------------------------------ 91 92 // --- business ----------------------------------------------------------- 93 94 /** 95 * Adds an include to the list of includes. 96 * 97 * @param include the include to add. 98 * @throws NullPointerException if {@code include} is <code>null</code>. 99 */ 100 public void addInclude(final ModuleClusion include) 101 throws NullPointerException 102 { 103 includes.add(Arg.checkNotNull("include", include)); 104 } 105 106 /** 107 * Adds an exclude to the list of excludes. 108 * 109 * @param exclude the exclude to add. 110 * @throws NullPointerException if {@code exclude} is <code>null</code>. 111 */ 112 public void addExclude(final ModuleClusion exclude) 113 throws NullPointerException 114 { 115 excludes.add(Arg.checkNotNull("exclude", exclude)); 116 } 117 118 /** 119 * Builds an instance of {@link ModuleMatcher}. 120 * 121 * @return the instance. 122 */ 123 public ModuleMatcher build() 124 { 125 return new ModuleMatcher(this); 126 } 127 128 // --- object basics ------------------------------------------------------ 129 } 130 131 // ********************************* Methods ******************************** 132 133 // --- init ----------------------------------------------------------------- 134 135 // --- get&set -------------------------------------------------------------- 136 137 /** 138 * Returns the list of inclusions. 139 * 140 * @return the list of inclusions. 141 */ 142 public List<ModuleClusion> getIncludes() 143 { 144 return includes; 145 } 146 147 /** 148 * Returns the list of exclusions. 149 * 150 * @return the list of exclusions. 151 */ 152 public List<ModuleClusion> getExcludes() 153 { 154 return excludes; 155 } 156 157 // --- business ------------------------------------------------------------- 158 159 /** 160 * Checks if the matcher matches with the given module name. 161 * 162 * @param name the module name to match. 163 * @return <code>true</code> on a match, <code>false</code> otherwise. 164 */ 165 public boolean matches(final String name) 166 { 167 final boolean included = matches(includes, name, true); 168 if (included) 169 { 170 final boolean excluded = matches(excludes, name, false); 171 return !excluded; 172 } 173 174 return included; 175 } 176 177 private boolean matches(final List<ModuleClusion> cludes, final String name, 178 final boolean defaultValue) 179 { 180 if (cludes.isEmpty()) 181 { 182 return defaultValue; 183 } 184 for (final ModuleClusion clusion : cludes) 185 { 186 if (clusion.match(name)) 187 { 188 return true; 189 } 190 } 191 return false; 192 } 193 194 // --- object basics -------------------------------------------------------- 195 196 /** 197 * {@inheritDoc} 198 * <p> 199 * Provides the properties via reflection for displaying debug information. 200 * </p> 201 */ 202 @Override 203 public String toString() 204 { 205 return ToStringBuilder.reflectionToString(this); 206 } 207 }