package org.pfaa.chemica.model;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public interface Mixture extends IndustrialMaterial {
	List<MixtureComponent> getComponents();
	
	default MixtureComponent getComponent(IndustrialMaterial material) {
		for (MixtureComponent comp : this.getComponents()) {
			if (comp.material.equals(material)) {
				return comp;
			}
		}
		return null;
	}
	
	default Mixture concentrate(Mixture mixture, int factor) {
		List<MixtureComponent> comps = mixture.getComponents();
		return new SimpleMixture(comps.stream().map((comp) -> {
			if (comp.weight < 1.0)
				return comp.concentrate(factor);
			return comp;
		}).collect(Collectors.toList()));
	}
	
	default Extraction extract(IndustrialMaterial extractant, IndustrialMaterial... materials) {
		Mixture extract = new SimpleMixture();
		Mixture residuum = new SimpleMixture();
		for (IndustrialMaterial material : materials) {
			MixtureComponent comp = this.getComponent(material);
			if (comp != null) {
				extract.mix(comp);
			}
		}
		List<IndustrialMaterial> materialList = Arrays.asList(materials);
		for (MixtureComponent comp : this.getComponents()) {
			if (!materialList.contains(comp.material)) {
				residuum.mix(comp);
			}
		}
		return new Extraction(extractant, extract, residuum);
	}
	
	default double getTotalWeight() {
		double weight = 0;
		for (MixtureComponent comp : this.getComponents()) {
			weight += comp.weight;
		}
		return weight;
	}

}

@@ -51,16 +51,43 @@
 Arrays;%0A
+import java.util.Iterator;%0A
 import j
@@ -101,16 +101,16 @@
 l.List;%0A
-
 import j
@@ -138,16 +138,57 @@
 ctors;%0A%0A
+import com.google.common.collect.Lists;%0A%0A
 public i
@@ -460,32 +460,32 @@
  comp;%0A%09%09%09%7D%0A%09%09%7D%0A
-
 %09%09return null;%0A%09
@@ -484,24 +484,362 @@
  null;%0A%09%7D%0A%09%0A
+%09default Mixture removeComponent(IndustrialMaterial material) %7B%0A%09%09List%3CMixtureComponent%3E comps = Lists.newArrayList(this.getComponents());%0A%09%09Iterator%3CMixtureComponent%3E it = comps.iterator();%0A%09%09while(it.hasNext()) %7B%0A%09%09%09if (it.next().material.equals(material)) %7B%0A%09%09%09%09it.remove();%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return new SimpleMixture(comps);%0A%09%7D%0A%09%0A
 %09default Mix

support removing material from mixture