Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 941 Add multiplication operation #943

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public abstract class Modifier implements SpecDriven, ContextualTransform {
STOCK_FUNCTIONS.put( "longSubtract", new Math.longSubtract() );
STOCK_FUNCTIONS.put( "divide", new Math.divide() );
STOCK_FUNCTIONS.put( "divideAndRound", new Math.divideAndRound() );

STOCK_FUNCTIONS.put( "multiply", new Math.multiply() );

STOCK_FUNCTIONS.put( "toInteger", new Objects.toInteger() );
STOCK_FUNCTIONS.put( "toDouble", new Objects.toDouble() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,32 @@ public static Optional<Double> divideAndRound(List<Object> argList, int digitsAf

return Optional.empty();
}

public static Optional<Integer> multiply(List<Object> argList) {

if ( argList == null || argList.size() != 2 ) {
return Optional.empty();
}

Optional<? extends Number> numerator = Objects.toNumber(argList.get(0));
Optional<? extends Number> denominator = Objects.toNumber(argList.get(1));

if(numerator.isPresent() && denominator.isPresent()) {

long drLongValue = denominator.get().longValue();
if(drLongValue == 0) {
return Optional.empty();
}

long nrLongValue = numerator.get().longValue();
// long result = nrLongValue * drLongValue;
Copy link
Contributor

@sibasish-palo sibasish-palo Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeshuai007 Please remove this comment

long result = java.lang.Math.multiplyExact(nrLongValue, drLongValue);
return Optional.of((int)result);
}

return Optional.empty();
}


@SuppressWarnings( "unchecked" )
public static final class max extends Function.BaseFunction<Object> {
Expand Down Expand Up @@ -445,4 +471,14 @@ protected Optional<Object> applyList( final List<Object> argLongList ) {
return (Optional) longSubtract(argLongList);
}
}

@SuppressWarnings( "unchecked" )
public static final class multiply extends Function.ListFunction {

@Override
protected Optional<Object> applyList(List<Object> argList) {
return (Optional)multiply(argList);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public Iterator<Object[]> getTestCases() {

Function DIV_OF = new Math.divide();
Function DIV_AND_ROUND_OF = new Math.divideAndRound();

Function MULTIPLY_OF = new Math.multiply();

testCases.add( new Object[] { "max-empty-array", MAX_OF, new Object[] {}, Optional.empty() } );
testCases.add( new Object[] { "max-empty-list", MAX_OF, new ArrayList( ), Optional.empty() } );
Expand Down Expand Up @@ -275,7 +277,9 @@ public Iterator<Object[]> getTestCases() {
testCases.add( new Object[] { "divAndRound-trailing-precision-array", DIV_AND_ROUND_OF, Arrays.asList(3, 5.0, 2), Optional.of(2.500)});
testCases.add( new Object[] { "divAndRound-no-precision-array", DIV_AND_ROUND_OF, Arrays.asList(0, 5.0, 2), Optional.of(3.0)}); // Round up as >= 0.5
testCases.add( new Object[] { "divAndRound-no-precision-array", DIV_AND_ROUND_OF, Arrays.asList(0, 4.8, 2), Optional.of(2.0)}); // Round down as < 0.5


testCases.add( new Object[] { "multiply-combo-array", MULTIPLY_OF, Arrays.asList(30, 2), Optional.of(60)});

return testCases.iterator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"roundedDiv": "=divideAndRound(4, @(1,nr),@(1,dr))", // Round the result to the 4 decimal points
//
"badArgs1" : "=divide(1,2,3)", // too many params
"badArgs2" : "=divide(1)" // not enough params
"badArgs2" : "=divide(1)", // not enough params
"multiply": "=multiply(@(1,nr),@(1,dr))"
},
"data5": {
"happyInt": "=intSubtract(@(1,aInt),@(1,bInt))",
Expand Down Expand Up @@ -114,7 +115,8 @@
"explicit2": 4.0,
"value": [5, 2],
"nr": 51,
"dr": 13
"dr": 13,
"multiply":663
},
"data5": {
// og data
Expand Down