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

de.unirostock.sems.xmlutils.ds.mappers.MultiNodeMapper Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package de.unirostock.sems.xmlutils.ds.mappers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;



/**
 * The Class MultiNodeMapper maps Strings to a list of nodes. Intended to map
 * non-unique labels (e.g. tag names => nodes). T is usually TreeNode or
 * DocumentNode.
 * 
 * @param 
 *          the generic type, e.g. TreeNode or DocumentNode
 * @see de.unirostock.sems.xmlutils.ds.TreeNode
 * @see de.unirostock.sems.xmlutils.ds.DocumentNode
 * @see de.unirostock.sems.xmlutils.ds.TextNode
 * @author Martin Scharm
 */
public class MultiNodeMapper
{
	
	/** The mapper itself. */
	private HashMap>	mapper;
	
	
	/**
	 * Instantiate a new mapper.
	 */
	public MultiNodeMapper ()
	{
		mapper = new HashMap> ();
	}
	
	
	/**
	 * Gets the known identifiers.
	 * 
	 * @return the known identifiers
	 */
	public Set getIds ()
	{
		return mapper.keySet ();
	}
	
	
	/**
	 * Adds a node.
	 * 
	 * @param id
	 *          the identifier
	 * @param node
	 *          the node
	 */
	public void addNode (String id, T node)
	{
		List nodes = mapper.get (id);
		if (nodes == null)
		{
			nodes = new ArrayList ();
			mapper.put (id, nodes);
		}
		nodes.add (node);
	}
	
	
	/**
	 * Removes a node.
	 * 
	 * @param id
	 *          the identifier
	 * @param node
	 *          the node
	 */
	public void rmNode (String id, T node)
	{
		List nodes = mapper.get (id);
		if (nodes == null)
		{
			return;
		}
		nodes.remove (node);
	}
	
	
	/**
	 * Gets the nodes that are stored for a certain identifier.
	 * 
	 * @param id
	 *          the identifier
	 * @return the nodes
	 */
	public List getNodes (String id)
	{
		return mapper.get (id);
	}
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString ()
	{
		StringBuilder s = new StringBuilder (" ");
		for (String a : mapper.keySet ())
			s.append (a + " =>> " + mapper.get (a).toString () + "");
		return s.toString ();
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy