Reformat code and remove unused code
This commit is contained in:
parent
8add68bddb
commit
99452483f8
@ -77,7 +77,7 @@ public abstract class BaseNumberBuilder {
|
|||||||
if ("−".equals(match) || "-".equals(match) || "+".equals(match)) {
|
if ("−".equals(match) || "-".equals(match) || "+".equals(match)) {
|
||||||
final StringBuilder localNb = numberBuilder;
|
final StringBuilder localNb = numberBuilder;
|
||||||
if (localNb != null && localNb.length() > 0) {
|
if (localNb != null && localNb.length() > 0) {
|
||||||
if (localNb.charAt(localNb.length() - 1) == MathType.POWER_10) {
|
if (localNb.charAt(localNb.length() - 1) == MathType.EXPONENT) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,6 @@ import android.text.SpannableStringBuilder;
|
|||||||
import jscl.MathContext;
|
import jscl.MathContext;
|
||||||
import jscl.MathEngine;
|
import jscl.MathEngine;
|
||||||
import jscl.NumeralBase;
|
import jscl.NumeralBase;
|
||||||
import jscl.math.numeric.Real;
|
|
||||||
import jscl.text.DoubleParser;
|
import jscl.text.DoubleParser;
|
||||||
import jscl.text.JsclIntegerParser;
|
import jscl.text.JsclIntegerParser;
|
||||||
import jscl.text.ParseException;
|
import jscl.text.ParseException;
|
||||||
@ -45,60 +44,23 @@ public class NumberBuilder extends BaseNumberBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int replaceNumberInText(@Nonnull SpannableStringBuilder sb,
|
private static int replaceNumberInText(@Nonnull SpannableStringBuilder sb,
|
||||||
@Nullable String number,
|
@Nullable String oldNumber,
|
||||||
int trimmedChars,
|
int trimmedChars,
|
||||||
@Nonnull NumeralBase nb,
|
@Nonnull NumeralBase nb,
|
||||||
@Nonnull final MathEngine engine) {
|
@Nonnull final MathEngine engine) {
|
||||||
if (number == null) {
|
if (oldNumber == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// in any case remove old number from text
|
// in any case remove old number from text
|
||||||
final int oldNumberLength = number.length() + trimmedChars;
|
final int oldNumberLength = oldNumber.length() + trimmedChars;
|
||||||
sb.delete(sb.length() - oldNumberLength, sb.length());
|
sb.delete(sb.length() - oldNumberLength, sb.length());
|
||||||
|
|
||||||
final String newNumber = formatNumber(number, nb, engine);
|
final String newNumber = engine.format(oldNumber, nb);
|
||||||
sb.append(newNumber);
|
sb.append(newNumber);
|
||||||
// offset between old number and new number
|
// offset between old number and new number
|
||||||
return newNumber.length() - oldNumberLength;
|
return newNumber.length() - oldNumberLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
private static String formatNumber(@Nonnull String number, @Nonnull NumeralBase nb, @Nonnull MathEngine engine) {
|
|
||||||
String result;
|
|
||||||
|
|
||||||
int indexOfDot = number.indexOf('.');
|
|
||||||
|
|
||||||
if (indexOfDot < 0) {
|
|
||||||
int indexOfE;
|
|
||||||
if (nb == NumeralBase.hex) {
|
|
||||||
indexOfE = -1;
|
|
||||||
} else {
|
|
||||||
indexOfE = number.indexOf(MathType.POWER_10);
|
|
||||||
}
|
|
||||||
if (indexOfE < 0) {
|
|
||||||
result = engine.addGroupingSeparators(nb, number);
|
|
||||||
} else {
|
|
||||||
final String partBeforeE;
|
|
||||||
if (indexOfE != 0) {
|
|
||||||
partBeforeE = engine.addGroupingSeparators(nb, number.substring(0, indexOfE));
|
|
||||||
} else {
|
|
||||||
partBeforeE = "";
|
|
||||||
}
|
|
||||||
result = partBeforeE + number.substring(indexOfE);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
final String integerPart;
|
|
||||||
if (indexOfDot != 0) {
|
|
||||||
integerPart = engine.addGroupingSeparators(nb, number.substring(0, indexOfDot));
|
|
||||||
} else {
|
|
||||||
integerPart = "";
|
|
||||||
}
|
|
||||||
result = integerPart + number.substring(indexOfDot);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private static Double toDouble(@Nonnull String s, @Nonnull NumeralBase nb, @Nonnull final MathContext mc) throws NumberFormatException {
|
private static Double toDouble(@Nonnull String s, @Nonnull NumeralBase nb, @Nonnull final MathContext mc) throws NumberFormatException {
|
||||||
final NumeralBase defaultNb = mc.getNumeralBase();
|
final NumeralBase defaultNb = mc.getNumeralBase();
|
||||||
@ -112,7 +74,7 @@ public class NumberBuilder extends BaseNumberBuilder {
|
|||||||
p.exceptionsPool.release(e);
|
p.exceptionsPool.release(e);
|
||||||
try {
|
try {
|
||||||
p.reset();
|
p.reset();
|
||||||
return ((Real) DoubleParser.parser.parse(p, null).content()).doubleValue();
|
return DoubleParser.parser.parse(p, null).content().doubleValue();
|
||||||
} catch (ParseException e1) {
|
} catch (ParseException e1) {
|
||||||
p.exceptionsPool.release(e1);
|
p.exceptionsPool.release(e1);
|
||||||
throw new NumberFormatException();
|
throw new NumberFormatException();
|
||||||
|
@ -23,24 +23,21 @@
|
|||||||
package org.solovyev.android.calculator.math;
|
package org.solovyev.android.calculator.math;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import jscl.JsclMathEngine;
|
||||||
|
import jscl.NumeralBase;
|
||||||
|
import jscl.math.function.Constants;
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.App;
|
import org.solovyev.android.calculator.App;
|
||||||
import org.solovyev.android.calculator.Engine;
|
import org.solovyev.android.calculator.Engine;
|
||||||
import org.solovyev.android.calculator.ParseException;
|
import org.solovyev.android.calculator.ParseException;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
import jscl.JsclMathEngine;
|
|
||||||
import jscl.NumeralBase;
|
|
||||||
import jscl.math.function.Constants;
|
|
||||||
|
|
||||||
|
|
||||||
public enum MathType {
|
public enum MathType {
|
||||||
|
|
||||||
@ -224,7 +221,7 @@ public enum MathType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public static final List<String> groupSymbols = Arrays.asList("()", "[]", "{}");
|
public static final List<String> groupSymbols = Arrays.asList("()", "[]", "{}");
|
||||||
public final static Character POWER_10 = 'E';
|
public final static Character EXPONENT = 'E';
|
||||||
public static final String E = "e";
|
public static final String E = "e";
|
||||||
public static final String C = "c";
|
public static final String C = "c";
|
||||||
public final static String NAN = "NaN";
|
public final static String NAN = "NaN";
|
||||||
|
@ -21,6 +21,7 @@ import jscl.math.function.Function;
|
|||||||
import jscl.math.function.FunctionsRegistry;
|
import jscl.math.function.FunctionsRegistry;
|
||||||
import jscl.math.function.IConstant;
|
import jscl.math.function.IConstant;
|
||||||
import jscl.math.function.PostfixFunctionsRegistry;
|
import jscl.math.function.PostfixFunctionsRegistry;
|
||||||
|
import jscl.math.function.*;
|
||||||
import jscl.math.operator.Operator;
|
import jscl.math.operator.Operator;
|
||||||
import jscl.math.operator.Percent;
|
import jscl.math.operator.Percent;
|
||||||
import jscl.math.operator.Rand;
|
import jscl.math.operator.Rand;
|
||||||
@ -30,6 +31,12 @@ import jscl.text.ParseException;
|
|||||||
import static midpcalc.Real.NumberFormat.FSE_ENG;
|
import static midpcalc.Real.NumberFormat.FSE_ENG;
|
||||||
import static midpcalc.Real.NumberFormat.FSE_NONE;
|
import static midpcalc.Real.NumberFormat.FSE_NONE;
|
||||||
import static midpcalc.Real.NumberFormat.FSE_SCI;
|
import static midpcalc.Real.NumberFormat.FSE_SCI;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static midpcalc.Real.NumberFormat.*;
|
||||||
|
|
||||||
public class JsclMathEngine implements MathEngine {
|
public class JsclMathEngine implements MathEngine {
|
||||||
|
|
||||||
@ -65,14 +72,6 @@ public class JsclMathEngine implements MathEngine {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int integerValue(final double value) throws NotIntegerException {
|
|
||||||
if (Math.floor(value) == value) {
|
|
||||||
return (int) value;
|
|
||||||
} else {
|
|
||||||
throw NotIntegerException.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public String evaluate(@Nonnull String expression) throws ParseException {
|
public String evaluate(@Nonnull String expression) throws ParseException {
|
||||||
return evaluateGeneric(expression).toString();
|
return evaluateGeneric(expression).toString();
|
||||||
@ -252,21 +251,6 @@ public class JsclMathEngine implements MathEngine {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
public String convert(@Nonnull Double value, @Nonnull NumeralBase to) {
|
|
||||||
String ungroupedValue;
|
|
||||||
try {
|
|
||||||
// check if double can be converted to integer
|
|
||||||
integerValue(value);
|
|
||||||
|
|
||||||
ungroupedValue = to.toString(new BigDecimal(value).toBigInteger());
|
|
||||||
} catch (NotIntegerException e) {
|
|
||||||
ungroupedValue = to.toString(value, precision);
|
|
||||||
}
|
|
||||||
|
|
||||||
return addGroupingSeparators(to, ungroupedValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public MessageRegistry getMessageRegistry() {
|
public MessageRegistry getMessageRegistry() {
|
||||||
return messageRegistry;
|
return messageRegistry;
|
||||||
@ -277,32 +261,35 @@ public class JsclMathEngine implements MathEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedDoubleValue) {
|
@Override
|
||||||
if (hasGroupingSeparator()) {
|
public String format(@Nonnull String value, @Nonnull NumeralBase nb) {
|
||||||
final String groupingSeparator = getGroupingSeparator(nb);
|
if (!useGroupingSeparator) {
|
||||||
|
return value;
|
||||||
final int dotIndex = ungroupedDoubleValue.indexOf(".");
|
|
||||||
|
|
||||||
String ungroupedValue;
|
|
||||||
if (dotIndex >= 0) {
|
|
||||||
ungroupedValue = ungroupedDoubleValue.substring(0, dotIndex);
|
|
||||||
} else {
|
|
||||||
ungroupedValue = ungroupedDoubleValue;
|
|
||||||
}
|
|
||||||
// inject group separator in the resulted string
|
|
||||||
// NOTE: space symbol is always used!!!
|
|
||||||
StringBuilder result = insertSeparators(nb, groupingSeparator, ungroupedValue, true);
|
|
||||||
|
|
||||||
result = result.reverse();
|
|
||||||
|
|
||||||
if (dotIndex >= 0) {
|
|
||||||
result.append(insertSeparators(nb, groupingSeparator, ungroupedDoubleValue.substring(dotIndex), false));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
} else {
|
|
||||||
return ungroupedDoubleValue;
|
|
||||||
}
|
}
|
||||||
|
final int dot = value.indexOf('.');
|
||||||
|
if (dot >= 0) {
|
||||||
|
final String intPart = dot != 0 ? insertSeparators(value.substring(0, dot), nb) : "";
|
||||||
|
return intPart + value.substring(dot);
|
||||||
|
}
|
||||||
|
final int e = nb == NumeralBase.hex ? -1 : value.indexOf('E');
|
||||||
|
if (e >= 0) {
|
||||||
|
final String intPart = e != 0 ? insertSeparators(value.substring(0, e), nb) : "";
|
||||||
|
return intPart + value.substring(e);
|
||||||
|
}
|
||||||
|
return insertSeparators(value, nb);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public String insertSeparators(@Nonnull String value, @Nonnull NumeralBase nb) {
|
||||||
|
final String separator = getGroupingSeparator(nb);
|
||||||
|
final StringBuilder result = new StringBuilder(value.length() + nb.getGroupingSize() * separator.length());
|
||||||
|
for (int i = value.length() - 1; i >= 0; i--) {
|
||||||
|
result.append(value.charAt(i));
|
||||||
|
if (i != 0 && (value.length() - i) % nb.getGroupingSize() == 0) {
|
||||||
|
result.append(separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.reverse().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasGroupingSeparator() {
|
private boolean hasGroupingSeparator() {
|
||||||
@ -318,30 +305,8 @@ public class JsclMathEngine implements MathEngine {
|
|||||||
return nb == NumeralBase.dec ? groupingSeparator : ' ';
|
return nb == NumeralBase.dec ? groupingSeparator : ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
public void setRoundResult(boolean roundResult) {
|
||||||
private StringBuilder insertSeparators(@Nonnull NumeralBase nb,
|
this.roundResult = roundResult;
|
||||||
@Nonnull String groupingSeparator,
|
|
||||||
@Nonnull String value,
|
|
||||||
boolean reversed) {
|
|
||||||
final StringBuilder result = new StringBuilder(value.length() + nb.getGroupingSize() * groupingSeparator.length());
|
|
||||||
|
|
||||||
if (reversed) {
|
|
||||||
for (int i = value.length() - 1; i >= 0; i--) {
|
|
||||||
result.append(value.charAt(i));
|
|
||||||
if (i != 0 && (value.length() - i) % nb.getGroupingSize() == 0) {
|
|
||||||
result.append(groupingSeparator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (int i = 0; i < value.length(); i++) {
|
|
||||||
result.append(value.charAt(i));
|
|
||||||
if (i != 0 && i != value.length() - 1 && i % nb.getGroupingSize() == 0) {
|
|
||||||
result.append(groupingSeparator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPrecision(int precision) {
|
public void setPrecision(int precision) {
|
||||||
|
@ -51,7 +51,7 @@ public interface MathContext {
|
|||||||
String format(double value, @Nonnull NumeralBase nb);
|
String format(double value, @Nonnull NumeralBase nb);
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedIntValue);
|
String format(@Nonnull String value, @Nonnull NumeralBase nb);
|
||||||
|
|
||||||
void setNotation(int notation);
|
void setNotation(int notation);
|
||||||
}
|
}
|
||||||
|
@ -26,9 +26,6 @@ public interface MathEngine extends MathContext {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
Generic elementaryGeneric(@Nonnull String expression) throws ParseException;
|
Generic elementaryGeneric(@Nonnull String expression) throws ParseException;
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
String convert(@Nonnull Double value, @Nonnull NumeralBase to);
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
MessageRegistry getMessageRegistry();
|
MessageRegistry getMessageRegistry();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user