All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.apaq.framework.criteria.rules.LikeRule Maven / Gradle / Ivy

/*
 * CrudContainer
 * Copyright (C) 2011 by Apaq
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package dk.apaq.framework.criteria.rules;

import dk.apaq.framework.criteria.Rule.FiltrationItem;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 */
public class LikeRule extends AbstractSpecificRule {

    public static final char WILDCARD_MULTI = '*';
    public static final char WILDCARD_SINGLE = '?';
    private static final Pattern GRAB_SP_CHARS = Pattern.compile("([\\\\*+\\[\\](){}\\$.?\\^|])");
    private final float precision;
    private final boolean caseSensitive;
    private final String value;

    public LikeRule(String propertyId, String value, float precision, boolean caseSensitive) {
        super(propertyId);
        this.value = value;

        if (precision > 1) {
            precision = 1.0F;
        }

        if (precision < 0) {
            precision = 0.0F;
        }

        this.precision = precision;
        this.caseSensitive = caseSensitive;
    }

    public LikeRule(String propertyId, String value, boolean caseSensitive) {
        this(propertyId, value, 1.0F, caseSensitive);
    }

    public LikeRule(String propertyId, String value) {
        this(propertyId, value, 1.0F, true);
    }

    public float getPrecision() {
        return precision;
    }

    public boolean isPrecise() {
        return 1.0F == precision;
    }

    public String getValue() {
        return value;
    }

    public boolean isCaseSensitive() {
        return caseSensitive;
    }

    @Override
    public boolean passesRule(FiltrationItem item) {
        Object value = item.getFiltrationProperty(getPropertyId());
        if (!(value instanceof String)) {
            // We can only handle strings
            return false;
        }
        String colValue = (String) value;

        String pattern = escapeRegex(getValue());
        pattern = pattern.replace("\\*", ".*");
        pattern = pattern.replace("\\%", ".*");

        if (isCaseSensitive()) {
            return colValue.matches(pattern);
        }
        return colValue.toUpperCase().matches(pattern.toUpperCase());
    }

    private static String escapeRegex(String inStr) {
        Matcher match = GRAB_SP_CHARS.matcher(inStr);
        return match.replaceAll("\\\\$1");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy