From 79cdc60ce29602484a64bcad1cf59ec6da6db675 Mon Sep 17 00:00:00 2001 From: Antoine James Tournepiche Date: Fri, 6 Dec 2019 18:44:01 +0100 Subject: [PATCH 1/4] :sparkles: Increase calculator numbers capacity (from float to double) (#16) --- build.gradle | 6 ++--- .../com/github/antoinejt/jasc/ConsoleUI.java | 2 +- .../jasc/calculator/CalculatorEngine.java | 26 +++++++++---------- .../jasc/calculator/FunctionType.java | 8 +++--- .../jasc/calculator/OperationType.java | 12 ++++----- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/build.gradle b/build.gradle index 69509a4..1dc446e 100644 --- a/build.gradle +++ b/build.gradle @@ -7,12 +7,12 @@ 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 +def wipBuildNumber = '1' // if different from 0 it indicates that this is a wip version, not a yet released one def buildNumber = (wipBuildNumber != '0') ? "-alpha.${wipBuildNumber}" : '' def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}${buildNumber}" diff --git a/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java b/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java index 0cf4835..5f2444c 100644 --- a/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java +++ b/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java @@ -174,7 +174,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/calculator/CalculatorEngine.java b/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java index da94b74..43cdd9e 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,20 +72,20 @@ 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]; + private double[] getOperands() { + double[] operands = new double[2]; for (int i = 0; i < 2; i++) { operands[i] = stack.pop(); @@ -93,7 +93,7 @@ private float[] getOperands() { return operands; } - private void reinjectOperandsIntoTheStack(float[] operands) { + private void reinjectOperandsIntoTheStack(double[] operands) { assert operands.length == 2; for (int i = 0; i < 2; i++) { @@ -101,11 +101,11 @@ private void reinjectOperandsIntoTheStack(float[] operands) { } } - 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); } } From 9c5eaa1ae3ac0593a696b6c9778c067883150596 Mon Sep 17 00:00:00 2001 From: Antoine James Tournepiche Date: Fri, 6 Dec 2019 18:51:53 +0100 Subject: [PATCH 2/4] :recycle: Increase verbosity by removing some really short for loops Removes 2 for loops which loop just once (2 values) --- build.gradle | 2 +- .../antoinejt/jasc/calculator/CalculatorEngine.java | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 1dc446e..48098da 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ def lastUpdate = 'December 6th 2019' def majorVersionNumber = '0' def minorVersionNumber = '8' def patchVersionNumber = '0' -def wipBuildNumber = '1' // if different from 0 it indicates that this is a wip version, not a yet released one +def wipBuildNumber = '2' // if different from 0 it indicates that this is a wip version, not a yet released one def buildNumber = (wipBuildNumber != '0') ? "-alpha.${wipBuildNumber}" : '' def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}${buildNumber}" 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 43cdd9e..f75c719 100644 --- a/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java +++ b/src/main/java/com/github/antoinejt/jasc/calculator/CalculatorEngine.java @@ -86,19 +86,16 @@ public void applyOperation(OperationType operation) throws IllegalStateException private double[] getOperands() { double[] operands = new double[2]; - - for (int i = 0; i < 2; i++) { - operands[i] = stack.pop(); - } + operands[0] = stack.pop(); + operands[1] = stack.pop(); return 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 double getOperationResult(OperationType operation, double[] operands) { From 488a38659b639940232f9a904a81cdae021663af Mon Sep 17 00:00:00 2001 From: Antoine James Tournepiche Date: Sat, 7 Dec 2019 00:01:43 +0100 Subject: [PATCH 3/4] :sparkles: Add a build infos command (#12) N.B. : Found a bug when checking view validity, then disable the view checker. This bug needs to be fixed --- build.gradle | 2 +- .../com/github/antoinejt/jasc/ConsoleUI.java | 18 +++++++++++++++++- .../github/antoinejt/jasc/ManifestInfos.java | 11 +++++++++-- .../antoinejt/jasc/parser/MiniViewParser.java | 5 ++++- src/main/resources/views/cli/help.txt | 1 + src/main/resources/views/cli/infos.txt | 12 ++++++++++++ 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/main/resources/views/cli/infos.txt diff --git a/build.gradle b/build.gradle index 48098da..ccd1f0e 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ def lastUpdate = 'December 6th 2019' def majorVersionNumber = '0' def minorVersionNumber = '8' def patchVersionNumber = '0' -def wipBuildNumber = '2' // if different from 0 it indicates that this is a wip version, not a yet released one +def wipBuildNumber = '3' // if different from 0 it indicates that this is a wip version, not a yet released one def buildNumber = (wipBuildNumber != '0') ? "-alpha.${wipBuildNumber}" : '' def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}${buildNumber}" diff --git a/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java b/src/main/java/com/github/antoinejt/jasc/ConsoleUI.java index 5f2444c..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; 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/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 }} From eb4c747d8e5957cd9da85d04a16f6b201a571fa2 Mon Sep 17 00:00:00 2001 From: Antoine James Tournepiche Date: Fri, 2 Oct 2020 16:15:02 +0200 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=96=20Release=200.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index ccd1f0e..c2a5806 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ def lastUpdate = 'December 6th 2019' def majorVersionNumber = '0' def minorVersionNumber = '8' def patchVersionNumber = '0' -def wipBuildNumber = '3' // if different from 0 it indicates that this is a wip version, not a yet released one +def wipBuildNumber = '0' // if different from 0 it indicates that this is a wip version, not a yet released one def buildNumber = (wipBuildNumber != '0') ? "-alpha.${wipBuildNumber}" : '' def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}${buildNumber}"