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

Add root and log filters. #299

Merged
merged 8 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add root and log filters.
  • Loading branch information
Matt Coley committed Mar 4, 2019
commit 92ec11bed50472258be7aeb709595305ccb7e593
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down Expand Up @@ -130,6 +134,11 @@
<artifactId>re2j</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>ch.obermuhlner</groupId>
<artifactId>big-math</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
98 changes: 98 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/filter/LogFilter.java
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";
Copy link
Contributor

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.

}
}
94 changes: 94 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/filter/RootFilter.java
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);
}
}