/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.edge;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BaseEntity {
	
	private List<BaseEntity> parents = new ArrayList<>(2);
	private List<BaseEntity> children = new ArrayList<>(2);
	
	public void addChild(BaseEntity e) {
		children.add(e);
		e.parents.add(this);
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getParents(Class<T> clazz) {
		Set<T> result = new HashSet<>();
		for (BaseEntity b : parents) {
			if (clazz.isAssignableFrom(b.getClass())) {
				result.add((T) b);
			}
		}
		return new ArrayList<>(result);
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getChildren(Class<T> clazz) {
		List<T> result = new ArrayList<>();
		for (BaseEntity b : children) {
			if (b.getClass() == clazz) {
				result.add((T) b);
			}
		}
		return result;
	}
	
	public void removeChild(BaseEntity pEntity) {
		children.remove(pEntity);
		
		 // check for multiple relationships
		pEntity.parents.remove(this);
	}

}
