From e629c9502b93fcd646bd236149b333fa9e637cf5 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 29 Nov 2011 14:17:59 +0400 Subject: [PATCH] numeral bases added --- res/layout/calc_nine_digit_button.xml | 1 - .../android/calculator/math/MathType.java | 44 ++++++++++++++++--- .../calculator/model/CalculatorEngine.java | 2 +- .../calculator/model/NumberBuilder.java | 32 ++++++++++++-- .../android/calculator/model/Var.java | 2 +- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/res/layout/calc_nine_digit_button.xml b/res/layout/calc_nine_digit_button.xml index b3bfc9cb..fe28538f 100644 --- a/res/layout/calc_nine_digit_button.xml +++ b/res/layout/calc_nine_digit_button.xml @@ -12,6 +12,5 @@ calc:textDown="e" calc:textUp="π" calc:directionTextScale="0.5" - style="?digitButtonStyle" a:onClick="digitButtonClickHandler"/> \ No newline at end of file diff --git a/src/main/java/org/solovyev/android/calculator/math/MathType.java b/src/main/java/org/solovyev/android/calculator/math/MathType.java index 76548323..9c2fa87e 100644 --- a/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -5,6 +5,7 @@ package org.solovyev.android.calculator.math; +import jscl.NumeralBase; import jscl.math.function.Constant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,18 +15,27 @@ import org.solovyev.android.calculator.model.ParseException; import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.Finder; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +import java.util.*; public enum MathType { - digit(100, true, true, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9") { + numeral_base(50, true, false) { + + private final List tokens = new ArrayList(10); + { + for (NumeralBase numeralBase : NumeralBase.values()) { + final String jsclPrefix = numeralBase.getJsclPrefix(); + if (jsclPrefix != null) { + tokens.add(jsclPrefix); + } + } + } + + @NotNull @Override - public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) { - return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot; + public List getTokens() { + return tokens; } }, @@ -118,6 +128,26 @@ public enum MathType { } }, + digit(1125, true, true) { + + private final List tokens = new ArrayList(16); + { + for (Character character : NumeralBase.hex.getAcceptableCharacters()) { + tokens.add(character.toString()); + } + } + @Override + public boolean isNeedMultiplicationSignBefore(@NotNull MathType mathTypeBefore) { + return super.isNeedMultiplicationSignBefore(mathTypeBefore) && mathTypeBefore != digit && mathTypeBefore != dot && mathTypeBefore != numeral_base; + } + + @NotNull + @Override + public List getTokens() { + return tokens; + } + }, + comma(1150, false, false, ","), text(1200, false, false) { diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index 8b2560d4..b306065a 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -147,7 +147,7 @@ public enum CalculatorEngine { public Result evaluate(@NotNull JsclOperation operation, @NotNull String expression, - @Nullable MessageRegistry mr) throws ParseException { + @Nullable MessageRegistry mr) throws ParseException { synchronized (lock) { final StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java index 765a2c45..068902d8 100644 --- a/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java +++ b/src/main/java/org/solovyev/android/calculator/model/NumberBuilder.java @@ -6,6 +6,7 @@ package org.solovyev.android.calculator.model; +import jscl.NumeralBase; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.android.calculator.math.MathType; @@ -30,6 +31,9 @@ public class NumberBuilder { private final boolean simpleFormat; + @Nullable + private NumeralBase nb; + public NumberBuilder(boolean simpleFormat) { this.simpleFormat = simpleFormat; } @@ -39,13 +43,17 @@ public class NumberBuilder { number = null; final MathType.Result possibleResult; - if (CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.dot, MathType.grouping_separator, MathType.power_10) || - isSignAfterE(mathTypeResult)) { + if ((CollectionsUtils.contains(mathTypeResult.getMathType(), MathType.digit, MathType.numeral_base, MathType.dot, MathType.grouping_separator, MathType.power_10) || + isSignAfterE(mathTypeResult)) && numeralBaseCheck(mathTypeResult)) { if (numberBuilder == null) { numberBuilder = new StringBuilder(); } - numberBuilder.append(mathTypeResult.getMatch()); + if (mathTypeResult.getMathType() != MathType.numeral_base) { + numberBuilder.append(mathTypeResult.getMatch()); + } else { + nb = NumeralBase.getByPrefix(mathTypeResult.getMatch()); + } possibleResult = null; } else { @@ -55,6 +63,23 @@ public class NumberBuilder { return possibleResult == null ? mathTypeResult : possibleResult; } + private boolean numeralBaseCheck( @NotNull MathType.Result mathType ) { + if ( mathType.getMathType() == MathType.digit ) { + final Character ch = mathType.getMatch().charAt(0); + if ( NumeralBase.hex.getAcceptableCharacters().contains(ch) && !NumeralBase.dec.getAcceptableCharacters().contains(ch) ) { + if ( nb == NumeralBase.hex ) { + return true; + } else { + return false; + } + } else { + return true; + } + } else { + return true; + } + } + private boolean isSignAfterE(@NotNull MathType.Result mathTypeResult) { if ("-".equals(mathTypeResult.getMatch()) || "+".equals(mathTypeResult.getMatch())) { if (numberBuilder != null && numberBuilder.length() > 0) { @@ -88,6 +113,7 @@ public class NumberBuilder { } numberBuilder = null; + nb = null; } else { number = null; } diff --git a/src/main/java/org/solovyev/android/calculator/model/Var.java b/src/main/java/org/solovyev/android/calculator/model/Var.java index a97c3a59..8d75275f 100644 --- a/src/main/java/org/solovyev/android/calculator/model/Var.java +++ b/src/main/java/org/solovyev/android/calculator/model/Var.java @@ -160,7 +160,7 @@ public class Var implements IConstant { Double result = null; if (value != null) { try { - result = ExtendedConstant.getDoubleValue0(getName(), value); + result = Double.valueOf(value); } catch (NumberFormatException e) { // do nothing - string is not a double }