Skip to content

Instantly share code, notes, and snippets.

@sangkyoonnam
Created April 16, 2021 11:17
Show Gist options
  • Save sangkyoonnam/f45790a65cb9706762bbb864a0d3f2ac to your computer and use it in GitHub Desktop.
Save sangkyoonnam/f45790a65cb9706762bbb864a0d3f2ac to your computer and use it in GitHub Desktop.
패키지 타겟팅 처리
package co.adison.offerwall.utils;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
public class ExpressionUtils {
public static double eval(String expression) {
Expression e = new ExpressionBuilder(expression)
.operator(LogicOperators.getOperators())
.build();
return e.evaluate();
}
}
public fun isPassedTargetPackages(): Boolean {
var passed = true
filter?.targetPackages?.let {
if (!it.isNullOrBlank()) {
var exp = it
val p = Pattern.compile("\\{[a-zA-Z0-9_]+([.a-zA-Z0-9_])*\\}")
val m = p.matcher(exp)
try {
while (m.find()) {
val raw = m.group()
val packageName = raw.replace("{", "").replace("}", "")
val value = if (isInstalled(packageName)) { "1.0" } else { "0.0" }
exp = exp.replace(raw, value)
}
passed = ExpressionUtils.eval(exp) === 1.0
} catch (e: Exception) {
passed = false
}
}
}
return passed
}
package co.adison.offerwall.utils;
import net.objecthunter.exp4j.operator.Operator;
public class LogicOperators {
private static final int INDEX_LESS_THAN = 0;
private static final int INDEX_LESS_THAN_OR_EQUAL_TO = 1;
private static final int INDEX_GREATER_THAN = 2;
private static final int INDEX_GREATER_THAN_OR_EQUAL_TO = 3;
private static final int INDEX_EXACTLY_EQUAL_TO = 4;
private static final int INDEX_NOT_EQUAL_TO = 5;
private static final int INDEX_NOT_X = 6;
private static final int INDEX_X_OR_Y = 7;
private static final int INDEX_X_AND_Y = 8;
private static final double TRUE = 1.0d;
private static final double FALSE = 0.0d;
private static final Operator[] operators = new Operator[9];
static {
operators[INDEX_LESS_THAN] = new Operator("<", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] < args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_LESS_THAN_OR_EQUAL_TO] = new Operator("<=", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] <= args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_GREATER_THAN] = new Operator(">", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] > args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_GREATER_THAN_OR_EQUAL_TO] = new Operator(">=", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] >= args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_EXACTLY_EQUAL_TO] = new Operator("==", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] == args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_NOT_EQUAL_TO] = new Operator("!=", 2, true, LogicOperator.PRECEDENCE_LOGIC_THAN) {
@Override
public double apply(final double... args) {
if (args[0] != args[1]) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_NOT_X] = new Operator("!", 1, true, LogicOperator.PRECEDENCE_LOGIC_NOT) {
@Override
public double apply(final double... args) {
if (args[0] == 0) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_X_OR_Y] = new Operator("||", 2, true, LogicOperator.PRECEDENCE_LOGIC_AND_OR) {
@Override
public double apply(final double... args) {
if (args[0] != FALSE || args[1] != FALSE) {
return TRUE;
} else {
return FALSE;
}
}
};
operators[INDEX_X_AND_Y] = new Operator("&&", 2, true, LogicOperator.PRECEDENCE_LOGIC_AND_OR) {
@Override
public double apply(final double... args) {
if (args[0] != 0 && args[1] != 0) {
return TRUE;
} else {
return FALSE;
}
}
};
}
public static Operator[] getOperators() {
return operators;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment