/**
 * generated by Xtext 2.26.0
 */
package com.minres.rdl.rdl.impl;

import com.minres.rdl.rdl.ComponentDefinition;
import com.minres.rdl.rdl.ComponentInstance;
import com.minres.rdl.rdl.EnumInstanceType;
import com.minres.rdl.rdl.Instantiation;
import com.minres.rdl.rdl.RdlPackage;

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.impl.MinimalEObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Instantiation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link com.minres.rdl.rdl.impl.InstantiationImpl#getInstanceType <em>Instance Type</em>}</li>
 *   <li>{@link com.minres.rdl.rdl.impl.InstantiationImpl#getAlias <em>Alias</em>}</li>
 *   <li>{@link com.minres.rdl.rdl.impl.InstantiationImpl#getComponentRef <em>Component Ref</em>}</li>
 *   <li>{@link com.minres.rdl.rdl.impl.InstantiationImpl#getComponent <em>Component</em>}</li>
 *   <li>{@link com.minres.rdl.rdl.impl.InstantiationImpl#getComponentInstances <em>Component Instances</em>}</li>
 * </ul>
 *
 * @generated
 */
public class InstantiationImpl extends MinimalEObjectImpl.Container implements Instantiation
{
  /**
   * The cached value of the '{@link #getInstanceType() <em>Instance Type</em>}' containment reference.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getInstanceType()
   * @generated
   * @ordered
   */
  protected EnumInstanceType instanceType;

  /**
   * The default value of the '{@link #getAlias() <em>Alias</em>}' attribute.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getAlias()
   * @generated
   * @ordered
   */
  protected static final String ALIAS_EDEFAULT = null;

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

  /**
   * The cached value of the '{@link #getComponentRef() <em>Component Ref</em>}' reference.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getComponentRef()
   * @generated
   * @ordered
   */
  protected ComponentDefinition componentRef;

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

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

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

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

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public EnumInstanceType getInstanceType()
  {
    return instanceType;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public NotificationChain basicSetInstanceType(EnumInstanceType newInstanceType, NotificationChain msgs)
  {
    EnumInstanceType oldInstanceType = instanceType;
    instanceType = newInstanceType;
    if (eNotificationRequired())
    {
      ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__INSTANCE_TYPE, oldInstanceType, newInstanceType);
      if (msgs == null) msgs = notification; else msgs.add(notification);
    }
    return msgs;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void setInstanceType(EnumInstanceType newInstanceType)
  {
    if (newInstanceType != instanceType)
    {
      NotificationChain msgs = null;
      if (instanceType != null)
        msgs = ((InternalEObject)instanceType).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - RdlPackage.INSTANTIATION__INSTANCE_TYPE, null, msgs);
      if (newInstanceType != null)
        msgs = ((InternalEObject)newInstanceType).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - RdlPackage.INSTANTIATION__INSTANCE_TYPE, null, msgs);
      msgs = basicSetInstanceType(newInstanceType, msgs);
      if (msgs != null) msgs.dispatch();
    }
    else if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__INSTANCE_TYPE, newInstanceType, newInstanceType));
  }

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

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void setAlias(String newAlias)
  {
    String oldAlias = alias;
    alias = newAlias;
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__ALIAS, oldAlias, alias));
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public ComponentDefinition getComponentRef()
  {
    if (componentRef != null && componentRef.eIsProxy())
    {
      InternalEObject oldComponentRef = (InternalEObject)componentRef;
      componentRef = (ComponentDefinition)eResolveProxy(oldComponentRef);
      if (componentRef != oldComponentRef)
      {
        if (eNotificationRequired())
          eNotify(new ENotificationImpl(this, Notification.RESOLVE, RdlPackage.INSTANTIATION__COMPONENT_REF, oldComponentRef, componentRef));
      }
    }
    return componentRef;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public ComponentDefinition basicGetComponentRef()
  {
    return componentRef;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void setComponentRef(ComponentDefinition newComponentRef)
  {
    ComponentDefinition oldComponentRef = componentRef;
    componentRef = newComponentRef;
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__COMPONENT_REF, oldComponentRef, componentRef));
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public ComponentDefinition getComponent()
  {
    return component;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public NotificationChain basicSetComponent(ComponentDefinition newComponent, NotificationChain msgs)
  {
    ComponentDefinition oldComponent = component;
    component = newComponent;
    if (eNotificationRequired())
    {
      ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__COMPONENT, oldComponent, newComponent);
      if (msgs == null) msgs = notification; else msgs.add(notification);
    }
    return msgs;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void setComponent(ComponentDefinition newComponent)
  {
    if (newComponent != component)
    {
      NotificationChain msgs = null;
      if (component != null)
        msgs = ((InternalEObject)component).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - RdlPackage.INSTANTIATION__COMPONENT, null, msgs);
      if (newComponent != null)
        msgs = ((InternalEObject)newComponent).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - RdlPackage.INSTANTIATION__COMPONENT, null, msgs);
      msgs = basicSetComponent(newComponent, msgs);
      if (msgs != null) msgs.dispatch();
    }
    else if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, RdlPackage.INSTANTIATION__COMPONENT, newComponent, newComponent));
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public EList<ComponentInstance> getComponentInstances()
  {
    if (componentInstances == null)
    {
      componentInstances = new EObjectContainmentEList<ComponentInstance>(ComponentInstance.class, this, RdlPackage.INSTANTIATION__COMPONENT_INSTANCES);
    }
    return componentInstances;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
  {
    switch (featureID)
    {
      case RdlPackage.INSTANTIATION__INSTANCE_TYPE:
        return basicSetInstanceType(null, msgs);
      case RdlPackage.INSTANTIATION__COMPONENT:
        return basicSetComponent(null, msgs);
      case RdlPackage.INSTANTIATION__COMPONENT_INSTANCES:
        return ((InternalEList<?>)getComponentInstances()).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 RdlPackage.INSTANTIATION__INSTANCE_TYPE:
        return getInstanceType();
      case RdlPackage.INSTANTIATION__ALIAS:
        return getAlias();
      case RdlPackage.INSTANTIATION__COMPONENT_REF:
        if (resolve) return getComponentRef();
        return basicGetComponentRef();
      case RdlPackage.INSTANTIATION__COMPONENT:
        return getComponent();
      case RdlPackage.INSTANTIATION__COMPONENT_INSTANCES:
        return getComponentInstances();
    }
    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 RdlPackage.INSTANTIATION__INSTANCE_TYPE:
        setInstanceType((EnumInstanceType)newValue);
        return;
      case RdlPackage.INSTANTIATION__ALIAS:
        setAlias((String)newValue);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT_REF:
        setComponentRef((ComponentDefinition)newValue);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT:
        setComponent((ComponentDefinition)newValue);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT_INSTANCES:
        getComponentInstances().clear();
        getComponentInstances().addAll((Collection<? extends ComponentInstance>)newValue);
        return;
    }
    super.eSet(featureID, newValue);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void eUnset(int featureID)
  {
    switch (featureID)
    {
      case RdlPackage.INSTANTIATION__INSTANCE_TYPE:
        setInstanceType((EnumInstanceType)null);
        return;
      case RdlPackage.INSTANTIATION__ALIAS:
        setAlias(ALIAS_EDEFAULT);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT_REF:
        setComponentRef((ComponentDefinition)null);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT:
        setComponent((ComponentDefinition)null);
        return;
      case RdlPackage.INSTANTIATION__COMPONENT_INSTANCES:
        getComponentInstances().clear();
        return;
    }
    super.eUnset(featureID);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public boolean eIsSet(int featureID)
  {
    switch (featureID)
    {
      case RdlPackage.INSTANTIATION__INSTANCE_TYPE:
        return instanceType != null;
      case RdlPackage.INSTANTIATION__ALIAS:
        return ALIAS_EDEFAULT == null ? alias != null : !ALIAS_EDEFAULT.equals(alias);
      case RdlPackage.INSTANTIATION__COMPONENT_REF:
        return componentRef != null;
      case RdlPackage.INSTANTIATION__COMPONENT:
        return component != null;
      case RdlPackage.INSTANTIATION__COMPONENT_INSTANCES:
        return componentInstances != null && !componentInstances.isEmpty();
    }
    return super.eIsSet(featureID);
  }

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

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

} //InstantiationImpl
