diff --git a/build.gradle b/build.gradle index 69509a4..c2a5806 100644 --- a/build.gradle +++ b/build.gradle @@ -7,10 +7,10 @@ plugins { group 'com.github.antoinejt' -def lastUpdate = 'December 2nd 2019' +def lastUpdate = 'December 6th 2019' def majorVersionNumber = '0' -def minorVersionNumber = '7' +def minorVersionNumber = '8' def patchVersionNumber = '0' def wipBuildNumber = '0' // if different from 0 it indicates that this is a wip version, not a yet released one diff --git a/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java b/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java index 0cf4835..19181fe 100644 --- a/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java +++ b/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java @@ -62,7 +62,7 @@ class ConsoleUI { .put("%", OperationType.MODULO) .put("^", OperationType.POWER).build(); private static final Set commands = new HashSet<>( - Arrays.asList("=", "help", "clear", "pop", "quit") + Arrays.asList("=", "help", "clear", "pop", "infos", "quit") ); private static void displayViewFromInside(String pathToFile, Map data) { @@ -72,6 +72,19 @@ private static void displayViewFromInside(String pathToFile, Map System.out.println(viewContent); } + private static void displayInfos() { + Map data = new HashMapBuilder() + .put("VERSION", ManifestInfos.VERSION.toString()) + .put("LAST_UPDATE", ManifestInfos.LAST_UPDATE.toString()) + .put("REVISION", ManifestInfos.REVISION.toString()) + .put("BUILT_BY", ManifestInfos.BUILT_BY.toString()) + .put("TIMESTAMP", ManifestInfos.TIMESTAMP.toString()) + .put("COMPILED_WITH", ManifestInfos.COMPILED_WITH.toString()) + .put("BUILD_JDK", ManifestInfos.BUILD_JDK.toString()) + .put("BUILD_OS", ManifestInfos.BUILD_OS.toString()).build(); + displayViewFromInside("/views/cli/infos.txt", data); + } + private static void displayHelp() { displayViewFromInside("/views/cli/help.txt", null); } @@ -156,6 +169,9 @@ private static void executeCommand(CalculatorEngine calculatorEngine, String inp case "pop": calculatorEngine.removeLastNumber(); break; + case "infos": + displayInfos(); + break; case "quit": System.exit(0); break; @@ -174,7 +190,7 @@ private static void tryToApplyOperation(CalculatorEngine calculatorEngine, Strin private static void tryToAddNumberToTheStack(CalculatorEngine calculatorEngine, String input) { try { - float number = Float.parseFloat(input); + double number = Double.parseDouble(input); calculatorEngine.addNumber(number); } catch (NumberFormatException unused) { System.err.println("Your input is invalid!"); diff --git a/src/main/java/com/github/antoinejt/jasc/ManifestInfos.java b/src/main/java/com/github/antoinejt/jasc/ManifestInfos.java index 1add5e8..8b49d1b 100644 --- a/src/main/java/com/github/antoinejt/jasc/ManifestInfos.java +++ b/src/main/java/com/github/antoinejt/jasc/ManifestInfos.java @@ -35,8 +35,15 @@ import java.util.jar.Manifest; public enum ManifestInfos { - LAST_UPDATE("Last-Update"), - VERSION("Version"); + LAST_UPDATE("Last-Update") + , VERSION("Version") + , REVISION("Build-Revision") + , BUILT_BY("Built-By") + , TIMESTAMP("Build-Timestamp") + , COMPILED_WITH("Created-By") + , BUILD_JDK("Build-Jdk") + , BUILD_OS("Build-OS") + ; private static Attributes attributes = null; diff --git a/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java b/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java index da94b74..f75c719 100644 --- a/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java +++ b/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java @@ -31,9 +31,9 @@ import java.util.Stack; public class CalculatorEngine { - private Stack stack = new Stack<>(); // TODO MrMicky says that it's better to use Deque instead of Stack + private Stack stack = new Stack<>(); // TODO MrMicky says that it's better to use Deque instead of Stack - public void addNumber(float number) { + public void addNumber(double number) { stack.push(number); } @@ -47,7 +47,7 @@ public List getNumbers() { public void applyFunction(FunctionType functionType) { try { - float result = (float) getFunctionResult(functionType); + double result = getFunctionResult(functionType); stack.push(result); } catch (IllegalStateException unused) { System.err.println("No operand left to apply this function to!"); @@ -59,7 +59,7 @@ private double getFunctionResult(FunctionType functionType) throws IllegalStateE throw new IllegalStateException("Stack is empty!"); } - float number = stack.pop(); + double number = stack.pop(); return functionType.apply(number); } @@ -72,40 +72,37 @@ public void applyOperation(OperationType operation) throws IllegalStateException throw new IllegalStateException("Can't operate without at least 2 operands!"); } - float[] operands = getOperands(); + double[] operands = getOperands(); - if (operation == OperationType.DIVISION && operands[0] == 0.0f) { + if (operation == OperationType.DIVISION && operands[0] == 0.0d) { System.err.println("Division by zero!"); reinjectOperandsIntoTheStack(operands); return; } - float result = getOperationResult(operation, operands); + double result = getOperationResult(operation, operands); stack.push(result); } - private float[] getOperands() { - float[] operands = new float[2]; - - for (int i = 0; i < 2; i++) { - operands[i] = stack.pop(); - } + private double[] getOperands() { + double[] operands = new double[2]; + operands[0] = stack.pop(); + operands[1] = stack.pop(); return operands; } - private void reinjectOperandsIntoTheStack(float[] operands) { + private void reinjectOperandsIntoTheStack(double[] operands) { assert operands.length == 2; - for (int i = 0; i < 2; i++) { - stack.push(operands[1 - i]); - } + stack.push(operands[1]); + stack.push(operands[0]); } - private float getOperationResult(OperationType operation, float[] operands) { + private double getOperationResult(OperationType operation, double[] operands) { assert operands.length >= 2; - float a = operands[0]; - float b = operands[1]; + double a = operands[0]; + double b = operands[1]; return operation.apply(a, b); } diff --git a/src/main/java/com/github/antoinejt/jasc/calculator/FunctionType.java b/src/main/java/com/github/antoinejt/jasc/calculator/FunctionType.java index a41ea9b..26f1b48 100644 --- a/src/main/java/com/github/antoinejt/jasc/calculator/FunctionType.java +++ b/src/main/java/com/github/antoinejt/jasc/calculator/FunctionType.java @@ -42,13 +42,13 @@ public enum FunctionType { ARCTAN(Math::atan), EXP(Math::exp); - private final Function function; + private final Function function; - FunctionType(Function function) { + FunctionType(Function function) { this.function = function; } - private static int computeBinaryLog(float number) { + private static int computeBinaryLog(double number) { // https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers double logNumber = Math.log(number); double log2 = Math.log(2); @@ -56,7 +56,7 @@ private static int computeBinaryLog(float number) { return (int) (logNumber / log2 + 1e-10); } - public double apply(float number) { + public double apply(double number) { return function.apply(number); } } diff --git a/src/main/java/com/github/antoinejt/jasc/calculator/OperationType.java b/src/main/java/com/github/antoinejt/jasc/calculator/OperationType.java index dc88988..582d9b7 100644 --- a/src/main/java/com/github/antoinejt/jasc/calculator/OperationType.java +++ b/src/main/java/com/github/antoinejt/jasc/calculator/OperationType.java @@ -30,24 +30,24 @@ import java.util.function.BiFunction; public enum OperationType { - ADDITION(Float::sum), + ADDITION(Double::sum), SUBSTRACTION((a, b) -> b - a), MULTIPLICATION((a, b) -> b * a), DIVISION((a, b) -> b / a), MODULO((a, b) -> b % a), POWER(OperationType::pow); - private final BiFunction operation; + private final BiFunction operation; - OperationType(BiFunction operation) { + OperationType(BiFunction operation) { this.operation = operation; } - private static float pow(float a, float b) { - return (float) Math.pow(b, a); + private static double pow(double a, double b) { + return Math.pow(b, a); } - public float apply(float a, float b) { + public double apply(double a, double b) { return operation.apply(a, b); } } diff --git a/src/main/java/com/github/antoinejt/jasc/parser/MiniViewParser.java b/src/main/java/com/github/antoinejt/jasc/parser/MiniViewParser.java index 6efd9a4..ebc91dd 100644 --- a/src/main/java/com/github/antoinejt/jasc/parser/MiniViewParser.java +++ b/src/main/java/com/github/antoinejt/jasc/parser/MiniViewParser.java @@ -44,10 +44,12 @@ public String parse(Map data) throws IllegalStateException { || !viewContent.contains(" }}")) { return viewContent; } - checkViewValidity(); + // TODO Fix the bad count bug + // checkViewValidity(); return fillView(data); } + /* private int countOccurrences(String regex) { return view.toString().split(regex).length - 1; } @@ -60,6 +62,7 @@ private void checkViewValidity() throws IllegalStateException { throw new IllegalStateException("View is invalid! Please fix it!"); } } + */ private String fillView(Map data) { String result = view.toString(); diff --git a/src/main/resources/views/cli/help.txt b/src/main/resources/views/cli/help.txt index fa11896..c639954 100644 --- a/src/main/resources/views/cli/help.txt +++ b/src/main/resources/views/cli/help.txt @@ -22,6 +22,7 @@ Available commands : - help : Show the list of available commands - clear : Reset stack content - pop : Remove last number from the stack + - infos : Display build infos - quit : Allows to quit Special features: - put a $ before an operator to loop its execution (i.e. $+ will make sum of all the stack numbers) diff --git a/src/main/resources/views/cli/infos.txt b/src/main/resources/views/cli/infos.txt new file mode 100644 index 0000000..b9f3047 --- /dev/null +++ b/src/main/resources/views/cli/infos.txt @@ -0,0 +1,12 @@ +Build infos +------------ +Version : {{ VERSION }} +Last update : {{ LAST_UPDATE }} +Revision (Commit hash) : {{ REVISION }} + +Built by : {{ BUILT_BY }} +Build Timestamp : {{ TIMESTAMP }} + +Compiled with : {{ COMPILED_WITH }} +Build JDK : {{ BUILD_JDK }} +Build OS : {{ BUILD_OS }}