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 MinResult) { // take the smaller of the 2 values Comparable toAdd = (Comparable) resultsToAdd.getValue(); Comparable newMin = minValue; if (newMin.getClass() != toAdd.getClass()) { // 2 different data types, therefore convert Class bestClass = CalcUtil.bestClass(new Object[] {toAdd, newMin}); if (bestClass != toAdd.getClass()) toAdd = (Comparable) CalcUtil.convert(toAdd, bestClass); if (bestClass != newMin.getClass()) newMin = (Comparable) CalcUtil.convert(newMin, bestClass); } if (newMin.compareTo(toAdd) > 0) { newMin = toAdd; } return new MinResult(newMin); } 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 MinResult) { //take the smaller of the 2 values Comparable toAdd = (Comparable) resultsToAdd.getValue(); Comparable newMin = minValue; if (newMin.getClass() != toAdd.getClass()) { //2 different data types, therefore convert Class bestClass = CalcUtil.bestClass(new Object[] {toAdd, newMin}); if (bestClass != toAdd.getClass()) toAdd = (Comparable) CalcUtil.convert(toAdd, bestClass); if (bestClass != newMin.getClass()) newMin = (Comparable) CalcUtil.convert(newMin, bestClass); } if (newMin.compareTo(toAdd) > 0) { newMin = toAdd; } return new MinResult(newMin); } 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 MinResult) { //take the smaller of the 2 values Comparable toAdd = (Comparable) resultsToAdd.getValue(); Comparable newMin = minValue; if (newMin.getClass() != toAdd.getClass()) { //2 different data types, therefore convert Class bestClass = CalcUtil.bestClass(new Object[] {toAdd, newMin}); if (bestClass != toAdd.getClass()) toAdd = (Comparable) CalcUtil.convert(toAdd, bestClass); if (bestClass != newMin.getClass()) newMin = (Comparable) CalcUtil.convert(newMin, bestClass); } if (newMin.compareTo(toAdd) > 0) { newMin = toAdd; } return new MinResult(newMin); } else { throw new IllegalArgumentException( "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented."); } } }