/*******************************************************************************
 *  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;  

import cdac.medinfo.sdk.hl7282.baselibs.segmentfactory.DataElementFactory;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidComponentTypeException;
import cdac.medinfo.sdk.hl7282.utils.EnumDataType;
import cdac.medinfo.sdk.hl7282.utils.IDataTypeMap;

/**
 * CD - Channel Definition<p>
 * This data type is used for labeling of digital waveform data
 * <p>Represents an HL7 CD data type. 
 * This data type has the following attributes:</p><p>
 * 1.  channel identifier (WVI)<b>Optional</b><br>
 * 2. electrode names (WVS)<b>Optional</b><br>
 * 3. channel sensitivity/units (CSU)<b>Optional</b><br>
 * 4. calibration parameters (CCP)<b>Optional</b><br>
 * 5. sampling frequency (NM)<b>Optional</b><br>
 * 6. minimum/maximum data values (NR)<b>Optional</b><br><p>
 * This class provides accessors & mutators for the elements mentioned above.
 * This class override the equal method, to check whether some other object is "equal to" this one.
 * 
 * <p>User can also create Data Type through {@link DataElementFactory} 
 */
public class CD extends CompositeDataType   
{
	


	/**
	 * Default Constructor for CD
	 */
	public CD()
	{
		super(EnumDataType.HL7_DT_CD);
		 
	}

	/**
	 * Constructor  for CD class which takes four arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */ 
	public CD(int iMinLength, int iMaxLength, String strCLength,int iComponentType) throws InvalidComponentTypeException
	{
		super(EnumDataType.HL7_DT_CD);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}


	/**
	 * Constructor for CD class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public CD(int iMinLength, int iMaxLength, String strCLength,int iTableNo,int iComponentType)
	{
		super(EnumDataType.HL7_DT_CD);
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;
		this.iTableNo = iTableNo;
		this.iComponentType = iComponentType;
		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());		
		}
	}

	/**
	 * Constructor for CD class which takes two arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public CD(int iComponentType,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}

	/**
	 * Constructor for CD class which takes five arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public CD(int iComponentType,int iMinLength, int iMaxLength, String strCLength,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}
	/**
	 * Constructor for CD class which takes six arguments and 
	 * sets the value in the respective member variable.
	 * @param objIDataTypeMap object of Data Type Map.
	 * @param iComponentType The Type of the component i.e Component or a subcomponent.
	 * @param iMinLength The Minimum length the DataType can have.
	 * @param iMaxLength The Maximum length the DataType can have.
	 * @param iCLength The Conformance length the DataType can have.
	 * @param iTableNo The Table number to which the value for the component is specified. 
	 * @throws InvalidComponentTypeException - if Component Type is not specified or valid. 
	 */
	public CD(int iComponentType,int iMinLength, int iMaxLength, String strCLength,int ITableNo,IDataTypeMap objIDataTypeMap)
	{
		super(EnumDataType.HL7_DT_CD);
		this.objIDataTypeMap = objIDataTypeMap;
		this.iComponentType = iComponentType;
		this.iTableNo = ITableNo;
		this.iMinLength = iMinLength;
		this.iMaxLength = iMaxLength;
		this.iConformanceLength = strCLength;

		if(!isValidComponentType())
		{
			throw new InvalidComponentTypeException(this.getClass().getSimpleName());
		}
	}	


	/**
	 * Sets the Channel Identifier.
	 * @param objChannelIdentifier Specifies the number and name of the recording channel where waveform data is transmitted.
	 */
	public void setChannelIdentifier(WVI objChannelIdentifier)
	{
		setComponentType(1,objChannelIdentifier);
	}  

	/**
	 * Sets the Waveform Source.
	 * @param objWaveformSource Specifies the Waveform Source.
	 */
	public void setWaveformSource(WVS objWaveformSource)
	{
		setComponentType(2,objWaveformSource);
	}  

	/**
	 * Sets the Channel Sensitivity Units.
	 * @param objSSChannelSensitivityUnits Defines the channel sensitivity (gain) and the units in which it is measured.
	 */
	public void setChannelSensitivityUnits(CSU objSSChannelSensitivityUnits)
	{
		setComponentType(3,objSSChannelSensitivityUnits);
	}  

