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

org.opencds.cqf.cql.runtime.Tuple Maven / Gradle / Ivy

There is a newer version: 1.3.12.1
Show newest version
package org.opencds.cqf.cql.runtime;

import org.opencds.cqf.cql.elm.execution.EqualEvaluator;
import org.opencds.cqf.cql.elm.execution.EquivalentEvaluator;

import java.util.HashMap;

public class Tuple implements CqlType {

    protected HashMap elements;

    public Tuple() {
        this.elements = new HashMap<>();
    }

    public Object getElement(String key) {
        return elements.get(key);
    }

    public HashMap getElements() {
        if (elements == null) { return new HashMap<>(); }
        return elements;
    }

    public void setElements(HashMap elements) {
        this.elements = elements;
    }

    public Tuple withElements(HashMap elements) {
        setElements(elements);
        return this;
    }

    @Override
    public Boolean equivalent(Object other) {
        if (this.getElements().size() != ((Tuple) other).getElements().size()) {
            return false;
        }

        for (String key : ((Tuple) other).getElements().keySet()) {
            if (this.getElements().containsKey(key)) {
                Object areKeyValsSame = EquivalentEvaluator.equivalent(((Tuple) other).getElements().get(key), this.getElements().get(key));
                if (!(Boolean) areKeyValsSame) {
                    return false;
                }
            }
            else {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean equal(Object other) {
        if (this.getElements().size() != ((Tuple) other).getElements().size()) {
            return false;
        }

        for (String key : ((Tuple) other).getElements().keySet()) {
            if (this.getElements().containsKey(key)) {
                if (((Tuple) other).getElements().get(key) == null
                        && this.getElements().get(key) == null)
                {
                    continue;
                }
                Boolean equal = EqualEvaluator.equal(((Tuple) other).getElements().get(key), this.getElements().get(key));
                if (equal == null) { return null; }
                else if (!equal) { return false; }
            }
            else { return false; }
        }

        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("Tuple {\n");
        for (String key : elements.keySet()) {
            builder.append("\t").append(key).append(" -> ").append(elements.get(key)).append("\n");
        }
        return builder.append("}").toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy