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

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;

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

import org.eclipse.xtext.xbase.lib.Functions.Function1;

import org.eclipse.xtext.xbase.lib.IterableExtensions;

import ua.ansymo.hintco.DecompositionPortAdaptation;
import ua.ansymo.hintco.HintcoPackage;
import ua.ansymo.hintco.PortAdaptation;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Decomposition Port Adaptation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link ua.ansymo.hintco.impl.DecompositionPortAdaptationImpl#getChildren <em>Children</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.DecompositionPortAdaptationImpl#getName <em>Name</em>}</li>
 * </ul>
 *
 * @generated
 */
public abstract class DecompositionPortAdaptationImpl extends PortAdaptationImpl implements DecompositionPortAdaptation {
	/**
	 * The cached value of the '{@link #getChildren() <em>Children</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getChildren()
	 * @generated
	 * @ordered
	 */
	protected EList<PortAdaptation> children;

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

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PortAdaptation> getChildren() {
		if (children == null) {
			children = new EObjectContainmentWithInverseEList<PortAdaptation>(PortAdaptation.class, this, HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__CHILDREN, HintcoPackage.PORT_ADAPTATION__PARENT);
		}
		return children;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		int _weight = this.getWeight();
		String _plus = ("(w=" + Integer.valueOf(_weight));
		return (_plus + ")");
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<PortAdaptation> selectedAdaptations() {
		int _size = this.getChildren().size();
		final BasicEList<PortAdaptation> res = new BasicEList<PortAdaptation>(_size);
		final Function1<PortAdaptation, Boolean> _function = new Function1<PortAdaptation, Boolean>() {
			public Boolean apply(final PortAdaptation a) {
				return Boolean.valueOf(a.isSelected());
			}
		};
		Iterable<PortAdaptation> _filter = IterableExtensions.<PortAdaptation>filter(this.getChildren(), _function);
		for (final PortAdaptation c : _filter) {
			if ((c instanceof DecompositionPortAdaptation)) {
				res.addAll(((DecompositionPortAdaptation)c).selectedAdaptations());
			}
			else {
				res.add(c);
			}
		}
		return res;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getChildren()).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.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				return ((InternalEList<?>)getChildren()).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.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				return getChildren();
			case HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__NAME:
				return getName();
		}
		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.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				getChildren().clear();
				getChildren().addAll((Collection<? extends PortAdaptation>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				getChildren().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__CHILDREN:
				return children != null && !children.isEmpty();
			case HintcoPackage.DECOMPOSITION_PORT_ADAPTATION__NAME:
				return NAME_EDEFAULT == null ? getName() != null : !NAME_EDEFAULT.equals(getName());
		}
		return super.eIsSet(featureID);
	}

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

} //DecompositionPortAdaptationImpl
