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

com.sap.cloud.security.ams.dcn.engine.IsNullOperatorNode Maven / Gradle / Ivy

Go to download

Client Library for integrating Jakarta EE applications with SAP Authorization Management Service (AMS)

The newest version!
/************************************************************************
 * © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cloud.security.ams.dcn.engine;

import static com.sap.cloud.security.ams.dcl.client.el.Call.QualifiedNames.IS_NULL;

import com.sap.cloud.security.ams.dcl.client.el.Call;
import java.util.Objects;
import java.util.Optional;

/** {@link EvaluationNode} implementation of the IS_NULL operator. */
public class IsNullOperatorNode extends EvaluationNode {
  private final EvaluationNode subNode;

  /**
   * Creates a new IS_NULL operator node for the given sub node.
   *
   * @param subNode the node to apply the IS_NULL operator to
   */
  IsNullOperatorNode(EvaluationNode subNode) {
    this.subNode = subNode;
  }

  @Override
  public EvaluationNode deepSimplify() {
    return new IsNullOperatorNode(subNode.deepSimplify());
  }

  /**
   * This method returns {@link ValueNode#TRUE} if the sub node evaluates to {@link
   * ValueNode#UNSET}, it returns {@link ValueNode#IGNORE} if the sub node evaluates to {@link
   * ValueNode#IGNORE}, and it returns {@link ValueNode#FALSE} if the sub node evaluates to any
   * other {@link ValueNode}. Otherwise, it returns a new {@link IsNullOperatorNode} instance for
   * the evaluation result of the sub node.
   *
   * @param valueAccessor accessor for attribute values
   * @return the result of the IS_NULL operator
   */
  @Override
  public EvaluationNode evaluate(ValueAccessor valueAccessor) {
    EvaluationNode node = subNode.evaluate(valueAccessor);

    if (ValueNode.UNSET.equals(node)) {
      return ValueNode.TRUE;
    }

    if (ValueNode.IGNORE.equals(node)) {
      return ValueNode.IGNORE;
    }

    Optional value = node.value();
    if (value.isEmpty()) {
      return new IsNullOperatorNode(node);
    }

    return ValueNode.FALSE;
  }

  /**
   * Converts this node to a condition object by returning a {@link Call} object with name {@link
   * Call.QualifiedNames#IS_NULL} and the condition of the sub node as argument.
   *
   * @return the condition object
   */
  @Override
  public Object toCondition() {
    return Call.create(IS_NULL, subNode.toCondition());
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    IsNullOperatorNode that = (IsNullOperatorNode) o;
    return Objects.equals(subNode, that.subNode);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(subNode);
  }

  @Override
  public String toString() {
    return "IsNull{" + subNode + '}';
  }
}