/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.text;

import java.io.Serializable;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class Wildcard implements Serializable, Predicate<String> {
	
	private static final long serialVersionUID = 2L;	
	
	private final Pattern re;

	private Wildcard(Pattern re) {
		this.re = re;
	}
	
	public static Wildcard compile(String pattern) {
		return compile(pattern,0);
	}
	
	public static Wildcard compile(String pattern, int flags) {
        String p = StringUtils.escapeRE(pattern)
            .replace("\\*", ".*")
            .replace("\\?",".");
		return new Wildcard(Pattern.compile(p, flags));
	}
    
    public static boolean match(String pattern, String text) {
        return compile(pattern).test(text);
    }

	@Override // NOPMD
	public boolean test(String text) {
		return re.matcher(text).matches();
	}
	
	public Pattern asPattern() {
		return re;
	}
	
}
