public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException("Parameter is not a compatible type"); } if (resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof CountResult) { // add the two counts int toAdd = resultsToAdd.toInt(); return new CountResult(count + toAdd); } else if (resultsToAdd instanceof SumResult) { // we don't want to implement this twice, so we'll call the // SumResult version of this function return resultsToAdd.merge(this); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException("Parameter is not a compatible type"); } if (resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof SumResult) { // create a new strategy object of the correct dataType Number[] sums = new Number[2]; sums[0] = (Number) sum.getResult(); sums[1] = (Number) resultsToAdd.getValue(); SumStrategy newSum = createStrategy(CalcUtil.getObject(sums).getClass()); // add the two sums newSum.add(sums[0]); newSum.add(sums[1]); return new SumResult(newSum); } else if (resultsToAdd instanceof CountResult) { // SumResult + CountResult = AverageResult int count = resultsToAdd.toInt(); AverageResult newResult = new AverageResult(count, sum.getResult()); return newResult; } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
fc2.accepts(countVisitor2, null); int value1 = countVisitor.getResult().toInt(); assertEquals(3, value1); int value2 = countVisitor2.getResult().toInt(); assertEquals(2, value2); CalcResult countResult2 = countVisitor2.getResult(); CalcResult countResult3 = countResult1.merge(countResult2); assertEquals(5, countResult3.toInt()); assertEquals(20, countResult4.toInt()); assertEquals(5, countResult3.toInt()); assertEquals(20, countResult4.toInt()); assertEquals(25, countResult5.toInt());
int value = ((Integer) result).intValue(); assertEquals(1, value); int value2 = minVisitor.getResult().toInt(); assertEquals(1, value2); CalcResult minResult2 = minVisitor2.getResult(); CalcResult minResult3 = minResult1.merge(minResult2); assertEquals(1, minResult3.toInt()); assertEquals(1, minResult5.toInt()); assertEquals(10, minResult4.toInt()); assertEquals(-5, minResult7.toInt()); assertEquals(-5, minResult6.toInt()); assertEquals(-50, minResult7.toInt()); minResult7 = minResult7.merge(minResult1); assertEquals(-100.0, minResult7.toDouble(), 0); assertEquals(-100, minResult7.toInt());
assertEquals(2, medianResult1.toInt()); assertEquals(1.5, medianResult2.toDouble(), 0);
public void testCountSumMerge() throws IllegalFilterException, IOException { CountVisitor countVisitor = new CountVisitor(); fc2.accepts(countVisitor, null); // count = 2 SumVisitor sumVisitor = new SumVisitor(3, ft2); fc2.accepts(sumVisitor, null); // sum = 7.5 CalcResult countResult = countVisitor.getResult(); CalcResult sumResult = sumVisitor.getResult(); CalcResult averageResult1 = countResult.merge(sumResult); CalcResult averageResult2 = sumResult.merge(countResult); // both average results were correct? assertEquals((double) 3.75, averageResult1.toDouble(), 0); assertEquals((double) 3.75, averageResult2.toDouble(), 0); // neither sum nor count was destroyed? assertEquals(2, countResult.toInt()); assertEquals((double) 7.5, sumResult.toDouble(), 0); }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException( "Parameter is not a compatible type"); } if(resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof CountResult) { //add the two counts int toAdd = resultsToAdd.toInt(); return new CountResult(count + toAdd); } else if (resultsToAdd instanceof SumResult) { // we don't want to implement this twice, so we'll call the // SumResult version of this function return resultsToAdd.merge(this); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException( "Parameter is not a compatible type"); } if (resultsToAdd instanceof CountResult) { //add the two counts int toAdd = resultsToAdd.toInt(); return new CountResult(count + toAdd); } else if (resultsToAdd instanceof SumResult) { // we don't want to implement this twice, so we'll call the // SumResult version of this function return resultsToAdd.merge(this); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
int value1 = sumVisitor.getResult().toInt(); assertEquals(6, value1);
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException( "Parameter is not a compatible type"); } if (resultsToAdd instanceof SumResult) { //create a new strategy object of the correct dataType Number[] sums = new Number[2]; sums[0] = (Number) sum.getResult(); sums[1] = (Number) resultsToAdd.getValue(); SumStrategy newSum = createStrategy(CalcUtil.getObject(sums).getClass()); //add the two sums newSum.add(sums[0]); newSum.add(sums[1]); return new SumResult(newSum); } else if (resultsToAdd instanceof CountResult) { //SumResult + CountResult = AverageResult int count = resultsToAdd.toInt(); AverageResult newResult = new AverageResult(count, sum.getResult()); return newResult; } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }
public CalcResult merge(CalcResult resultsToAdd) { if (!isCompatible(resultsToAdd)) { throw new IllegalArgumentException( "Parameter is not a compatible type"); } if(resultsToAdd == CalcResult.NULL_RESULT) { return this; } if (resultsToAdd instanceof SumResult) { //create a new strategy object of the correct dataType Number[] sums = new Number[2]; sums[0] = (Number) sum.getResult(); sums[1] = (Number) resultsToAdd.getValue(); SumStrategy newSum = createStrategy(CalcUtil.getObject(sums).getClass()); //add the two sums newSum.add(sums[0]); newSum.add(sums[1]); return new SumResult(newSum); } else if (resultsToAdd instanceof CountResult) { //SumResult + CountResult = AverageResult int count = resultsToAdd.toInt(); AverageResult newResult = new AverageResult(count, sum.getResult()); return newResult; } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }