KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > filter > NameMatch


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
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
20 package edu.umd.cs.findbugs.filter;
21
22 import java.util.regex.Pattern JavaDoc;
23
24 /**
25  * Matches a String value against a predefined specification.
26  *
27  * Matching can be done in three modes depending on ctor matchSpec argument.
28  *
29  * If matchSpec is null, match will succeed for any value (including empty String and null)
30  *
31  * If matchSpec starts with ~ character it will be treated as java.util.regex.Pattern, with the ~
32  * character ommited. The pattern will be matched against whole value (ie Matcher.match(), not Matcher.find())
33  *
34  * If matchSpec is a non-null String with any other initial charcter, exact matching using String.equals(String)
35  * will be performed.
36  *
37  * @author rafal@caltha.pl
38  */

39 public class NameMatch {
40     
41     private String JavaDoc exact;
42     
43     private Pattern JavaDoc pattern;
44     
45     public String JavaDoc getValue() {
46         if (exact != null) return exact;
47         return pattern.toString();
48     }
49     public NameMatch(String JavaDoc matchSpec) {
50         if (matchSpec != null) {
51             if (matchSpec.startsWith("~")) {
52                 pattern = Pattern.compile(matchSpec.substring(1));
53             } else {
54                 exact = matchSpec;
55             }
56         }
57     }
58     
59     public boolean match(String JavaDoc value) {
60         if (exact != null)
61             return exact.equals(value);
62         if (pattern != null)
63             return pattern.matcher(value).matches();
64         return true;
65     }
66     
67     @Override JavaDoc
68     public String JavaDoc toString() {
69         if (exact != null)
70             return "exact(" + exact + ")";
71         if (pattern != null)
72             return "regex(" + pattern.toString() + ")";
73         return "any()";
74     }
75 }
76
Popular Tags