Skip to content

Commit

Permalink
Update to v0.5.0 : merge pull request #11 from AntoineJT/develop
Browse files Browse the repository at this point in the history
Big release : v0.5.0
  • Loading branch information
AntoineJT authored Oct 11, 2019
2 parents e300f3b + 8b04783 commit 5e3d352
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 190 deletions.
26 changes: 19 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@ plugins {
}

group 'com.github.antoinejt'
version '0.4.0'

def lastUpdate = 'October 10th 2019'

def majorVersionNumber = '0'
def minorVersionNumber = '5'
def patchVersionNumber = '0'

def versionNumber = "${majorVersionNumber}.${minorVersionNumber}.${patchVersionNumber}"
def internalVersionNumber = "${versionNumber}${versioning.info.branch != 'master' ? '-DEVELOP' : ''}"

version "${versionNumber}-${versioning.info.full}"

sourceCompatibility = 1.8

jar {
manifest {
attributes(
'Main-Class' : 'com.github.antoinejt.jasc.Main',
'Built-By' : System.properties['user.name'],
'Main-Class': 'com.github.antoinejt.jasc.Main',
'Built-By': System.properties['user.name'],
'Last-Update': lastUpdate,
'Version': internalVersionNumber,
'Build-Timestamp': new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
'Build-Revision' : versioning.info.commit,
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
'Build-Revision': versioning.info.commit,
'Created-By': "Gradle ${gradle.gradleVersion}",
'Build-Jdk': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS': "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.github.antoinejt.jasc.calculator.CalculatorEngine;
import com.github.antoinejt.jasc.calculator.FunctionType;
import com.github.antoinejt.jasc.calculator.OperandException;
import com.github.antoinejt.jasc.calculator.OperationType;
import com.github.antoinejt.jasc.util.TextFormatter;

import java.util.*;

final class ConsoleUI {
class ConsoleUI {
private static final Map<String, FunctionType> functions = Collections.unmodifiableMap(new HashMap<String, FunctionType>() {{
put("sqrt", FunctionType.SQRT);
put("log", FunctionType.LOG10);
Expand Down Expand Up @@ -72,8 +71,8 @@ private static void displayIntro() {
"Created by Antoine James Tournepiche",
"Repository link : https://github.com/AntoineJT/jasc",
"-----------------------------------------------------",
"Version : " + Constants.VERSION,
"Last update : " + Constants.LAST_UPDATE,
"Version : " + ManifestInfos.VERSION,
"Last update : " + ManifestInfos.LAST_UPDATE,
"--------------------------------",
"This calculator uses a stack, so you must define at least 2 numbers before using some calculation operator",
"You must type numbers with or without a dot, not a comma",
Expand Down Expand Up @@ -115,8 +114,8 @@ static void useConsole() throws Exception {
try {
OperationType operationType = operators.get(input);

calculatorEngine.operate(operationType);
} catch (OperandException unused) {
calculatorEngine.applyOperation(operationType);
} catch (IllegalStateException unused) {
System.err.println("You need to specify at least 2 operands before you can make some calculation!");
}
continue;
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/github/antoinejt/jasc/Constants.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/com/github/antoinejt/jasc/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.antoinejt.jasc;

public class Main {
public static void main(String[] args){
public static void main(String[] args) {
try {
ConsoleUI.useConsole();
} catch (Exception exception) {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/github/antoinejt/jasc/ManifestInfos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.antoinejt.jasc;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public enum ManifestInfos {
LAST_UPDATE("Last-Update"),
VERSION("Version");

private final String value;

ManifestInfos(String name) {
Attributes attributes = null;
try {
attributes = getAttributesFromManifest();
} catch (IOException ignored) {
}
value = (attributes != null) ? attributes.getValue(name) : null;
}

private Attributes getAttributesFromManifest() throws IOException {
URL resource = getClass().getClassLoader().getResource("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(Objects.requireNonNull(resource, "MANIFEST.MF doesn't exists!").openStream());
return manifest.getMainAttributes();
}

@Override
public String toString() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -1,115 +1,54 @@
package com.github.antoinejt.jasc.calculator;

import com.github.antoinejt.jasc.util.ReflectUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

public final class CalculatorEngine {
private final Stack<Float> stack = new Stack<>();
private static final Map<FunctionType, Function<Float, Double>> computatingFunctions = Collections.unmodifiableMap(new HashMap<FunctionType, Function<Float, Double>>() {
{
put(FunctionType.SQRT, Math::sqrt);
put(FunctionType.LOG10, Math::log10);
put(FunctionType.LN, Math::log);
put(FunctionType.LOGB, number -> (double) computeBinaryLog(number));
put(FunctionType.COS, Math::cos);
put(FunctionType.SIN, Math::sin);
put(FunctionType.TAN, Math::tan);
put(FunctionType.ARCCOS, Math::acos);
put(FunctionType.ARCSIN, Math::asin);
put(FunctionType.ARCTAN, Math::atan);
put(FunctionType.EXP, Math::exp);
}

private int computeBinaryLog(float 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);
import java.util.Stack;

return (int) (logNumber / log2 + 1e-10);
}
});
private static final Map<OperationType, BiFunction<Float, Float, Float>> operationFunctions = Collections.unmodifiableMap(new HashMap<OperationType, BiFunction<Float, Float, Float>>() {
{
put(OperationType.ADDITION, Float::sum);
put(OperationType.SUBSTRACTION, (a, b) -> b - a);
put(OperationType.MULTIPLICATION, (a, b) -> b * a);
put(OperationType.DIVISION, (a, b) -> b / a);
put(OperationType.MODULO, (a, b) -> b % a);
put(OperationType.POWER, this::pow);
}

private float pow(float a, float b) {
return (float) Math.pow(b, a);
}
});
public class CalculatorEngine {
private Stack<Float> stack = new Stack<>(); // TODO MrMicky says that it's better to use Deque instead of Stack

public void addNumber(float number) {
stack.push(number);
}

public void clear() {
stack.clear();
stack = new Stack<>();
}

public List getNumbers() {
List stackContent = null;

try {
stackContent = (List) ReflectUtil.getPrivateField(stack, "stack");
} catch (IllegalAccessException | NoSuchFieldException exception) {
exception.printStackTrace();
}
return stackContent;
return stack;
}

private float[] getOperands() {
float[] operands = new float[2];
public void applyFunction(FunctionType functionType) {
try {
float result = (float) getFunctionResult(functionType);

for (int i = 0; i < 2; i++) {
operands[i] = stack.pop();
stack.push(result);
} catch (IllegalStateException unused) {
System.err.println("No operand left to apply this function to!");
}
return operands;
}

private double getFunctionResult(FunctionType functionType) throws OperandException, CalculatorException {
int stackSize = stack.getSize();

if (stackSize > 0) {
float number = stack.pop();
private double getFunctionResult(FunctionType functionType) throws IllegalStateException {
int stackSize = stack.size();

if (!computatingFunctions.containsKey(functionType)){
throw new CalculatorException("The provided function is not handled!");
}
return computatingFunctions.get(functionType).apply(number);
if (stackSize == 0) {
throw new IllegalStateException("Stack is empty!");
}
throw new OperandException("Stack is empty!");
}

public void applyFunction(FunctionType functionType) throws CalculatorException {
try {
float result = (float) getFunctionResult(functionType);
float number = stack.pop();

stack.push(result);
} catch (OperandException unused) {
System.err.println("No operand left to apply this function to!");
}
return functionType.apply(number);
}

public void removeLastNumber(){
public void removeLastNumber() {
stack.pop();
}

public void operate(OperationType operation) throws OperandException, CalculatorException {
int stackSize = stack.getSize();
public void applyOperation(OperationType operation) throws IllegalStateException {
int stackSize = stack.size();

if (stackSize < 2) {
throw new OperandException("Can't operate without at least 2 operands!");
throw new IllegalStateException("Can't operate without at least 2 operands!");
}

float[] operands = getOperands();
Expand All @@ -120,25 +59,32 @@ public void operate(OperationType operation) throws OperandException, Calculator
return;
}

float result = computate(operation, operands);
float 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();
}
return operands;
}

private void reinjectOperandsIntoTheStack(float[] operands) {
// assert operands.length == 2;
assert operands.length == 2;
for (int i = 0; i < 2; i++) {
stack.push(operands[1 - i]);
}
}

private float computate(OperationType operation, float[] operands) throws CalculatorException {
private float getOperationResult(OperationType operation, float[] operands) {
assert operands.length >= 2;
float a = operands[0];
float b = operands[1];

if (!operationFunctions.containsKey(operation)) {
throw new CalculatorException("The provided operation is not handled!");
}
return operationFunctions.get(operation).apply(a, b);
return operation.apply(a, b);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
package com.github.antoinejt.jasc.calculator;

import java.util.function.Function;

public enum FunctionType {
SQRT,
LOG10,
LN,
LOGB,
COS,
SIN,
TAN,
ARCCOS,
ARCSIN,
ARCTAN,
EXP
SQRT(Math::sqrt),
LOG10(Math::log10),
LN(Math::log),
LOGB(number -> (double) computeBinaryLog(number)),
COS(Math::cos),
SIN(Math::sin),
TAN(Math::tan),
ARCCOS(Math::acos),
ARCSIN(Math::asin),
ARCTAN(Math::atan),
EXP(Math::exp);

private final Function<Float, Double> function;

FunctionType(Function<Float, Double> function) {
this.function = function;
}

private static int computeBinaryLog(float 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);

return (int) (logNumber / log2 + 1e-10);
}

public double apply(float number) {
return function.apply(number);
}
}

This file was deleted.

Loading

0 comments on commit 5e3d352

Please sign in to comment.