/*******************************************************************************
 *  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.baselibs.segmentfactory;

import cdac.medinfo.sdk.hl7282.utils.IPairedCollection;
import cdac.medinfo.sdk.hl7282.utils.ISegmentMap;
import cdac.medinfo.sdk.hl7282.utils.PairedCollection;

/**
 * SegmentDictionary 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 SegmentDictionary
{
	/* 
	 * Provides 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<String, ISegmentMap> objSegmentMapList = null;

	private static SegmentDictionary objSegmentDictionary = null;

	/**
	 * Default private constructor for this class.
	 * It's a singleton class.
	 */
	private SegmentDictionary()
	{
		objSegmentMapList = new PairedCollection<String, ISegmentMap>();
	}

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

	/**
	 * Adds entry for new element in data dictionary.
	 * @param strSegmentName Name of Segment name
	 * @param objSegmentMap Object of Segment Map
	 * @return boolean - Shows success of addition process
	 */
	boolean addSegmentMap(String strSegmentName,ISegmentMap objSegmentMap)
	{
		objSegmentMap.setLock(true);
		objSegmentMapList.put(strSegmentName,objSegmentMap);
		
		return true;
	}

	/**
	 * Retrieves Item list for the specified Segment.
	 * @param strSegmentKey String value of Segment
	 * @return ISingleCollection<ItemRowDescriptor> - Item list for the specified Segment
	 */
	ISegmentMap getSegmentMap(String strSegmentKey)
	{
		return objSegmentMapList.get(strSegmentKey);
	}

	/**
	 * Returns the status of Data Dictionary that it is populated or not.
	 * @param strSegmentName Name of Segment
	 * @return boolean - true, if Data Dictionary is populated
	 */
	boolean isSegmentPopulated(String strSegmentName)
	{
		if(objSegmentMapList.contains(strSegmentName))
			return true;
		return false;
	}
}
