(no commit message)
[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 class ConfigItem<ValueType> {
25
26     private Pattern _pattern;
27
28     private ValueType _value;
29
30     /**
31      * Constructs the item. 
32      * @param aPattern Pattern. 
33      * @param aValue Value. 
34      */
35     protected ConfigItem(String aPattern, ValueType aValue) {
36         _pattern = Pattern.compile(aPattern);
37         _value = aValue;
38     }
39
40     /**
41      * Returns the object in case the value matches. 
42      * @param aValue Value to match. 
43      * @return Object in case there is a match, null otherwise. 
44      */
45     protected ValueType match(String aValue) {
46         if (!_pattern.matcher(aValue).matches()) {
47             return null;
48         }
49         return _value;
50     }
51
52 }