-
Notifications
You must be signed in to change notification settings - Fork 170
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
Add root
and log
filters.
#299
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92ec11b
Add root and log filters.
f0ebc92
Merge branch 'master' into root-log-filters
4fdbd26
tests
464d3e2
better math:
5fece8b
Add more edge case tests.
25c7754
update docs.
de8408f
fix docs.
021c9a5
update documentation.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add root and log filters.
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.math.MathContext; | ||
|
||
import com.google.common.primitives.Doubles; | ||
import com.hubspot.jinjava.interpret.InterpretException; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
|
||
import ch.obermuhlner.math.big.BigDecimalMath; | ||
|
||
public class LogFilter implements Filter { | ||
|
||
private static final MathContext PRECISION = new MathContext(100); | ||
|
||
@Override | ||
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { | ||
|
||
// default to e | ||
Double base = null; | ||
if (args.length > 0 && args[0] != null) { | ||
base = Doubles.tryParse(args[0]); | ||
} | ||
|
||
if (object instanceof Integer) { | ||
if (base == null) { | ||
return Math.log((Integer) object); | ||
} | ||
return calculateLog((Integer) object, base); | ||
} | ||
if (object instanceof Float) { | ||
if (base == null) { | ||
return Math.log((Float) object); | ||
} | ||
return calculateLog((Float) object, base); | ||
} | ||
if (object instanceof Long) { | ||
if (base == null) { | ||
return Math.log((Long) object); | ||
} | ||
return calculateLog((Long) object, base); | ||
} | ||
if (object instanceof Short) { | ||
if (base == null) { | ||
return Math.log((Short) object); | ||
} | ||
return calculateLog((Short) object, base); | ||
} | ||
if (object instanceof Double) { | ||
if (base == null) { | ||
return Math.log((Double) object); | ||
} | ||
return calculateLog((Double) object, base); | ||
} | ||
if (object instanceof BigDecimal) { | ||
if (base == null) { | ||
return BigDecimalMath.log((BigDecimal) object, PRECISION); | ||
} | ||
return calculateLog((BigDecimal) object, new BigDecimal(base)); | ||
} | ||
if (object instanceof BigInteger) { | ||
if (base == null) { | ||
return BigDecimalMath.log(new BigDecimal((BigInteger) object), PRECISION); | ||
} | ||
return calculateLog(new BigDecimal((BigInteger) object), new BigDecimal(base)); | ||
} | ||
if (object instanceof Byte) { | ||
return calculateLog((Byte) object, base); | ||
} | ||
if (object instanceof String) { | ||
try { | ||
if (base == null) { | ||
return BigDecimalMath.log(new BigDecimal((String) object), PRECISION); | ||
} else { | ||
return calculateLog(new BigDecimal((String) object), new BigDecimal(base)); | ||
} | ||
} catch (Exception e) { | ||
throw new InterpretException(object + " can't be handled by log filter", e); | ||
} | ||
} | ||
|
||
return object; | ||
} | ||
|
||
private double calculateLog(double num, double base) { | ||
return Math.log(num) / Math.log(base); | ||
} | ||
|
||
private BigDecimal calculateLog(BigDecimal num, BigDecimal base) { | ||
return BigDecimalMath.log(num, PRECISION).divide(BigDecimalMath.log(base, PRECISION)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "log"; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.math.MathContext; | ||
|
||
import com.google.common.primitives.Doubles; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.InterpretException; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
|
||
import ch.obermuhlner.math.big.BigDecimalMath; | ||
|
||
@JinjavaDoc( | ||
value = "Return the absolute value of the argument.", | ||
params = { | ||
@JinjavaParam(value = "number", type = "number", desc = "The number that you want to get the absolute value of") | ||
}, | ||
snippets = { | ||
@JinjavaSnippet( | ||
code = "{% set my_number = -53 %}\n" + | ||
"{{ my_number|abs }}") | ||
}) | ||
public class RootFilter implements Filter { | ||
|
||
private static final MathContext PRECISION = new MathContext(100); | ||
|
||
@Override | ||
public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { | ||
|
||
double root = 2; | ||
if (args.length > 0 && args[0] != null) { | ||
Double tryRoot = Doubles.tryParse(args[0]); | ||
if (tryRoot != null) { | ||
root = tryRoot; | ||
} | ||
} | ||
|
||
if (object instanceof Integer) { | ||
return calculateRoot((Integer) object, root); | ||
} | ||
if (object instanceof Float) { | ||
return calculateRoot((Float) object, root); | ||
} | ||
if (object instanceof Long) { | ||
return calculateRoot((Long) object, root); | ||
} | ||
if (object instanceof Short) { | ||
return calculateRoot((Short) object, root); | ||
} | ||
if (object instanceof Double) { | ||
return calculateRoot((Double) object, root); | ||
} | ||
if (object instanceof BigDecimal) { | ||
if (root == 2) { | ||
return BigDecimalMath.sqrt((BigDecimal) object, PRECISION); | ||
} | ||
return BigDecimalMath.root((BigDecimal) object, new BigDecimal(root), PRECISION); | ||
} | ||
if (object instanceof BigInteger) { | ||
if (root == 2) { | ||
return BigDecimalMath.sqrt(new BigDecimal((BigInteger) object), PRECISION); | ||
} | ||
return BigDecimalMath.root(new BigDecimal((BigInteger) object), new BigDecimal(root), PRECISION); | ||
} | ||
if (object instanceof Byte) { | ||
return calculateRoot((Byte) object, root); | ||
} | ||
if (object instanceof String) { | ||
try { | ||
if (root == 2) { | ||
return BigDecimalMath.sqrt(new BigDecimal((String) object), PRECISION); | ||
} else { | ||
return BigDecimalMath.root(new BigDecimal((String) object), new BigDecimal(root), PRECISION); | ||
} | ||
} catch (Exception e) { | ||
throw new InterpretException(object + " can't be handled by root filter", e); | ||
} | ||
} | ||
|
||
return object; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "root"; | ||
} | ||
|
||
private double calculateRoot(double base, double n) { | ||
return Math.pow(Math.E, Math.log(base)/n); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not thrilled about the very generic name of this, but it makes sense to be compatible with ansible.