mergedoc.core.PatternCache.java Source code

Java tutorial

Introduction

Here is the source code for mergedoc.core.PatternCache.java

Source

/*
 * Copyright (c) 2003- Shinji Kashihara. All rights reserved.
 * This program are made available under the terms of the Common Public License
 * v1.0 which accompanies this distribution, and is available at cpl-v10.html.
 */
package mergedoc.core;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Pattern ???
 * ??????
 *
 * @author Shinji Kashihara
 */
public class PatternCache {

    /**  */
    private static final Log log = LogFactory.getLog(PatternCache.class);

    /** ?????Pattern  */
    private static final Map<String, Pattern> regexCache = new HashMap<String, Pattern>();

    /** ?Pattern  */
    private static final Map<String, Pattern> literalCache = new HashMap<String, Pattern>();

    /**
     * ?????
     */
    private PatternCache() {
    }

    /**
     * ?? Pattern ????
     * ? regex ???????? Pattern ????
     *
     * <p>?<br>
     * regex ???????? Pattern ?
     * ????????????????
     *
     * @param regex ??
     * @return Pattern 
     */
    public static Pattern getPattern(String regex) {

        Pattern pattern = regexCache.get(regex);
        if (pattern == null) {
            pattern = Pattern.compile(regex);
            regexCache.put(regex, pattern);
        }
        return pattern;
    }

    /**
     *  Pattern ????
     * ? target ???????? Pattern ????
     *
     * <p>?<br>
     * target ???????? Pattern ?
     * ????????????????
     *
     * @param target 
     * @return Pattern 
     */
    public static Pattern getLiteralPattern(String target) {

        Pattern pattern = literalCache.get(target);
        if (pattern == null) {
            pattern = Pattern.compile(target, Pattern.LITERAL);
            literalCache.put(target, pattern);
        }
        return pattern;
    }
}