/**
 */
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.DecompositionUnitAdaptation;
import ua.ansymo.hintco.HintcoPackage;
import ua.ansymo.hintco.UnitAdaptation;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Decomposition Unit Adaptation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link ua.ansymo.hintco.impl.DecompositionUnitAdaptationImpl#getChildren <em>Children</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.DecompositionUnitAdaptationImpl#getName <em>Name</em>}</li>
 * </ul>
 *
 * @generated
 */
public abstract class DecompositionUnitAdaptationImpl extends UnitAdaptationImpl implements DecompositionUnitAdaptation {
	/**
	 * 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<UnitAdaptation> 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 DecompositionUnitAdaptationImpl() {
		super();
	}

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<UnitAdaptation> getChildren() {
		if (children == null) {
			children = new EObjectContainmentWithInverseEList<UnitAdaptation>(UnitAdaptation.class, this, HintcoPackage.DECOMPOSITION_UNIT_ADAPTATION__CHILDREN, HintcoPackage.UNIT_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<UnitAdaptation> selectedAdaptations() {
		int _size = this.getChildren().size();
		final BasicEList<UnitAdaptation> res = new BasicEList<UnitAdaptation>(_size);
		final Function1<UnitAdaptation, Boolean> _function = new Function1<UnitAdaptation, Boolean>() {
			public Boolean apply(final UnitAdaptation a) {
				return Boolean.valueOf(a.isSelected());
			}
		};
		Iterable<UnitAdaptation> _filter = IterableExtensions.<UnitAdaptation>filter(this.getChildren(), _function);
		for (final UnitAdaptation c : _filter) {
			if ((c instanceof DecompositionUnitAdaptation)) {
				res.addAll(((DecompositionUnitAdaptation)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_UNIT_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_UNIT_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_UNIT_ADAPTATION__CHILDREN:
				return getChildren();
			case HintcoPackage.DECOMPOSITION_UNIT_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_UNIT_ADAPTATION__CHILDREN:
				getChildren().clear();
				getChildren().addAll((Collection<? extends UnitAdaptation>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.DECOMPOSITION_UNIT_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_UNIT_ADAPTATION__CHILDREN:
				return children != null && !children.isEmpty();
			case HintcoPackage.DECOMPOSITION_UNIT_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_UNIT_ADAPTATION___SELECTED_ADAPTATIONS:
				return selectedAdaptations();
		}
		return super.eInvoke(operationID, arguments);
	}

} //DecompositionUnitAdaptationImpl
