/**
 */
package ua.ansymo.hintco.impl;

import java.util.Collection;

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.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

import ua.ansymo.hintco.CosimUnitDeclaration;
import ua.ansymo.hintco.CosimUnitInstance;
import ua.ansymo.hintco.HintcoPackage;
import ua.ansymo.hintco.PortInstance;
import ua.ansymo.hintco.PrecendenceNode;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Cosim Unit Instance</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getPrecedes <em>Precedes</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getSucceeds <em>Succeeds</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getBefore <em>Before</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getAfter <em>After</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getDeclaration <em>Declaration</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.CosimUnitInstanceImpl#getPorts <em>Ports</em>}</li>
 * </ul>
 *
 * @generated
 */
public class CosimUnitInstanceImpl extends IDedImpl implements CosimUnitInstance {
	/**
	 * The cached value of the '{@link #getPrecedes() <em>Precedes</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPrecedes()
	 * @generated
	 * @ordered
	 */
	protected EList<PrecendenceNode> precedes;

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

	/**
	 * The cached value of the '{@link #getBefore() <em>Before</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBefore()
	 * @generated
	 * @ordered
	 */
	protected PrecendenceNode before;

	/**
	 * The cached value of the '{@link #getAfter() <em>After</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getAfter()
	 * @generated
	 * @ordered
	 */
	protected PrecendenceNode after;

	/**
	 * The cached value of the '{@link #getDeclaration() <em>Declaration</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDeclaration()
	 * @generated
	 * @ordered
	 */
	protected CosimUnitDeclaration declaration;

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

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PrecendenceNode> getPrecedes() {
		if (precedes == null) {
			precedes = new EObjectWithInverseResolvingEList.ManyInverse<PrecendenceNode>(PrecendenceNode.class, this, HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES, HintcoPackage.PRECENDENCE_NODE__SUCCEEDS);
		}
		return precedes;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PrecendenceNode> getSucceeds() {
		if (succeeds == null) {
			succeeds = new EObjectWithInverseResolvingEList.ManyInverse<PrecendenceNode>(PrecendenceNode.class, this, HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS, HintcoPackage.PRECENDENCE_NODE__PRECEDES);
		}
		return succeeds;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PrecendenceNode getBefore() {
		if (before != null && before.eIsProxy()) {
			InternalEObject oldBefore = (InternalEObject)before;
			before = (PrecendenceNode)eResolveProxy(oldBefore);
			if (before != oldBefore) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE, oldBefore, before));
			}
		}
		return before;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PrecendenceNode basicGetBefore() {
		return before;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetBefore(PrecendenceNode newBefore, NotificationChain msgs) {
		PrecendenceNode oldBefore = before;
		before = newBefore;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE, oldBefore, newBefore);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBefore(PrecendenceNode newBefore) {
		if (newBefore != before) {
			NotificationChain msgs = null;
			if (before != null)
				msgs = ((InternalEObject)before).eInverseRemove(this, HintcoPackage.PRECENDENCE_NODE__AFTER, PrecendenceNode.class, msgs);
			if (newBefore != null)
				msgs = ((InternalEObject)newBefore).eInverseAdd(this, HintcoPackage.PRECENDENCE_NODE__AFTER, PrecendenceNode.class, msgs);
			msgs = basicSetBefore(newBefore, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE, newBefore, newBefore));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PrecendenceNode getAfter() {
		if (after != null && after.eIsProxy()) {
			InternalEObject oldAfter = (InternalEObject)after;
			after = (PrecendenceNode)eResolveProxy(oldAfter);
			if (after != oldAfter) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, HintcoPackage.COSIM_UNIT_INSTANCE__AFTER, oldAfter, after));
			}
		}
		return after;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PrecendenceNode basicGetAfter() {
		return after;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetAfter(PrecendenceNode newAfter, NotificationChain msgs) {
		PrecendenceNode oldAfter = after;
		after = newAfter;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, HintcoPackage.COSIM_UNIT_INSTANCE__AFTER, oldAfter, newAfter);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAfter(PrecendenceNode newAfter) {
		if (newAfter != after) {
			NotificationChain msgs = null;
			if (after != null)
				msgs = ((InternalEObject)after).eInverseRemove(this, HintcoPackage.PRECENDENCE_NODE__BEFORE, PrecendenceNode.class, msgs);
			if (newAfter != null)
				msgs = ((InternalEObject)newAfter).eInverseAdd(this, HintcoPackage.PRECENDENCE_NODE__BEFORE, PrecendenceNode.class, msgs);
			msgs = basicSetAfter(newAfter, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.COSIM_UNIT_INSTANCE__AFTER, newAfter, newAfter));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CosimUnitDeclaration getDeclaration() {
		if (declaration != null && declaration.eIsProxy()) {
			InternalEObject oldDeclaration = (InternalEObject)declaration;
			declaration = (CosimUnitDeclaration)eResolveProxy(oldDeclaration);
			if (declaration != oldDeclaration) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION, oldDeclaration, declaration));
			}
		}
		return declaration;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CosimUnitDeclaration basicGetDeclaration() {
		return declaration;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDeclaration(CosimUnitDeclaration newDeclaration) {
		CosimUnitDeclaration oldDeclaration = declaration;
		declaration = newDeclaration;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION, oldDeclaration, declaration));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PortInstance> getPorts() {
		if (ports == null) {
			ports = new EObjectContainmentWithInverseEList<PortInstance>(PortInstance.class, this, HintcoPackage.COSIM_UNIT_INSTANCE__PORTS, HintcoPackage.PORT_INSTANCE__UNIT);
		}
		return ports;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getPrecedes()).basicAdd(otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getSucceeds()).basicAdd(otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				if (before != null)
					msgs = ((InternalEObject)before).eInverseRemove(this, HintcoPackage.PRECENDENCE_NODE__AFTER, PrecendenceNode.class, msgs);
				return basicSetBefore((PrecendenceNode)otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				if (after != null)
					msgs = ((InternalEObject)after).eInverseRemove(this, HintcoPackage.PRECENDENCE_NODE__BEFORE, PrecendenceNode.class, msgs);
				return basicSetAfter((PrecendenceNode)otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getPorts()).basicAdd(otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				return ((InternalEList<?>)getPrecedes()).basicRemove(otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				return ((InternalEList<?>)getSucceeds()).basicRemove(otherEnd, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				return basicSetBefore(null, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				return basicSetAfter(null, msgs);
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				return ((InternalEList<?>)getPorts()).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 HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				return getPrecedes();
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				return getSucceeds();
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				if (resolve) return getBefore();
				return basicGetBefore();
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				if (resolve) return getAfter();
				return basicGetAfter();
			case HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION:
				if (resolve) return getDeclaration();
				return basicGetDeclaration();
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				return getPorts();
		}
		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 HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				getPrecedes().clear();
				getPrecedes().addAll((Collection<? extends PrecendenceNode>)newValue);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				getSucceeds().clear();
				getSucceeds().addAll((Collection<? extends PrecendenceNode>)newValue);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				setBefore((PrecendenceNode)newValue);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				setAfter((PrecendenceNode)newValue);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION:
				setDeclaration((CosimUnitDeclaration)newValue);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				getPorts().clear();
				getPorts().addAll((Collection<? extends PortInstance>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				getPrecedes().clear();
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				getSucceeds().clear();
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				setBefore((PrecendenceNode)null);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				setAfter((PrecendenceNode)null);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION:
				setDeclaration((CosimUnitDeclaration)null);
				return;
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				getPorts().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES:
				return precedes != null && !precedes.isEmpty();
			case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS:
				return succeeds != null && !succeeds.isEmpty();
			case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE:
				return before != null;
			case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER:
				return after != null;
			case HintcoPackage.COSIM_UNIT_INSTANCE__DECLARATION:
				return declaration != null;
			case HintcoPackage.COSIM_UNIT_INSTANCE__PORTS:
				return ports != null && !ports.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == PrecendenceNode.class) {
			switch (derivedFeatureID) {
				case HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES: return HintcoPackage.PRECENDENCE_NODE__PRECEDES;
				case HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS: return HintcoPackage.PRECENDENCE_NODE__SUCCEEDS;
				case HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE: return HintcoPackage.PRECENDENCE_NODE__BEFORE;
				case HintcoPackage.COSIM_UNIT_INSTANCE__AFTER: return HintcoPackage.PRECENDENCE_NODE__AFTER;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == PrecendenceNode.class) {
			switch (baseFeatureID) {
				case HintcoPackage.PRECENDENCE_NODE__PRECEDES: return HintcoPackage.COSIM_UNIT_INSTANCE__PRECEDES;
				case HintcoPackage.PRECENDENCE_NODE__SUCCEEDS: return HintcoPackage.COSIM_UNIT_INSTANCE__SUCCEEDS;
				case HintcoPackage.PRECENDENCE_NODE__BEFORE: return HintcoPackage.COSIM_UNIT_INSTANCE__BEFORE;
				case HintcoPackage.PRECENDENCE_NODE__AFTER: return HintcoPackage.COSIM_UNIT_INSTANCE__AFTER;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

} //CosimUnitInstanceImpl
