/**
 */
package org.etsi.mts.tdl.impl;

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;

import java.util.List;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EObjectResolvingEList;

import org.eclipse.ocl.pivot.evaluation.Executor;
import org.eclipse.ocl.pivot.ids.IdResolver;
import org.eclipse.ocl.pivot.ids.IdResolver.IdResolverExtension;
import org.eclipse.ocl.pivot.library.collection.CollectionAsOrderedSetOperation;
import org.eclipse.ocl.pivot.library.collection.CollectionUnionOperation;
import org.eclipse.ocl.pivot.library.oclany.OclAnyOclAsTypeOperation;
import org.eclipse.ocl.pivot.utilities.PivotUtil;
import org.eclipse.ocl.pivot.utilities.ValueUtil;
import org.eclipse.ocl.pivot.values.InvalidValueException;
import org.eclipse.ocl.pivot.values.OrderedSetValue;
import org.eclipse.ocl.pivot.values.SetValue;
import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.Extension;
import org.etsi.mts.tdl.GateType;
import org.etsi.mts.tdl.GateTypeKind;
import org.etsi.mts.tdl.PackageableElement;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tdlTables;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Gate Type</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link org.etsi.mts.tdl.impl.GateTypeImpl#getDataType <em>Data Type</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.GateTypeImpl#getKind <em>Kind</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.GateTypeImpl#getExtension <em>Extension</em>}</li>
 * </ul>
 *
 * @generated
 */
public class GateTypeImpl extends PackageableElementImpl implements GateType
{
	/**
	 * The cached value of the '{@link #getDataType() <em>Data Type</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataType()
	 * @generated
	 * @ordered
	 */
	protected EList<DataType> dataType;

	/**
	 * The default value of the '{@link #getKind() <em>Kind</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getKind()
	 * @generated
	 * @ordered
	 */
	protected static final GateTypeKind KIND_EDEFAULT = GateTypeKind.MESSAGE;

	/**
	 * The cached value of the '{@link #getKind() <em>Kind</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getKind()
	 * @generated
	 * @ordered
	 */
	protected GateTypeKind kind = KIND_EDEFAULT;

