e42a16c439dc84fe0f4820365a893f74673b0642
[utils] / crawler / basic / src / main / java / org / wamblee / crawler / impl / ConfigItem.java
1 /*
2  * Copyright 2005 the original author or authors.
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
17 package org.wamblee.crawler.impl;
18
19 import java.util.regex.Pattern;
20
21 /**
22  * Configuration item for obtaining an object in case a pattern matches.
23  *
24  * @author Erik Brakkee
25  */
26 class ConfigItem<ValueType> {
27
28     private Pattern _pattern;
29
30     private ValueType _value;
31
32     /**
33      * Constructs the item. 
34      * @param aPattern Pattern. 
35      * @param aValue Value. 
36      */
37     protected ConfigItem(String aPattern, ValueType aValue) {
38         _pattern = Pattern.compile(aPattern);
39         _value = aValue;
40     }
41
42     /**
43      * Returns the object in case the value matches. 
44      * @param aValue Value to match. 
45      * @return Object in case there is a match, null otherwise. 
46      */
47     protected ValueType match(String aValue) {
48         if (!_pattern.matcher(aValue).matches()) {
49             return null;
50         }
51         return _value;
52     }
53
54 }