/**
 *
 */
package org.jmist.framework.model;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import org.jmist.framework.Intersection;
import org.jmist.toolkit.BoundingBoxBuilder3;
import org.jmist.toolkit.Box3;
import org.jmist.toolkit.Interval;
import org.jmist.toolkit.Ray3;
import org.jmist.toolkit.Sphere;

/**
 * @author bkimmel
 *
 */
public class CompositeModelElement extends AbstractModelElement {

	/**
	 * Initializes the name of this model element.
	 * @param name The name of this model element.
	 */
	public CompositeModelElement(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#addChild(org.jmist.framework.model.ModelElement)
	 */
	@Override
	public void addChild(ModelElement child) throws UnsupportedOperationException {
		children.add(child);
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#children()
	 */
	@Override
	public Iterable<ModelElement> children() {
		return children;
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#findChild(java.lang.String)
	 */
	@Override
	public ModelElement findChild(String name) {
		for (ModelElement child : this.children()) {
			if (child.getName().equals(name)) {
				return child;
			}
		}

		return null;
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#getBoundingBox()
	 */
	@Override
	public Box3 boundingBox() {
		BoundingBoxBuilder3 builder = new BoundingBoxBuilder3();

		for (ModelElement child : this.children()) {
			builder.add(child.boundingBox());
		}

		return builder.getBoundingBox();
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#getBoundingSphere()
	 */
	@Override
	public Sphere boundingSphere() {
		// TODO Create a bounding sphere builder and use that instead.
		Box3 boundingBox = this.boundingBox();
		return new Sphere(boundingBox.center(), boundingBox.diagonal() / 2.0);
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#removeChild(java.lang.String)
	 */
	@Override
	public void removeChild(String name) {
		for (Iterator<ModelElement> iter = children.iterator(); iter.hasNext();) {
			if (iter.next().getName().equals(name)) {
				iter.remove();
				break;
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.jmist.framework.model.AbstractModelElement#visibility(org.jmist.toolkit.Ray3, org.jmist.toolkit.Interval)
	 */
	@Override
	public boolean visibility(Ray3 ray, Interval I) {
		for (ModelElement child : this.children()) {
			if (!child.visibility(ray, I)) {
				return false;
			}
		}

		return true;
	}

	private List<ModelElement> children = new ArrayList<ModelElement>();

}

@@ -900,32 +900,24 @@
 ework.model.
-Abstract
 ModelElement
@@ -917,25 +917,66 @@
 Element#
-children(
+removeChild(org.jmist.framework.model.ModelElement
 )%0A%09 */%0A%09
@@ -997,16 +997,58 @@
 lic 
+void removeChild(ModelElement child) %7B%0A%0A%09%09
 Itera
-ble
+tor
 %3CMod
@@ -1062,241 +1062,264 @@
 nt%3E 
-children() %7B%0A%09%09return children;%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see org.jmist.framework.model.AbstractModelElement#findChild(java.lang.String)%0A%09 */%0A%09@Override%0A%09public ModelElement findChild(String name) %7B%0A%09%09for (ModelElement child : this.
+i = this.children.iterator();%0A%0A%09%09while (i.hasNext()) %7B%0A%09%09%09if (i.next() == child) %7B%0A%09%09%09%09i.remove();%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see org.jmist.framework.model.AbstractModelElement#children()%0A%09 */%0A%09@Override%0A%09public Iterable%3CModelElement%3E 
 chil
@@ -1328,92 +1328,28 @@
 en()
-)
  %7B%0A%09%09
-%09if (child.getName().equals(name)) %7B%0A%09%09%09%09return child;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return null
+return children
 ;%0A%09%7D
@@ -2005,333 +2005,8 @@
 %09%7D%0A%0A
-%09/* (non-Javadoc)%0A%09 * @see org.jmist.framework.model.AbstractModelElement#removeChild(java.lang.String)%0A%09 */%0A%09@Override%0A%09public void removeChild(String name) %7B%0A%09%09for (Iterator%3CModelElement%3E iter = children.iterator(); iter.hasNext();) %7B%0A%09%09%09if (iter.next().getName().equals(name)) %7B%0A%09%09%09%09iter.remove();%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %09/* 

Update CompositeModelElement to reflect previous changes to ModelElement (formerly IModelElement).