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

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

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.att.research.xacml.util.StringUtils;

/**
 * VariableMap is a collection of {@link com.att.research.xacmlatt.pdp.policy.VariableDefinition}s that are accessible by
 * the variable identifier.
 * 
 * @author car
 * @version $Revision: 1.1 $
 */
public class VariableMap {
	private List		variableDefinitions;
	private Map mapVariableDefinitions;
	
	private void ensureVariableDefinitions() {
		if (this.variableDefinitions == null) {
			this.variableDefinitions	= new ArrayList<>();
		}
	}
	
	private void ensureMap() {
		if (this.mapVariableDefinitions == null) {
			this.mapVariableDefinitions	= new HashMap<>();
		}
	}
	
	public VariableMap() {
	}

	/**
	 * Gets the VariableDefinition with the given String id.
	 * 
	 * @param variableId the String identifier of the VariableDefinition to retrieve
	 * @return the VariableDefinition with the given String id or null if not found.
	 */
	public VariableDefinition getVariableDefinition(String variableId) {
		return this.mapVariableDefinitions == null ? null : this.mapVariableDefinitions.get(variableId);
	}
	
	/**
	 * Gets an Iterator over the VariableDefinitions in this VariableMap
	 * in the order they were added.
	 * 
	 * @return an Iterator over the VariableDefinitions in this VariableMap
	 */
	public Iterator getVariableDefinitions() {
		return this.variableDefinitions == null ? null : this.variableDefinitions.iterator();
	}
	
	/**
	 * Adds the given VariableDefinition to this VariableMap.
	 * 
	 * @param variableDefinition the VariableDefinition to add
	 */
	public void add(VariableDefinition variableDefinition) {
		this.ensureMap();
		this.ensureVariableDefinitions();
		this.variableDefinitions.add(variableDefinition);
		this.mapVariableDefinitions.put(variableDefinition.getId(), variableDefinition);
	}
	
	/**
	 * Adds the contents of the given Collection of VariableDefinitions to the set of
	 * VariableDefinitions in this VariableMap
	 * 
	 * @param listVariableDefinitions the Collection of VariableDefinitions to add
	 */
	public void addVariableDefinitions(Collection listVariableDefinitions) {
		for (VariableDefinition variableDefinition: listVariableDefinitions) {
			this.add(variableDefinition);
		}
	}
	
	/**
	 * Sets the VariableDefinitions in this VariableMap to the contents of the given
	 * Collection.
	 * 
	 * @param listVariableDefinitions the Collection of VariableDefinition to set
	 */
	public void setVariableDefinitions(Collection listVariableDefinitions) {
		this.variableDefinitions	= null;
		this.mapVariableDefinitions	= null;
		if (listVariableDefinitions != null) {
			this.addVariableDefinitions(variableDefinitions);
		}		
	}
	
	@Override
	public String toString() {
		StringBuilder stringBuilder	= new StringBuilder("{");
		if (this.mapVariableDefinitions.size() > 0) {
			stringBuilder.append("variableDefinitions=");
			stringBuilder.append(StringUtils.toString(this.mapVariableDefinitions.values().iterator()));
		}
		stringBuilder.append('}');
		return stringBuilder.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy