/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.dictionaryreaderwriters;

import java.io.Serializable;


import cdac.medinfo.sdk.dcm30_04.utils.IPairedCollection;
import cdac.medinfo.sdk.dcm30_04.utils.PairedCollection;

/**
 * This class implements a dictionary which contains entry for all the elements.
 * This is a singleton class means only single instance can be created of this class.
 */
class DataDictionary implements Serializable
{
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/* 
	 * We have provided a PairedCollection which will encapsulate the Hashmap
	 * The key of the HashMap is an Integer which is the sum of the HashCodes
	 * of the GroupNumber and ElementNumber.
	 */
	IPairedCollection<Integer, RowDescriptor> objPairedCollection = null;

	private static DataDictionary objDataDictionary = null;

	/**
	 * Default private constructor for this class.
	 * It's a singleton class.
	 */
	private DataDictionary()
	{
		objPairedCollection = new PairedCollection<Integer,RowDescriptor>();
	}

	/**
	 * Creates instance of singleton class.
	 * @return DataDictionary - object of DataDictionary.
	 */
	static DataDictionary createInstance()
	{
		if(objDataDictionary == null)
			objDataDictionary = new DataDictionary();
		return objDataDictionary;
	}

	/**
	 * Adds entry for new element in data dictionary.
	 * @param objRowDescriptor Object of Row Descriptor class which contains different fields of an element.
	 * @return boolean - Shows success of addition process.
	 */
	boolean add(RowDescriptor objRowDescriptor)
	{
		Integer iMapKey =  createKey(objRowDescriptor.iGroupNumber,objRowDescriptor.iElementNumber);
		objPairedCollection.put(iMapKey, objRowDescriptor);
		objRowDescriptor = objPairedCollection.get(iMapKey);
		return true;
	}

	/**
	 * Retrieves a row containing all informations for a specified element from DataDictionary.
	 * @param iGroupNumber Integer value of group number.
	 * @param iElementNumber Integer value of element number.
	 * @return RowDescriptor - object of Row Descriptor for specified element.
	 */
	RowDescriptor getEntry(int iGroupNumber, int iElementNumber)
	{
		RowDescriptor objRowDescriptor = null;
		if(objPairedCollection != null)
		{
			Integer iMapKey =  createKey(iGroupNumber,iElementNumber);
			objRowDescriptor = objPairedCollection.get(iMapKey);			
		}
		return objRowDescriptor;
	}

	/**
	 * Generates a HashKey using the GroupNumber and ElementNumber.
	 * @param iGroupNumber Integer value of group number.
	 * @param iElementNumber Integer value of element number.
	 * @return Integer - integer value of hash code.
	 */
	private Integer createKey(int iGroupNumber, int iElementNumber)
	{
		int result = 0;
		result = 1000003 * result + iGroupNumber;
		result = 1000003 * result + iElementNumber;
		return result * 1000003;
	}

	/**
	 * Returns the status of Data Dictionary that it is populated or not.
	 * @return boolean - true, if Data Dictionary is populated.
	 */
	boolean isPopulated()
	{
		if(objPairedCollection.size() > 0)
			return true;
		return false;	
	}

	/**
	 * Retrieves the  Group Number and Element Number of Attribute Name provided.
	 * This function return a short array in which element at index 0 is Group Number
	 * and at index 1 is Element Number. 
	 * @param strDataElementName String value of Data Element name.
	 * @return short[] - return short array. The element at index 0 represents Group Number and 
	 * at index 1 represents Element Number. 
	 */
	public short[] getTag(String strDataElementName)
	{
		short[] arrShTag = null;
		for(RowDescriptor objRowDescriptor : objPairedCollection)
		{
			if(objRowDescriptor.getStrElementName().equalsIgnoreCase(strDataElementName))
			{
				arrShTag = new short[2];
				arrShTag[0] = (short) objRowDescriptor.getIGroupNumber();
				arrShTag[1] = (short) objRowDescriptor.getIElementNumber();
			}
		}
		return arrShTag;
	}
}

