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

com.att.research.xacmlatt.pdp.policy.ExpressionResult Maven / Gradle / Ivy

The newest version!
/*
 *
 *          Copyright (c) 2013,2019  AT&T Knowledge Ventures
 *                     SPDX-License-Identifier: MIT
 */
package com.att.research.xacmlatt.pdp.policy;

import java.util.Iterator;

import com.att.research.xacml.api.AttributeValue;
import com.att.research.xacml.api.Status;
import com.att.research.xacml.std.StdStatus;

/**
 * ExpressionResult is the object returned by the evaluate method of {@link com.att.research.xacmlatt.pdp.policy.Expression}
 * objects.
 * 
 * @author car
 * @version $Revision: 1.1 $
 */
public abstract class ExpressionResult implements FunctionArgument {
	private Status	status;

	/**
	 * ExpressionResultError extends ExpressionResult to represent error results.
	 * 
	 * @author car
	 * @version $Revision: 1.1 $
	 */
	private static class ExpressionResultError extends ExpressionResult {
		public ExpressionResultError(Status statusIn) {
			super(statusIn);
		}

		@Override
		public AttributeValue getValue() {
			return null;
		}

		@Override
		public boolean isBag() {
			return false;
		}

		@Override
		public Bag getBag() {
			return null;
		}
	}
	
	/**
	 * ExpressionResultSingle extends ExpressionResult to represent results with a single value.
	 * 
	 * @author car
	 * @version $Revision: 1.1 $
	 */
	private static class ExpressionResultSingle extends ExpressionResult {
		AttributeValue	attributeValue;
		
		public ExpressionResultSingle(AttributeValue attributeValueIn) {
			super(StdStatus.STATUS_OK);
			this.attributeValue	= attributeValueIn;
		}

		@Override
		public AttributeValue getValue() {
			return this.attributeValue;
		}

		@Override
		public boolean isBag() {
			return false;
		}

		@Override
		public Bag getBag() {
			return null;
		}
	}
	
	private static class ExpressionResultBag extends ExpressionResult {
		private Bag bag;
		
		public ExpressionResultBag(Bag bagIn) {
			super(StdStatus.STATUS_OK);
			this.bag	= bagIn;
		}
		
		@Override
		public AttributeValue getValue() {
			Iterator> iter	= this.bag.getAttributeValues();
			if (iter != null && iter.hasNext()) {
				return iter.next();
			} else {
				return null;
			}
		}

		@Override
		public boolean isBag() {
			return true;
		}

		@Override
		public Bag getBag() {
			return this.bag;
		}
	}
	
	private static class ExpressionResultEmptyBag extends ExpressionResult {
		public ExpressionResultEmptyBag() {
			super(StdStatus.STATUS_OK);
		}

		@Override
		public AttributeValue getValue() {
			return null;
		}

		@Override
		public boolean isBag() {
			return true;
		}

		@Override
		public Bag getBag() {
			return Bag.EMPTY;
		}
	}
	
	/**
	 * Creates a new ExpressionResult with the given {@link com.att.research.xacml.api.Status}.
	 * 
	 * @param statusIn the Status of this ExpressionResult
	 */
	protected ExpressionResult(Status statusIn) {
		this.status	= statusIn;
	}
	
	/**
	 * Gets the Status for this ExpressionResult.
	 * 
	 * @return the Status for this ExpressionResult
	 */
	public Status getStatus() {
		return this.status;
	}
	
	/**
	 * Shortcut procedure for determining if the Status of this ExpressionResult is OK.
	 * 
	 * @return true if the Status is null or has a StatusCode value of STATUS_CODE_OK.
	 */
	public boolean isOk() {
		return (this.getStatus() == null || this.getStatus().isOk());
	}
	
	/**
	 * Gets the single {@link com.att.research.xacml.api.AttributeValue} from this ExpressionResult.  If this
	 * ExpressionResult represents a bag, the first element in the bag is returned.
	 * 
	 * @return a single AttributeValue from this ExpressionResult
	 */
	public abstract AttributeValue getValue();
	
	/**
	 * Determines if this ExpressionResult represents a bag of AttributeValues or not.
	 * 
	 * @return true if this ExpressionResult represents a bag of AttributeValues, else false
	 */
	public abstract boolean isBag();
	
	/**
	 * Gets the {@link com.att.research.xacmlatt.pdp.policy.Bag} of values for this ExpressionResult if
	 * there is one.
	 * 
	 * @return the Bag of AttributeValues for this ExpressionResult.
	 */
	public abstract Bag getBag();
	
	@Override
	public String toString() {
		StringBuilder stringBuilder	= new StringBuilder("{");
		stringBuilder.append("isOk=" + this.isOk());
		stringBuilder.append(", isBag=" + this.isBag());
		Status thisStatus	= this.getStatus();
		if (thisStatus != null) {
			stringBuilder.append(", status=");
			stringBuilder.append(thisStatus.toString());
		}
		AttributeValue value = this.getValue();
		if (value != null) {
			stringBuilder.append(", value=");
			stringBuilder.append(value);
		}
		/*
		 * Not sure if I want this dumped
		if (this.isBag()) {
			Bag bag = this.getBag();
			if (bag != null) {
			}
		}
		*/
		stringBuilder.append('}');
		return stringBuilder.toString();
	}
	/**
	 * Creates a new instance of the ExpressionResult class representing an error.
	 * 
	 * @param statusIn the Status containing the error information
	 * @return a new ExpressionResult representing the error
	 */
	public static ExpressionResult newError(Status statusIn) {
		return new ExpressionResultError(statusIn);
	}
	
	public static ExpressionResult newSingle(AttributeValue attributeValue) {
		return new ExpressionResultSingle(attributeValue);
	}
	
	/**
	 * Creates a new instance of the ExpressionResult class representing a bag of values 
	 * from the given Bag.
	 * 
	 * @param bag the Bag for the new ExpressionResult
	 * @return a new ExpressionResult representing the given Bag.
	 */
	public static ExpressionResult newBag(Bag bag) {
		return new ExpressionResultBag(bag);
	}
	
	/**
	 * Creates a new instance of the ExpressionResult class representing an empty bag of values.
	 * 
	 * @return the ExpressionResult representing the empty bag of values of the expression
	 */
	public static ExpressionResult newEmpty() {
		return new ExpressionResultEmptyBag();
	}
	
	public static ExpressionResult newInstance(Status statusIn) {
		if (statusIn.getStatusCode().equals(StdStatus.STATUS_OK.getStatusCode())) {
			return newEmpty();
		} else {
			return newError(statusIn);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy