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

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;

import java.util.Iterator;
import java.util.List;
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.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.CollectionIsEmptyOperation;
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.BagValue;
import org.eclipse.ocl.pivot.values.InvalidValueException;
import org.eclipse.ocl.pivot.values.OrderedSetValue;
import org.eclipse.ocl.pivot.values.SequenceValue;
import org.eclipse.ocl.pivot.values.SequenceValue.Accumulator;
import org.etsi.mts.tdl.Constraint;
import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.Extension;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.PackageableElement;
import org.etsi.mts.tdl.StructuredDataType;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tdlTables;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Structured Data Type</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link org.etsi.mts.tdl.impl.StructuredDataTypeImpl#getExtension <em>Extension</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.StructuredDataTypeImpl#getMember <em>Member</em>}</li>
 * </ul>
 *
 * @generated
 */
public class StructuredDataTypeImpl extends DataTypeImpl implements StructuredDataType
{
	/**
	 * The cached value of the '{@link #getExtension() <em>Extension</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getExtension()
	 * @generated
	 * @ordered
	 */
	protected EList<Extension> extension;

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

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<Extension> getExtension()
	{
		if (extension == null) {
			extension = new EObjectContainmentEList<Extension>(Extension.class, this, tdlPackage.STRUCTURED_DATA_TYPE__EXTENSION);
		}
		return extension;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<Member> getMember()
	{
		if (member == null) {
			member = new EObjectContainmentEList<Member>(Member.class, this, tdlPackage.STRUCTURED_DATA_TYPE__MEMBER);
		}
		return member;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<Member> allMembers()
	{
		/**
		 *
		 * if not self.extension->isEmpty()
		 * then
		 *   self.extension->collect(e |
		 *     e.extending.oclAsType(StructuredDataType)
		 *     .allMembers())
		 *   ->union(self.member)
		 *   ->asOrderedSet()
		 * else self.member
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ List<Extension> extension_0 = this.getExtension();
		final /*@NonInvalid*/ List<Member> member_0 = this.getMember();
		final /*@NonInvalid*/ OrderedSetValue BOXED_extension_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Extension, extension_0);
		final /*@NonInvalid*/ OrderedSetValue BOXED_member_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Member, member_0);
		final /*@NonInvalid*/ boolean isEmpty = CollectionIsEmptyOperation.INSTANCE.evaluate(BOXED_extension_0).booleanValue();
		final /*@NonInvalid*/ Boolean not;
		if (!isEmpty) {
			not = ValueUtil.TRUE_VALUE;
		}
		else {
			if (isEmpty) {
				not = ValueUtil.FALSE_VALUE;
			}
			else {
				not = null;
			}
		}
		if (not == null) {
			throw new InvalidValueException("Null if condition");
		}
		/*@Thrown*/ OrderedSetValue local_0;
		if (not) {
			/*@Thrown*/ Accumulator accumulator = ValueUtil.createSequenceAccumulatorValue(tdlTables.SEQ_CLSSid_Member);
			Iterator<Object> ITERATOR_e_1 = BOXED_extension_0.iterator();
			/*@Thrown*/ SequenceValue collect;
			while (true) {
				if (!ITERATOR_e_1.hasNext()) {
					collect = accumulator;
					break;
				}
				/*@NonInvalid*/ Extension e_1 = (Extension)ITERATOR_e_1.next();
				/**
				 * e.extending.oclAsType(StructuredDataType).allMembers()
				 */
				final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_StructuredDataType_0 = idResolver.getClass(tdlTables.CLSSid_StructuredDataType, null);
				if (e_1 == null) {
					throw new InvalidValueException("Null source for \'\'http://www.etsi.org/spec/TDL/1.4.1\'::Extension::extending\'");
				}
				final /*@Thrown*/ PackageableElement extending = e_1.getExtending();
				final /*@Thrown*/ StructuredDataType oclAsType = (StructuredDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_StructuredDataType_0);
				final /*@Thrown*/ List<Member> allMembers = oclAsType.allMembers();
				final /*@Thrown*/ OrderedSetValue BOXED_allMembers = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Member, allMembers);
				//
				for (Object value : BOXED_allMembers.flatten().getElements()) {
					accumulator.add(value);
				}
			}
			final /*@Thrown*/ BagValue union = (BagValue)CollectionUnionOperation.INSTANCE.evaluate(collect, BOXED_member_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_member_0;
		}
		final /*@Thrown*/ List<Member> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(Member.class, local_0);
		return (EList<Member>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public EList<Constraint> allConstraints()
	{
		/**
		 *
		 * if not self.extension->isEmpty()
		 * then
		 *   self.extension->collect(e |
		 *     e.extending.oclAsType(StructuredDataType)
		 *     .allConstraints())
		 *   ->union(self.constraint)
		 *   ->asOrderedSet()
		 * else self.constraint
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ List<Constraint> constraint_0 = this.getConstraint();
		final /*@NonInvalid*/ List<Extension> extension_0 = this.getExtension();
		final /*@NonInvalid*/ OrderedSetValue BOXED_constraint_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Constraint, constraint_0);
		final /*@NonInvalid*/ OrderedSetValue BOXED_extension_0 = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Extension, extension_0);
		final /*@NonInvalid*/ boolean isEmpty = CollectionIsEmptyOperation.INSTANCE.evaluate(BOXED_extension_0).booleanValue();
		final /*@NonInvalid*/ Boolean not;
		if (!isEmpty) {
			not = ValueUtil.TRUE_VALUE;
		}
		else {
			if (isEmpty) {
				not = ValueUtil.FALSE_VALUE;
			}
			else {
				not = null;
			}
		}
		if (not == null) {
			throw new InvalidValueException("Null if condition");
		}
		/*@Thrown*/ OrderedSetValue local_0;
		if (not) {
			/*@Thrown*/ Accumulator accumulator = ValueUtil.createSequenceAccumulatorValue(tdlTables.SEQ_CLSSid_Constraint);
			Iterator<Object> ITERATOR_e_1 = BOXED_extension_0.iterator();
			/*@Thrown*/ SequenceValue collect;
			while (true) {
				if (!ITERATOR_e_1.hasNext()) {
					collect = accumulator;
					break;
				}
				/*@NonInvalid*/ Extension e_1 = (Extension)ITERATOR_e_1.next();
				/**
				 * e.extending.oclAsType(StructuredDataType).allConstraints()
				 */
				final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_StructuredDataType_0 = idResolver.getClass(tdlTables.CLSSid_StructuredDataType, null);
				if (e_1 == null) {
					throw new InvalidValueException("Null source for \'\'http://www.etsi.org/spec/TDL/1.4.1\'::Extension::extending\'");
				}
				final /*@Thrown*/ PackageableElement extending = e_1.getExtending();
				final /*@Thrown*/ StructuredDataType oclAsType = (StructuredDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, extending, TYP_tdl_c_c_StructuredDataType_0);
				final /*@Thrown*/ List<Constraint> allConstraints = oclAsType.allConstraints();
				final /*@Thrown*/ OrderedSetValue BOXED_allConstraints = idResolver.createOrderedSetOfAll(tdlTables.ORD_CLSSid_Constraint, allConstraints);
				//
				for (Object value : BOXED_allConstraints.flatten().getElements()) {
					accumulator.add(value);
				}
			}
			final /*@Thrown*/ BagValue union = (BagValue)CollectionUnionOperation.INSTANCE.evaluate(collect, BOXED_constraint_0);
			final /*@Thrown*/ OrderedSetValue asOrderedSet = CollectionAsOrderedSetOperation.INSTANCE.evaluate(union);
			local_0 = asOrderedSet;
		}
		else {
			local_0 = BOXED_constraint_0;
		}
		final /*@Thrown*/ List<Constraint> ECORE_local_0 = ((IdResolverExtension)idResolver).ecoreValueOfAll(Constraint.class, local_0);
		return (EList<Constraint>)ECORE_local_0;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
	{
		switch (featureID) {
			case tdlPackage.STRUCTURED_DATA_TYPE__EXTENSION:
				return ((InternalEList<?>)getExtension()).basicRemove(otherEnd, msgs);
			case tdlPackage.STRUCTURED_DATA_TYPE__MEMBER:
				return ((InternalEList<?>)getMember()).basicRemove(otherEnd, 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.STRUCTURED_DATA_TYPE__EXTENSION:
				return getExtension();
			case tdlPackage.STRUCTURED_DATA_TYPE__MEMBER:
				return getMember();
		}
		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.STRUCTURED_DATA_TYPE__EXTENSION:
				getExtension().clear();
				getExtension().addAll((Collection<? extends Extension>)newValue);
				return;
			case tdlPackage.STRUCTURED_DATA_TYPE__MEMBER:
				getMember().clear();
				getMember().addAll((Collection<? extends Member>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID)
	{
		switch (featureID) {
			case tdlPackage.STRUCTURED_DATA_TYPE__EXTENSION:
				getExtension().clear();
				return;
			case tdlPackage.STRUCTURED_DATA_TYPE__MEMBER:
				getMember().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID)
	{
		switch (featureID) {
			case tdlPackage.STRUCTURED_DATA_TYPE__EXTENSION:
				return extension != null && !extension.isEmpty();
			case tdlPackage.STRUCTURED_DATA_TYPE__MEMBER:
				return member != null && !member.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedOperationID(int baseOperationID, Class<?> baseClass)
	{
		if (baseClass == DataType.class) {
			switch (baseOperationID) {
				case tdlPackage.DATA_TYPE___ALL_CONSTRAINTS: return tdlPackage.STRUCTURED_DATA_TYPE___ALL_CONSTRAINTS;
				default: return super.eDerivedOperationID(baseOperationID, baseClass);
			}
		}
		return super.eDerivedOperationID(baseOperationID, baseClass);
	}

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

} //StructuredDataTypeImpl