	/**
	 * The cached value of the '{@link #getExtension() <em>Extension</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getExtension()
	 * @generated
	 * @ordered
	 */
	protected Extension extension;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected GateTypeImpl()
	{
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass()
	{
		return tdlPackage.Literals.GATE_TYPE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<DataType> getDataType()
	{
		if (dataType == null) {
			dataType = new EObjectResolvingEList<DataType>(DataType.class, this, tdlPackage.GATE_TYPE__DATA_TYPE);
		}
		return dataType;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public GateTypeKind getKind()
	{
		return kind;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setKind(GateTypeKind newKind)
	{
		GateTypeKind oldKind = kind;
		kind = newKind == null ? KIND_EDEFAULT : newKind;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.GATE_TYPE__KIND, oldKind, kind));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Extension getExtension()
	{
		return extension;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetExtension(Extension newExtension, NotificationChain msgs)
	{
		Extension oldExtension = extension;
		extension = newExtension;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, tdlPackage.GATE_TYPE__EXTENSION, oldExtension, newExtension);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setExtension(Extension newExtension)
	{
		if (newExtension != extension) {
			NotificationChain msgs = null;
			if (extension != null)
				msgs = ((InternalEObject)extension).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - tdlPackage.GATE_TYPE__EXTENSION, null, msgs);
			if (newExtension != null)
				msgs = ((InternalEObject)newExtension).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - tdlPackage.GATE_TYPE__EXTENSION, null, msgs);
			msgs = basicSetExtension(newExtension, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.GATE_TYPE__EXTENSION, newExtension, newExtension));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<DataType> allDataTypes()
	{
		/**
		 *
		 * if not self.extension.oclIsUndefined()
		 * then
		 *   self.extension.extending.oclAsType(GateType)
		 *   .allDataTypes()
		 *   ->union(self.dataType)
		 *   ->asOrderedSet()
		 * else self.dataType
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ List<DataType> dataType_0 = this.getDataType();
		final /*@NonInvalid*/ Extension extension = this.getExtension();
		final /*@NonInvalid*/ OrderedSetValue BOXED_dataType_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_DataType, dataType_0);
		final /*@NonInvalid*/ boolean oclIsUndefined = extension == null;
		final /*@NonInvalid*/ Boolean not;
		if (!oclIsUndefined) {
			not = ValueUtil.TRUE_VALUE;
		}
		else {
			if (oclIsUndefined) {
				not = ValueUtil.FALSE_VALUE;
			}
			else {
				not = null;
			}
		}
		if (not == null) {
			throw new InvalidValueException("Null if condition");
		}
		/*@Thrown*/ OrderedSetValue local_0;
		if (not) {
			final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_GateType_0 = idResolver.getClass(tdlTables.CLSSid_GateType, null);
			if (extension == null) {
				throw new InvalidValueException("Null source for \'\'http://www.etsi.org/spec/TDL/1.4.1\'::Extension::extending\'");
			}
			final /*@Thrown*/ PackageableElement extending = extension.getExtending();
			final /*@Thrown*/ GateType oclAsType = (GateType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_GateType_0);
			final /*@Thrown*/ List<DataType> allDataTypes = oclAsType.allDataTypes();
			final /*@Thrown*/ OrderedSetValue BOXED_allDataTypes = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_DataType, allDataTypes);
			final /*@Thrown*/ SetValue union = (SetValue)CollectionUnionOperation.INSTANCE.evaluate(BOXED_allDataTypes, BOXED_dataType_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_dataType_0;
		}
		final /*@Thrown*/ List<DataType> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(DataType.class, local_0);
		return (EList<DataType>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
	{
		switch (featureID) {
			case tdlPackage.GATE_TYPE__EXTENSION:
				return basicSetExtension(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType)
	{
		switch (featureID) {
			case tdlPackage.GATE_TYPE__DATA_TYPE:
				return getDataType();
			case tdlPackage.GATE_TYPE__KIND:
				return getKind();
			case tdlPackage.GATE_TYPE__EXTENSION:
				return getExtension();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue)
	{
		switch (featureID) {
			case tdlPackage.GATE_TYPE__DATA_TYPE:
				getDataType().clear();
				getDataType().addAll((Collection<? extends DataType>)newValue);
				return;
			case tdlPackage.GATE_TYPE__KIND:
				setKind((GateTypeKind)newValue);
				return;
			case tdlPackage.GATE_TYPE__EXTENSION:
				setExtension((Extension)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID)
	{
		switch (featureID) {
			case tdlPackage.GATE_TYPE__DATA_TYPE:
				getDataType().clear();
				return;
			case tdlPackage.GATE_TYPE__KIND:
				setKind(KIND_EDEFAULT);
				return;
			case tdlPackage.GATE_TYPE__EXTENSION:
				setExtension((Extension)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID)
	{
		switch (featureID) {
			case tdlPackage.GATE_TYPE__DATA_TYPE:
				return dataType != null && !dataType.isEmpty();
			case tdlPackage.GATE_TYPE__KIND:
				return kind != KIND_EDEFAULT;
			case tdlPackage.GATE_TYPE__EXTENSION:
				return extension != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments) throws InvocationTargetException
	{
		switch (operationID) {
			case tdlPackage.GATE_TYPE___ALL_DATA_TYPES:
				return allDataTypes();
		}
		return super.eInvoke(operationID, arguments);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString()
	{
		if (eIsProxy()) return super.toString();

		StringBuilder result = new StringBuilder(super.toString());
		result.append(" (kind: ");
		result.append(kind);
		result.append(')');
		return result.toString();
	}

} //GateTypeImpl
