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

org.eclipse.edc.query.LikeOperatorPredicate Maven / Gradle / Ivy

/*
 *  Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
 *
 *  This program and the accompanying materials are made available under the
 *  terms of the Apache License, Version 2.0 which is available at
 *  https://www.apache.org/licenses/LICENSE-2.0
 *
 *  SPDX-License-Identifier: Apache-2.0
 *
 *  Contributors:
 *       Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
 *
 */

package org.eclipse.edc.query;

import org.eclipse.edc.spi.query.OperatorPredicate;

import java.util.regex.Pattern;

public class LikeOperatorPredicate implements OperatorPredicate {

    @Override
    public boolean test(Object property, Object operandRight) {
        if (operandRight instanceof String stringOperand) {
            var regexPattern = Pattern.quote(stringOperand)
                    .replace("%", "\\E.*\\Q")
                    .replace("_", "\\E.\\Q");

            regexPattern = "^" + regexPattern + "$";

            return Pattern.compile(regexPattern).matcher(property.toString()).matches();
        }

        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy