SubtractiveMixingStrategy implements MixingStrategy { public Liquid mixLiquids(LinkedList<Liquid> inflow) { if (inflow == null || inflow.isEmpty()) { throw new IllegalArgumentException("Must at least mix one liquid"); } float fullAmount = 0; for (Liquid liquid : inflow) { fullAmount += liquid.getAmount(); } if (Math.abs(fullAmount) < 0.000001) { return new Liquid(0, inflow.peekFirst().getTemperature(), inflow.peekFirst().getColor()); } double c = 0; double m = 0; double y = 0; float temperature = 0; for (Liquid liquid : inflow) { c += (liquid.getAmount() / fullAmount) * liquid.getColor().getCyan(); m += (liquid.getAmount() / fullAmount) * liquid.getColor().getMagenta(); y += (liquid.getAmount() / fullAmount) * liquid.getColor().getYellow(); temperature += (liquid.getAmount() / fullAmount) * liquid.getTemperature(); } c = Math.min(1, c); m = Math.min(1, m); y = Math.min(1, y); temperature = Math.min(SimulationConstants.MAX_TEMPERATURE, Math.max(SimulationConstants.MIN_TEMPERATURE, temperature)); return new Liquid(fullAmount, temperature, new Color(c, m, y)); }  Liquid mixLiquids(LinkedList<Liquid> inflow);  }
@Test public void testSingleLiquid() { Liquid liquid = new Liquid(100, 350, new Color(0x000000)); LinkedList<Liquid> liquids = new LinkedList<>(); liquids.add(liquid); assertEquals(liquid, new SubtractiveMixingStrategy().mixLiquids(liquids)); }
@Test public void testMixingTwoLiquids() { LinkedList<Liquid> liquids = new LinkedList<>(); liquids.add(new Liquid(100, 350, new Color(0x000000))); liquids.add(new Liquid(100, 300, new Color(0xfefefe))); Liquid result = new SubtractiveMixingStrategy().mixLiquids(liquids); assertEquals(new Liquid(200, 325, new Color(0x7f7f7f)), result); }
@Test public void testMixingTwoLiquidsDifferentAmounts() { LinkedList<Liquid> liquids = new LinkedList<>(); liquids.add(new Liquid(300, 340, new Color(0x880000))); liquids.add(new Liquid(100, 300, new Color(0x440000))); Liquid result = new SubtractiveMixingStrategy().mixLiquids(liquids); assertEquals(new Liquid(400, 330, new Color(0x770000)), result); }
@Test public void testMixingMultipleLiquids() { LinkedList<Liquid> liquids = new LinkedList<>(); liquids.add(new Liquid(100, 350, new Color(0x000000))); liquids.add(new Liquid(100, 300, new Color(0x000005))); liquids.add(new Liquid(100, 300, new Color(0x000000))); liquids.add(new Liquid(100, 300, new Color(0x00a000))); liquids.add(new Liquid(100, 300, new Color(0x500000))); Liquid result = new SubtractiveMixingStrategy().mixLiquids(liquids); assertEquals(new Liquid(500, 310, new Color(0x102001)), result); }
@Test(expected = IllegalArgumentException.class) public void testMixingEmptyListOfLiquids() { LinkedList<Liquid> liquids = new LinkedList<>(); new SubtractiveMixingStrategy().mixLiquids(liquids); }
@Test(expected = IllegalArgumentException.class) public void testMixingNullListOfLiquids() { new SubtractiveMixingStrategy().mixLiquids(null); }
@Test public void testNoAmount() { LinkedList<Liquid> liquids = new LinkedList<>(); liquids.add(new Liquid(0, 300, new Color(0x000000))); liquids.add(new Liquid(0, 300, new Color(0xfefefe))); Liquid result = new SubtractiveMixingStrategy().mixLiquids(liquids); assertEquals(0, result.getAmount(), 0.00001); }