/**
 */
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.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

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.ComponentType;
import org.etsi.mts.tdl.Extension;
import org.etsi.mts.tdl.GateInstance;
import org.etsi.mts.tdl.PackageableElement;
import org.etsi.mts.tdl.Timer;
import org.etsi.mts.tdl.Variable;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tdlTables;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Component Type</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link org.etsi.mts.tdl.impl.ComponentTypeImpl#getGateInstance <em>Gate Instance</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.ComponentTypeImpl#getTimer <em>Timer</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.ComponentTypeImpl#getVariable <em>Variable</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.ComponentTypeImpl#getExtension <em>Extension</em>}</li>
 * </ul>
 *
 * @generated
 */
public class ComponentTypeImpl extends PackageableElementImpl implements ComponentType
{
	/**
	 * The cached value of the '{@link #getGateInstance() <em>Gate Instance</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getGateInstance()
	 * @generated
	 * @ordered
	 */
	protected EList<GateInstance> gateInstance;

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

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

	/**
	 * 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 ComponentTypeImpl()
	{
		super();
	}

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<GateInstance> getGateInstance()
	{
		if (gateInstance == null) {
			gateInstance = new EObjectContainmentEList<GateInstance>(GateInstance.class, this, tdlPackage.COMPONENT_TYPE__GATE_INSTANCE);
		}
		return gateInstance;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<Timer> getTimer()
	{
		if (timer == null) {
			timer = new EObjectContainmentEList<Timer>(Timer.class, this, tdlPackage.COMPONENT_TYPE__TIMER);
		}
		return timer;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<Variable> getVariable()
	{
		if (variable == null) {
			variable = new EObjectContainmentEList<Variable>(Variable.class, this, tdlPackage.COMPONENT_TYPE__VARIABLE);
		}
		return variable;
	}

	/**
	 * <!-- 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.COMPONENT_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.COMPONENT_TYPE__EXTENSION, null, msgs);
			if (newExtension != null)
				msgs = ((InternalEObject)newExtension).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - tdlPackage.COMPONENT_TYPE__EXTENSION, null, msgs);
			msgs = basicSetExtension(newExtension, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.COMPONENT_TYPE__EXTENSION, newExtension, newExtension));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<GateInstance> allGates()
	{
		/**
		 *
		 * if not self.extension.oclIsUndefined()
		 * then
		 *   self.extension.extending.oclAsType(ComponentType)
		 *   .allGates()
		 *   ->union(self.gateInstance)
		 *   ->asOrderedSet()
		 * else self.gateInstance
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ Extension extension = this.getExtension();
		final /*@NonInvalid*/ List<GateInstance> gateInstance_0 = this.getGateInstance();
		final /*@NonInvalid*/ OrderedSetValue BOXED_gateInstance_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_GateInstance, gateInstance_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_ComponentType_0 = idResolver.getClass(tdlTables.CLSSid_ComponentType, 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*/ ComponentType oclAsType = (ComponentType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_ComponentType_0);
			final /*@Thrown*/ List<GateInstance> allGates = oclAsType.allGates();
			final /*@Thrown*/ OrderedSetValue BOXED_allGates = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_GateInstance, allGates);
			final /*@Thrown*/ SetValue union = (SetValue)CollectionUnionOperation.INSTANCE.evaluate(BOXED_allGates, BOXED_gateInstance_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_gateInstance_0;
		}
		final /*@Thrown*/ List<GateInstance> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(GateInstance.class, local_0);
		return (EList<GateInstance>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<Timer> allTimers()
	{
		/**
		 *
		 * if not self.extension.oclIsUndefined()
		 * then
		 *   self.extension.extending.oclAsType(ComponentType)
		 *   .allTimers()
		 *   ->union(self.timer)
		 *   ->asOrderedSet()
		 * else self.timer
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ Extension extension = this.getExtension();
		final /*@NonInvalid*/ List<Timer> timer_0 = this.getTimer();
		final /*@NonInvalid*/ OrderedSetValue BOXED_timer_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Timer, timer_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_ComponentType_0 = idResolver.getClass(tdlTables.CLSSid_ComponentType, 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*/ ComponentType oclAsType = (ComponentType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_ComponentType_0);
			final /*@Thrown*/ List<Timer> allTimers = oclAsType.allTimers();
			final /*@Thrown*/ OrderedSetValue BOXED_allTimers = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Timer, allTimers);
			final /*@Thrown*/ SetValue union = (SetValue)CollectionUnionOperation.INSTANCE.evaluate(BOXED_allTimers, BOXED_timer_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_timer_0;
		}
		final /*@Thrown*/ List<Timer> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(Timer.class, local_0);
		return (EList<Timer>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<Variable> allVariables()
	{
		/**
		 *
		 * if not self.extension.oclIsUndefined()
		 * then
		 *   self.extension.extending.oclAsType(ComponentType)
		 *   .allVariables()
		 *   ->union(self.variable)
		 *   ->asOrderedSet()
		 * else self.variable
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ Extension extension = this.getExtension();
		final /*@NonInvalid*/ List<Variable> variable_0 = this.getVariable();
		final /*@NonInvalid*/ OrderedSetValue BOXED_variable_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Variable, variable_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_ComponentType_0 = idResolver.getClass(tdlTables.CLSSid_ComponentType, 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*/ ComponentType oclAsType = (ComponentType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_ComponentType_0);
			final /*@Thrown*/ List<Variable> allVariables = oclAsType.allVariables();
			final /*@Thrown*/ OrderedSetValue BOXED_allVariables = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Variable, allVariables);
			final /*@Thrown*/ SetValue union = (SetValue)CollectionUnionOperation.INSTANCE.evaluate(BOXED_allVariables, BOXED_variable_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_variable_0;
		}
		final /*@Thrown*/ List<Variable> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(Variable.class, local_0);
		return (EList<Variable>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
	{
		switch (featureID) {
			case tdlPackage.COMPONENT_TYPE__GATE_INSTANCE:
				return ((InternalEList<?>)getGateInstance()).basicRemove(otherEnd, msgs);
			case tdlPackage.COMPONENT_TYPE__TIMER:
				return ((InternalEList<?>)getTimer()).basicRemove(otherEnd, msgs);
			case tdlPackage.COMPONENT_TYPE__VARIABLE:
				return ((InternalEList<?>)getVariable()).basicRemove(otherEnd, msgs);
			case tdlPackage.COMPONENT_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.COMPONENT_TYPE__GATE_INSTANCE:
				return getGateInstance();
			case tdlPackage.COMPONENT_TYPE__TIMER:
				return getTimer();
			case tdlPackage.COMPONENT_TYPE__VARIABLE:
				return getVariable();
			case tdlPackage.COMPONENT_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.COMPONENT_TYPE__GATE_INSTANCE:
				getGateInstance().clear();
				getGateInstance().addAll((Collection<? extends GateInstance>)newValue);
				return;
			case tdlPackage.COMPONENT_TYPE__TIMER:
				getTimer().clear();
				getTimer().addAll((Collection<? extends Timer>)newValue);
				return;
			case tdlPackage.COMPONENT_TYPE__VARIABLE:
				getVariable().clear();
				getVariable().addAll((Collection<? extends Variable>)newValue);
				return;
			case tdlPackage.COMPONENT_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.COMPONENT_TYPE__GATE_INSTANCE:
				getGateInstance().clear();
				return;
			case tdlPackage.COMPONENT_TYPE__TIMER:
				getTimer().clear();
				return;
			case tdlPackage.COMPONENT_TYPE__VARIABLE:
				getVariable().clear();
				return;
			case tdlPackage.COMPONENT_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.COMPONENT_TYPE__GATE_INSTANCE:
				return gateInstance != null && !gateInstance.isEmpty();
			case tdlPackage.COMPONENT_TYPE__TIMER:
				return timer != null && !timer.isEmpty();
			case tdlPackage.COMPONENT_TYPE__VARIABLE:
				return variable != null && !variable.isEmpty();
			case tdlPackage.COMPONENT_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.COMPONENT_TYPE___ALL_GATES:
				return allGates();
			case tdlPackage.COMPONENT_TYPE___ALL_TIMERS:
				return allTimers();
			case tdlPackage.COMPONENT_TYPE___ALL_VARIABLES:
				return allVariables();
		}
		return super.eInvoke(operationID, arguments);
	}

} //ComponentTypeImpl
