Skip to content

Commit

Permalink
Merge pull request #9 from bartlomiej-gora/#5/refactor
Browse files Browse the repository at this point in the history
#5/refactor: get Rid off old calculator imlementation.
  • Loading branch information
bartgora committed Apr 8, 2016
2 parents 2400fae + 5ed3b5c commit 74053d4
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 541 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ pip-log.txt
#Mr Developer
.mr.developer.cfg

target/
target/
.idea
*.iml
10 changes: 1 addition & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pl.bgora</groupId>
<artifactId>RPNLibrary</artifactId>
<version>2.0.1</version>
<version>3.0.0</version>
<name>RPNLibrary</name>
<description>RPN Libray for Java.</description>
<build>
Expand All @@ -20,14 +20,6 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down
89 changes: 67 additions & 22 deletions src/main/java/pl/bgora/rpn/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.bgora.rpn;

import pl.bgora.rpn.exceptions.RPNException;
import pl.bgora.rpn.exceptions.NoSuchFunctionFound;
import pl.bgora.rpn.exceptions.WrongArgumentException;

import java.math.BigDecimal;
Expand All @@ -26,8 +27,15 @@
import java.util.LinkedList;


/**
* RPN Calculator Implementation with functions.
* <p/>
*
* @author Bartłomiej Góra (bartlomiej.gora@gmail.com)
*/
public class Calculator {


protected RPNChecking checker;

protected RPNExecuting executioner;
Expand Down Expand Up @@ -79,7 +87,7 @@ public static Calculator createDefaultCalculator() {
* @return Calculator Object.
*/
public static Calculator createDefaultCalculator(RoundingMode mode) {
return new FunctionsCalculator(new FunctionsDefaultChecker(), new FunctionsDefaultExecutioner(), mode);
return new Calculator(new DefaultChecker(), new DefaultExecutioner(), mode);
}

/**
Expand All @@ -97,11 +105,12 @@ public static Calculator createDefaultCalculator(RoundingMode mode) {
* @see pl.bgora.rpn.RPNExecuting
*/
public static Calculator createCalculator(RoundingMode mode, RPNChecking checker, RPNExecuting executioner) {
return new FunctionsCalculator(checker, executioner, mode);
return new Calculator(checker, executioner, mode);
}


/**
* Contructor Creates an instance of the class.
* Constructor Creates an instance of the class.
*
* @param checker Object that implementa RPNChecking - Used for checking operations in input.
* @param executioner Object iplementing RPNExecuting - used for executing operations on input.
Expand All @@ -113,20 +122,20 @@ public static Calculator createCalculator(RoundingMode mode, RPNChecking checker
this.roundingMode = mode;
}


/**
* Caluclates the input String.
* Calculates RPN String into BigDecimal Object.
*
* @param input Input string.
* @return value as {@code java.math.BigDecimal}
* @throws WrongArgumentException Thrown if the input is incorrect (Incorrect format, or
* unsupported opertians)
* @throws NoSuchFunctionFound
* @see pl.bgora.rpn.Calculator#calculate(java.lang.String)
*/
public BigDecimal calculate(final String input) throws RPNException {
public BigDecimal calculate(final String input) throws WrongArgumentException, NoSuchFunctionFound {
final String temp = prepareInput(input);
final String result = createRPN(temp);
return getResult(result);
}


/**
* Format input for further processing.
*
Expand All @@ -143,6 +152,7 @@ private String prepareInput(String input) throws WrongArgumentException {
boolean lastWasDigit = false;
boolean lastWasOperator = false;
boolean lastWasWhiteSpace = false;
boolean lastWasLetter = false;
// Iteration throght input String.
for (int i = 0; i < length; i++) {
c = inputValue.charAt(i);
Expand All @@ -154,9 +164,11 @@ private String prepareInput(String input) throws WrongArgumentException {
lastWasDigit = true;
result.append(c);
lastWasWhiteSpace = false;
lastWasLetter = false;
continue;
} else if (Character.isDigit(c)) {
lastWasDigit = true;
lastWasLetter = false;
lastWasOperator = false;
if (!lastWasWhiteSpace) {
result.append(" ");
Expand All @@ -166,6 +178,7 @@ private String prepareInput(String input) throws WrongArgumentException {
continue;
} else if (checker.isOperatorOrBracket(String.valueOf(c))) {
lastWasDigit = false;
lastWasLetter = false;
lastWasOperator = true;
if (!lastWasWhiteSpace) {
result.append(" ");
Expand All @@ -184,6 +197,16 @@ private String prepareInput(String input) throws WrongArgumentException {
lastWasDigit = false;
lastWasOperator = false;
continue;
} else if (Character.isLetter(c)) {
lastWasDigit = false;
lastWasOperator = false;
if (!lastWasLetter && !lastWasWhiteSpace) {
result.append(" ").append(c);
} else {
result.append(c);
}
lastWasWhiteSpace = false;
lastWasLetter = true;
} else {
throw new WrongArgumentException("Element \"" + c + "\" is not recognized by the Checker");
}
Expand Down Expand Up @@ -213,6 +236,15 @@ private String createRPN(String input) throws WrongArgumentException {
if (checker.isDigit(temp)) {
// input String.
result.append(" ").append(temp);
} else if (checker.isFunction(temp)) {
stack.push(temp);
} else if (",".equals(temp)) {
do {
stackOper = stack.pop();
if (!checker.isLeftBracket(stackOper)) {
result.append(" ").append(stackOper);
}
} while (!stack.isEmpty() && !checker.isLeftBracket(stackOper));
} else if (checker.isOperator(temp)) {
// 1) until the top of the stack is an operator, o2 such that:
// O1 is the total or left-total and its sequence
Expand All @@ -221,7 +253,8 @@ private String createRPN(String input) throws WrongArgumentException {
// Is less than o2,
// Remove O2 from the stack and add it to the output queue;
// 2) o1 put on the stack operators.
while (!stack.isEmpty() && checker.isOperator(stackOper = stack.peek())) {
while (!stack.isEmpty() && checker.isOperator(stack.peek())) {
stackOper = stack.peek();
if (checker.isLeftAssociativity(stackOper) && (checker.compareOperators(stackOper, temp) >= 0)) {
stack.pop();
result.append(" ").append(stackOper);
Expand All @@ -245,9 +278,14 @@ private String createRPN(String input) throws WrongArgumentException {
// Left bracket oil (and right too)
do {
temp = stack.pop();
result.append(" ").append(temp);
} while (!checker.isLeftBracket(stack.peek()));
stack.pop();
if (!checker.isLeftBracket(temp)) {
result.append(" ").append(temp);
}
} while (!checker.isLeftBracket(temp));

if (!stack.isEmpty() && checker.isFunction(stack.peek())) {
result.append(" ").append(stack.pop());
}
} else {
// If there is anything that can be recognized ....
throw new WrongArgumentException("Element \"" + temp + "\" is not recognized by the Checker");
Expand All @@ -268,8 +306,9 @@ private String createRPN(String input) throws WrongArgumentException {
* @param result Input RPN String
* @return value as {@code java.math.BigDecimal}
* @throws WrongArgumentException
* @throws NoSuchFunctionFound
*/
private BigDecimal getResult(String result) throws WrongArgumentException {
private BigDecimal getResult(String result) throws WrongArgumentException, NoSuchFunctionFound {
String[] factors = result.trim().split(" ");
Deque<String> stack = new LinkedList<String>();
String temp = null;
Expand All @@ -282,17 +321,23 @@ private BigDecimal getResult(String result) throws WrongArgumentException {
stack.push(temp);
} else if (checker.isOperator(temp)) {
var1 = stack.pop();
var2 = stack.pop();
if (!stack.isEmpty()) {
var2 = stack.pop();
} else {
var2 = "0.0";
}
value = executioner.executeOperator(temp, var2, var1, roundingMode);
stack.push(value.toPlainString());
} else if (checker.isFunction(temp)) {
int count = checker.getFunctionParamsCount(temp);
String[] table = new String[count];
for (int j = 0; j < count; j++) {
table[j] = stack.pop();
}
value = executioner.executeFunction(temp, roundingMode, table);
stack.push(value.toPlainString());
}
}
return new BigDecimal(stack.pop());
}


public RoundingMode getRoundingMode() {
return this.roundingMode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
*
* @author Bartłomiej Góra (bartlomiej.gora@gmail.com)
*/
class FunctionsDefaultChecker implements RPNChecking {
class DefaultChecker implements RPNChecking {

private Map<String, Integer> operators;

private Map<String, Integer> functions;

public FunctionsDefaultChecker() {
public DefaultChecker() {
operators = new HashMap<String, Integer>();
operators.put("+", 1);
operators.put("-", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.math.RoundingMode;


class FunctionsDefaultExecutioner implements RPNExecuting {
class DefaultExecutioner implements RPNExecuting {


@Override
Expand Down
Loading

0 comments on commit 74053d4

Please sign in to comment.