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

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

There is a newer version: 4.0.0
Show newest version
/*
 *
 *          Copyright (c) 2013,2019  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;

/**
 * TargetedCombinerParameterMap is a utility for maintaining a collection of {@link com.att.research.xacmlatt.pdp.policy.TargetedCombinerParameter}
 * objects with the mappings to their targets.
 * 
 * @author car
 * @version $Revision: 1.1 $
 *
 * @param  the type of the identifier for the TargetedCombinerParameters in the map
 * @param  the type of the object referenced by the identifier
 */
public class TargetedCombinerParameterMap {
	List>	targetedCombinerParameters		= null;
	Map								mapTargetIdToTarget				= new HashMap();
	Map>			mapTargetToCombinerParameters	= null;
	
	private void ensureTargetedCombinerParameters() {
		if (this.targetedCombinerParameters == null) {
			this.targetedCombinerParameters	= new ArrayList>();
		}
	}
	
	/**
	 * Gets the target from the given TargetedCombinerParameter if present.  If not, find the
	 * target in the target id to target mapping, update the TargetedCombinerParameter and then
	 * return the target.
	 * 
	 * @param targetedCombinerParameter the TargetedCombinerParameter to resolve
	 * @return the target for the given TargetedCombinerParameter
	 */
	protected U resolve(TargetedCombinerParameter targetedCombinerParameter) {
		U result;
		if ((result = targetedCombinerParameter.getTarget()) != null) {
			return result;
		} else if ((result = this.mapTargetIdToTarget.get(targetedCombinerParameter.getTargetId())) != null) {
			targetedCombinerParameter.setTarget(result);
			return result;
		} else {
			return null;
		}
	}
	
	/**
	 * Ensures the Map from targets to List of CombinerParameters has been
	 * created if needed.
	 * 
	 * @throws IllegalStateException if there are TargetedCombinerParameters that cannot be resolved
	 */
	protected void ensureMap() throws IllegalStateException {
		if (this.mapTargetToCombinerParameters == null) {
			if (this.targetedCombinerParameters != null && this.targetedCombinerParameters.size() > 0) {
				this.mapTargetToCombinerParameters	= new HashMap>();
				for (TargetedCombinerParameter targetedCombinerParameter: this.targetedCombinerParameters) {
					U	target	= this.resolve(targetedCombinerParameter);
					if (target == null) {
						throw new IllegalStateException("Unresolved TargetCombinerParameter \"" + targetedCombinerParameter.toString() + "\"");
					}
					List	listCombinerParameters	= this.mapTargetToCombinerParameters.get(target);
					if (listCombinerParameters == null) {
						listCombinerParameters	= new ArrayList<>();
						this.mapTargetToCombinerParameters.put(target, listCombinerParameters);
					}
					listCombinerParameters.add(targetedCombinerParameter);
				}
			}
		}
	}
	
	/**
	 * Creates a new TargetedCombinerParameterMap.
	 */
	public TargetedCombinerParameterMap() {
		super();
	}
	
	/**
	 * Adds a new target object to the identifier map.
	 * 
	 * @param targetId the id for the target
	 * @param target the target
	 */
	public void addTarget(T targetId, U target) {
		this.mapTargetIdToTarget.put(targetId, target);
	}
	
	/**
	 * Adds a new TargetedCombinerParameter to this TargetedCombinerParameterMap.
	 * 
	 * @param targetdCombinerParameter the TargetedCombinerParameter to add
	 */
	public void addCombinerParameter(TargetedCombinerParameter targetdCombinerParameter) {
		this.ensureTargetedCombinerParameters();
		this.targetedCombinerParameters.add(targetdCombinerParameter);
		this.mapTargetToCombinerParameters	= null;
	}
	
	/**
	 * Adds the contents of the given Collection of TargetedCombinerParameters to this TargetedCombinerParameterMap.
	 * 
	 * @param listTargetedCombinerParameters the Collection of TargetedCombinerParameters to add
	 */
	public void addCombinerParameters(Collection> listTargetedCombinerParameters) {
		this.ensureTargetedCombinerParameters();
		this.targetedCombinerParameters.addAll(listTargetedCombinerParameters);
		this.mapTargetToCombinerParameters	= null;
	}
	
	/**
	 * Sets the set of TargetedCombinerParameters for this TargetedCombinerParameterMap to the contents of the
	 * given Collection>
	 * 
	 * @param listTargetedCombinerParameters the Collection of TargetedCombinerParameters to set
	 */
	public void setCombinerParameters(Collection> listTargetedCombinerParameters) {
		this.targetedCombinerParameters	= null;
		if (listTargetedCombinerParameters != null) {
			this.addCombinerParameters(targetedCombinerParameters);
		}
	}
	
	/**
	 * Looks up the given target in the map for any {@link com.att.research.xacmlatt.pdp.policy.CombinerParameter}s for the
	 * given target.
	 * 
	 * @param target the target
	 * @return a List of CombinerParameters for the target or null if none
	 * @throws IllegalStateException if there are TargetedCombinerParameters that cannot be resolved
	 */
	public List getCombinerParameters(U target) throws IllegalStateException {
		this.ensureMap();
		return (this.mapTargetToCombinerParameters == null ? null : this.mapTargetToCombinerParameters.get(target));
	}
	
	public Iterator> getTargetedCombinerParameters() {
		return (this.targetedCombinerParameters == null ? null : this.targetedCombinerParameters.iterator());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy