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

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

There is a newer version: 0.10.1
Show newest version
/*
 *  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.List;
import java.util.Objects;

import static java.util.Optional.ofNullable;

public class EqualOperatorPredicate implements OperatorPredicate {

    @Override
    public boolean test(Object property, Object operandRight) {
        if (property == null) {
            return operandRight == null;
        }
        if (property.getClass().isEnum()) {
            var enumProperty = (Enum) property;
            if (operandRight instanceof String) {
                return Objects.equals(enumProperty.name(), operandRight);
            } else if (operandRight instanceof Number) {
                return Objects.equals(enumProperty.ordinal(), operandRight);
            }
        }

        if (property instanceof Number c1 && operandRight instanceof Number c2) {
            // interpret as double to not lose any precision
            return Double.compare(c1.doubleValue(), c2.doubleValue()) == 0;
        }

        if (property instanceof List list) {
            return list.stream().anyMatch(it -> Objects.equals(it, operandRight));
        } else if (property instanceof Boolean booleanProperty) {
            return ofNullable(operandRight)
                    .map(Object::toString)
                    .map(Boolean::parseBoolean)
                    .map(booleanOperand -> booleanOperand == booleanProperty)
                    .orElse(false);
        }
        return Objects.equals(property, operandRight);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy