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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
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.EcoreUtil;

import ua.ansymo.hintco.DecompositionPortAdaptation;
import ua.ansymo.hintco.HintcoPackage;
import ua.ansymo.hintco.IDed;
import ua.ansymo.hintco.PortAdaptation;
import ua.ansymo.hintco.PortInstance;
import ua.ansymo.hintco.UnitInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Port Instance</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link ua.ansymo.hintco.impl.PortInstanceImpl#getIdentifier <em>Identifier</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.PortInstanceImpl#getUnit <em>Unit</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.PortInstanceImpl#getAdaptation <em>Adaptation</em>}</li>
 * </ul>
 *
 * @generated
 */
public abstract class PortInstanceImpl extends PrecedenceNodeImpl implements PortInstance {
	/**
	 * The default value of the '{@link #getIdentifier() <em>Identifier</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getIdentifier()
	 * @generated
	 * @ordered
	 */
	protected static final String IDENTIFIER_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getIdentifier() <em>Identifier</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getIdentifier()
	 * @generated
	 * @ordered
	 */
	protected String identifier = IDENTIFIER_EDEFAULT;

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

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getIdentifier() {
		return identifier;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setIdentifier(String newIdentifier) {
		String oldIdentifier = identifier;
		identifier = newIdentifier;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.PORT_INSTANCE__IDENTIFIER, oldIdentifier, identifier));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public UnitInstance getUnit() {
		if (eContainerFeatureID() != HintcoPackage.PORT_INSTANCE__UNIT) return null;
		return (UnitInstance)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public UnitInstance basicGetUnit() {
		if (eContainerFeatureID() != HintcoPackage.PORT_INSTANCE__UNIT) return null;
		return (UnitInstance)eInternalContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetUnit(UnitInstance newUnit, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newUnit, HintcoPackage.PORT_INSTANCE__UNIT, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setUnit(UnitInstance newUnit) {
		if (newUnit != eInternalContainer() || (eContainerFeatureID() != HintcoPackage.PORT_INSTANCE__UNIT && newUnit != null)) {
			if (EcoreUtil.isAncestor(this, newUnit))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newUnit != null)
				msgs = ((InternalEObject)newUnit).eInverseAdd(this, HintcoPackage.UNIT_INSTANCE__PORTS, UnitInstance.class, msgs);
			msgs = basicSetUnit(newUnit, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.PORT_INSTANCE__UNIT, newUnit, newUnit));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public PortAdaptation getAdaptation() {
		return adaptation;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetAdaptation(PortAdaptation newAdaptation, NotificationChain msgs) {
		PortAdaptation oldAdaptation = adaptation;
		adaptation = newAdaptation;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, HintcoPackage.PORT_INSTANCE__ADAPTATION, oldAdaptation, newAdaptation);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAdaptation(PortAdaptation newAdaptation) {
		if (newAdaptation != adaptation) {
			NotificationChain msgs = null;
			if (adaptation != null)
				msgs = ((InternalEObject)adaptation).eInverseRemove(this, HintcoPackage.PORT_ADAPTATION__PORT, PortAdaptation.class, msgs);
			if (newAdaptation != null)
				msgs = ((InternalEObject)newAdaptation).eInverseAdd(this, HintcoPackage.PORT_ADAPTATION__PORT, PortAdaptation.class, msgs);
			msgs = basicSetAdaptation(newAdaptation, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.PORT_INSTANCE__ADAPTATION, newAdaptation, newAdaptation));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PortAdaptation> selectedAdaptations() {
		PortAdaptation _adaptation = this.getAdaptation();
		boolean _tripleEquals = (_adaptation == null);
		if (_tripleEquals) {
			return new BasicEList<PortAdaptation>(java.util.Collections.<PortAdaptation>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<PortAdaptation>newArrayList()));
		}
		boolean _isSelected = this.getAdaptation().isSelected();
		if (_isSelected) {
			PortAdaptation _adaptation_1 = this.getAdaptation();
			if ((_adaptation_1 instanceof DecompositionPortAdaptation)) {
				PortAdaptation _adaptation_2 = this.getAdaptation();
				return ((DecompositionPortAdaptation) _adaptation_2).selectedAdaptations();
			}
			PortAdaptation _adaptation_3 = this.getAdaptation();
			return new BasicEList<PortAdaptation>(java.util.Collections.<PortAdaptation>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<PortAdaptation>newArrayList(_adaptation_3)));
		}
		return new BasicEList<PortAdaptation>(java.util.Collections.<PortAdaptation>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<PortAdaptation>newArrayList()));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isInput() {
		return false;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean getsValueFrom(final PortInstance p) {
		return false;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String toString() {
		String _string = this.getUnit().toString();
		String _plus = (_string + ".");
		String _identifier = this.getIdentifier();
		return (_plus + _identifier);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.PORT_INSTANCE__UNIT:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetUnit((UnitInstance)otherEnd, msgs);
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				if (adaptation != null)
					msgs = ((InternalEObject)adaptation).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - HintcoPackage.PORT_INSTANCE__ADAPTATION, null, msgs);
				return basicSetAdaptation((PortAdaptation)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.PORT_INSTANCE__UNIT:
				return basicSetUnit(null, msgs);
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				return basicSetAdaptation(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case HintcoPackage.PORT_INSTANCE__UNIT:
				return eInternalContainer().eInverseRemove(this, HintcoPackage.UNIT_INSTANCE__PORTS, UnitInstance.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case HintcoPackage.PORT_INSTANCE__IDENTIFIER:
				return getIdentifier();
			case HintcoPackage.PORT_INSTANCE__UNIT:
				if (resolve) return getUnit();
				return basicGetUnit();
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				return getAdaptation();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case HintcoPackage.PORT_INSTANCE__IDENTIFIER:
				setIdentifier((String)newValue);
				return;
			case HintcoPackage.PORT_INSTANCE__UNIT:
				setUnit((UnitInstance)newValue);
				return;
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				setAdaptation((PortAdaptation)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.PORT_INSTANCE__IDENTIFIER:
				setIdentifier(IDENTIFIER_EDEFAULT);
				return;
			case HintcoPackage.PORT_INSTANCE__UNIT:
				setUnit((UnitInstance)null);
				return;
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				setAdaptation((PortAdaptation)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case HintcoPackage.PORT_INSTANCE__IDENTIFIER:
				return IDENTIFIER_EDEFAULT == null ? identifier != null : !IDENTIFIER_EDEFAULT.equals(identifier);
			case HintcoPackage.PORT_INSTANCE__UNIT:
				return basicGetUnit() != null;
			case HintcoPackage.PORT_INSTANCE__ADAPTATION:
				return adaptation != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == IDed.class) {
			switch (derivedFeatureID) {
				case HintcoPackage.PORT_INSTANCE__IDENTIFIER: return HintcoPackage.IDED__IDENTIFIER;
				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 == IDed.class) {
			switch (baseFeatureID) {
				case HintcoPackage.IDED__IDENTIFIER: return HintcoPackage.PORT_INSTANCE__IDENTIFIER;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments) throws InvocationTargetException {
		switch (operationID) {
			case HintcoPackage.PORT_INSTANCE___SELECTED_ADAPTATIONS:
				return selectedAdaptations();
			case HintcoPackage.PORT_INSTANCE___IS_INPUT:
				return isInput();
			case HintcoPackage.PORT_INSTANCE___GETS_VALUE_FROM__PORTINSTANCE:
				return getsValueFrom((PortInstance)arguments.get(0));
			case HintcoPackage.PORT_INSTANCE___TO_STRING:
				return toString();
		}
		return super.eInvoke(operationID, arguments);
	}

} //PortInstanceImpl
