View Javadoc

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.regex.Matcher;
19  import java.util.regex.Pattern;
20  import java.util.regex.PatternSyntaxException;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import de.smartics.util.lang.Arg;
25  
26  /**
27   * Clusion information for including and excluding on a module name.
28   */
29  public final class ModuleClusion
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The name to match for this clusion.
39     */
40    private final String name;
41  
42    /**
43     * The pattern to match. May be <code>null</code>.
44     */
45    private final Pattern namePattern;
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Default constructor.
53     *
54     * @param name the name to match for this clusion.
55     * @throws IllegalArgumentException if {@code name} is blank, but not
56     *           <code>null</code>.
57     */
58    public ModuleClusion(final String name) throws IllegalArgumentException
59    {
60      this.name = Arg.checkNotBlankExceptNull("name", name);
61      this.namePattern = compilePattern(name);
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    private static Pattern compilePattern(final String pattern)
71    {
72      if (StringUtils.isNotBlank(pattern))
73      {
74        try
75        {
76          return Pattern.compile(pattern);
77        }
78        catch (final PatternSyntaxException e)
79        {
80          // ignore
81        }
82      }
83      return null;
84    }
85  
86    // --- get&set --------------------------------------------------------------
87  
88    /**
89     * Returns the name to match for this clusion.
90     *
91     * @return the name to match for this clusion.
92     */
93    public String getName()
94    {
95      return name;
96    }
97  
98    // --- business -------------------------------------------------------------
99  
100   /**
101    * Compares the name of the clusion with the given {@code moduleName}.
102    *
103    * @param moduleName the name to match with.
104    * @return <code>true</code> on a match, <code>false</code> otherwise.
105    */
106   public boolean match(final String moduleName)
107   {
108     if (name == null)
109     {
110       return true;
111     }
112 
113     if (namePattern != null)
114     {
115       final Matcher matcher = namePattern.matcher(moduleName);
116       return matcher.matches();
117     }
118     else
119     {
120       return name.equals(moduleName);
121     }
122   }
123 
124   // --- object basics --------------------------------------------------------
125 
126   @Override
127   public String toString()
128   {
129     return name;
130   }
131 }