	/**
	 * Sets the Channel Calibration Parameters.
	 * @param objChannelCalibrationParameters Specifies the corrections to channel sensitivity, the baseline, and the 
	 * 											channel time skew.
	 */
	public void setChannelCalibrationParameters(CCP objChannelCalibrationParameters)
	{
		setComponentType(4,objChannelCalibrationParameters);
	}  

	/**
	 * Sets the Channel Sampling Frequency.
	 * @param strChannelSamplingFrequency Defines the sampling frequency in hertz of the channel
	 */
	public void setChannelSamplingFrequency(String strChannelSamplingFrequency)
	{
		if(strChannelSamplingFrequency!=null)
			setComponentType(5, strChannelSamplingFrequency);
		else
			setChannelSamplingFrequency((NM)null);
	}  

	/**
	 * Sets the Channel Sampling Frequency.
	 * @param objNM Defines the sampling frequency in hertz of the channel
	 */
	public void setChannelSamplingFrequency(NM objNM)
	{
		setComponentType(5,objNM);
	}  
	/**
	 * Sets the Minimum - Maximum Data Values.
	 * @param objMinMaxDataValues Specifies the minimum and maximum data values which can occur in this 
	 * 										channel in the digital waveform data
	 */
	public void setMinimumMaximumDataValues(NR objMinMaxDataValues)
	{
		setComponentType(6,objMinMaxDataValues);
	}  

	/**
	 * Returns Channel Identifier
	 * @return WVI - data type.
	 */
	public WVI getChannelIdentifier()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(1);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (WVI)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Waveform Source
	 * @return WVS - data type.
	 */
	public WVS getWaveformSource()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(2);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (WVS)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Channel Sensitivity Units
	 * @return CSU - data type.
	 */
	public CSU getChannelSensitivityUnits()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(3);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CSU)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Channel Calibration Parameters
	 * @return CCP - data type.
	 */
	public CCP getChannelCalibrationParameters()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(4);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (CCP)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Channel Calibration Parameters
	 * @return NM - data type.
	 */
	public NM getChannelSamplingFrequency()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(5);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Returns Minimum Maximum Data Values
	 * @return NR - data type.
	 */
	public NR getMinimumMaximumDataValues()
	{
		ISingleCollection<IDataType> objDataCollection =getComponentType(6);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NR)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * This method checks the equality of Object.
	 * @param objObject object of Object class.
	 * @return boolean - shows success or failure of the comparison.
	 */
	public boolean equals(Object objObject)
	{
		if((objObject == null ) || (!(objObject instanceof CD)))
			return false;

		if(this.getChannelIdentifier() == null)
		{
			if(((CD)objObject).getChannelIdentifier() != null)
				return false;
		}
		else if(false == (this.getChannelIdentifier().equals(((CD)objObject).getChannelIdentifier())))
			return false;

		if(this.getWaveformSource() == null)
		{
			if(((CD)objObject).getWaveformSource() != null)
				return false;
		}
		else if(false == (this.getWaveformSource().equals(((CD)objObject).getWaveformSource())))
			return false;

		if(this.getChannelSensitivityUnits() == null)
		{
			if(((CD)objObject).getChannelSensitivityUnits() != null)
				return false;
		}
		else if(false == (this.getChannelSensitivityUnits().equals(((CD)objObject).getChannelSensitivityUnits())))
			return false;

		if(this.getChannelCalibrationParameters() == null)
		{
			if(((CD)objObject).getChannelCalibrationParameters() != null)
				return false;
		}
		else if(false == (this.getChannelCalibrationParameters().equals(((CD)objObject).getChannelCalibrationParameters())))
			return false;

		if(this.getChannelSamplingFrequency() == null)
		{
			if(((CD)objObject).getChannelSamplingFrequency() != null)
				return false;
		}
		else if(false == (this.getChannelSamplingFrequency().equals(((CD)objObject).getChannelSamplingFrequency())))
			return false;

		if(this.getMinimumMaximumDataValues() == null)
		{
			if(((CD)objObject).getMinimumMaximumDataValues() != null)
				return false;
		}
		else if(false == (this.getMinimumMaximumDataValues().equals(((CD)objObject).getMinimumMaximumDataValues())))
			return false;

		return true;
	}
}
