/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import cdac.medinfo.sdk.hl7282.commoninterface.ICollectionIterator;

/**
 * This interface provides the user with an abstract interface towards collections
 * holding series of objects.
 * @param <T1> key with value T2
 * @param <T2> value for key T1
 */
public class PairedCollection <T1,T2> implements IPairedCollection<T1, T2>
{
	HashMap<T1, T2> objHashMap;
	
	/** Creates Instance of PairedCollection */
	public PairedCollection()
	{
		 objHashMap = new HashMap<T1,T2>();
	}
	
	/**
	 * This inner class works as an Iterator which helps iterating over the map contained in the DataSet
	 * This iterator can be used for the purpose of getting all the elements of the DataSet one by one
	 * This class is implemented such that the programmer can have any 
	 * object stored in it he/she wants and having ITag as its key.
	 */
	public class CollectionIterator implements ICollectionIterator<T2>
	{
		Set<T1> mapKeySet = null;
		TreeSet<T1> objSortedTreeSet = null;
		Iterator<T1> objIterator = null;
		/** Creates Instance of CollectionIterator */
		public CollectionIterator()
		{
			mapKeySet = objHashMap.keySet();
			objSortedTreeSet = new TreeSet<T1>();
			Iterator<T1> objSetIterator = mapKeySet.iterator();
			while(objSetIterator.hasNext())
			{
				objSortedTreeSet.add(objSetIterator.next());
			}
			objIterator = objSortedTreeSet.iterator();
		}
		
		/**
		 * Returns true if the iteration has more elements.
		 * @return boolean
		 */
		public boolean hasNext()
		{
			return objIterator.hasNext();
		}
		
		/**
		 * Returns the next element in the iteration.
		 * @return T2 - next element.
		 */
		public T2 next()
		{
			T1 objT1 = objIterator.next();
			T2 objT2 = objHashMap.get(objT1);
			return objT2;
		}
		
		/**
		 * 
		 */
		public void remove()
		{
			
		}
	}
	
	/**
	 * Returns an iterator over the internal collection
	 * @return ICollectionIterator&lt;T2&gt; - returns the iterator over the internal collection
	 */
	public ICollectionIterator<T2> iterator()
	{
		return new CollectionIterator();
	}
	
	/**
	 * Provides key based accessing over the internal collection.
	 * @param objT1 This parameter specifies the key whose corresponding value is to be retrieved.
	 * @return T2 - object of the type value user specifies at the time of instantiation of this interface.
	 */
	public T2 get(T1 objT1)
	{
		return objHashMap.get(objT1);
	}
	
	/**
	 * Provides a mechanism for adding a key value pair to the collection.
	 * @param objT1 This parameter specifies the key.
	 * @param objT2 This parameter specifies the value.
	 */
	public void put(T1 objT1, T2 objT2)
	{
		objHashMap.put(objT1, objT2);
	}
	
	/**
	 * Returns the status indicating whether the key is present in the collection or not
	 * @param objT1 This parameter specifies the key.
	 * @return boolean - returns the status indicating whether the key is present in the collection or not
	 */
	public boolean contains(T1 objT1)
	{
		return objHashMap.containsKey(objT1);
	}
	
	/**
	 * Removes the values associated with the given key.
	 * @param objT1 key whose mapping value is removed.
	 * @return T2 - previous value associated with the key or null if no mapping for this key is found.
	 */
	public T2 remove(T1 objT1)
	{
		return objHashMap.remove(objT1);
	}
	
	/**
	 * Returns the size of the collection.
	 * @return size - returns the size of the collection.
	 */
	public int size()
	{
		return objHashMap.size();
	}
	
	/**
	 * The implementation of this method gets all the keys in the collection.
	 * @return T1[] - array of keys in the collection.
	 */
	@SuppressWarnings("unchecked")
	public T1[] getKeys()
	{
		Set<T1> keySet =  objHashMap.keySet();
		return  (T1[])keySet.toArray();
	}
}
