Created
June 17, 2019 03:34
-
-
Save BallerIndustries/a82658597c206a98b29b619b59501fa3 to your computer and use it in GitHub Desktop.
Ajajha
This file contains hidden or 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
package com.dkc.pp.util; | |
import android.support.annotation.NonNull; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
public class BooleanPredicateProcessor { | |
private boolean isNumeric(String text) { | |
for (int i = 0; i < text.length(); i++) { | |
char c = text.charAt(i); | |
if (c < '0' || c > '9') { | |
return false; | |
} | |
} | |
return true; | |
} | |
String[] processArithmetic(String[] pieces) { | |
for (int i = 2; i < pieces.length; i++) { | |
if (pieces[i-1].equals("+")) { | |
int left = Integer.parseInt(pieces[i-2]); | |
int right = Integer.parseInt(pieces[i]); | |
pieces[i] = Integer.toString(left + right); | |
pieces[i-1] = null; | |
pieces[i-2] = null; | |
} | |
else if (pieces[i-1].equals("-")) { | |
int left = Integer.parseInt(pieces[i-2]); | |
int right = Integer.parseInt(pieces[i]); | |
pieces[i] = Integer.toString(left - right); | |
pieces[i-1] = null; | |
pieces[i-2] = null; | |
} | |
} | |
List<String> withoutNulls = new ArrayList<>(pieces.length); | |
for (String piece : pieces) { | |
if (piece != null) withoutNulls.add(piece); | |
} | |
return withoutNulls.toArray(new String[0]); | |
} | |
public boolean processPredicate(String predicate, Map<String, Integer> variables) { | |
String[] pieces = resolveVariables(predicate, variables); | |
pieces = processArithmetic(pieces); | |
String operation = pieces[1]; | |
int leftOperand = Integer.parseInt(pieces[0]); | |
int rightOperand = Integer.parseInt(pieces[2]); | |
switch (operation) { | |
case "<": | |
return leftOperand < rightOperand; | |
case ">": | |
return leftOperand > rightOperand; | |
case ">=": | |
return leftOperand >= rightOperand; | |
case "<=": | |
return leftOperand <= rightOperand; | |
case "==": | |
return leftOperand == rightOperand; | |
default: | |
throw new RuntimeException(String.format("Listen you mother fucker, I'm not interested in that operation. Take your %s and fuck off!", operation)); | |
} | |
} | |
@NonNull | |
private String[] resolveVariables(String predicate, Map<String, Integer> variables) { | |
String[] pieces = predicate.split(" "); | |
for (int i = 0; i < pieces.length; i++) { | |
if (variables.containsKey(pieces[i])) { | |
pieces[i] = variables.get(pieces[i]).toString(); | |
} | |
else if (!isNumeric(pieces[i]) && !isAnOperator(pieces[i])) { | |
throw new RuntimeException(String.format("Unable to resolve variable for string = '%s'", pieces[i])); | |
} | |
} | |
return pieces; | |
} | |
private boolean isAnOperator(String piece) { | |
switch (piece) { | |
case "<": | |
case ">": | |
case ">=": | |
case "<=": | |
case "==": | |
case "+": | |
case "-": | |
return true; | |
default: | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment