Digits are numeral base dependant now (show/hide digit buttons)
This commit is contained in:
parent
8fee55a17e
commit
556d044df7
@ -1,83 +0,0 @@
|
|||||||
package org.solovyev.android;
|
|
||||||
|
|
||||||
import jscl.NumeralBase;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 4/21/12
|
|
||||||
* Time: 8:00 PM
|
|
||||||
*/
|
|
||||||
public enum NumeralBaseUnitType implements UnitType<String> {
|
|
||||||
|
|
||||||
bin(NumeralBase.bin),
|
|
||||||
oct(NumeralBase.oct),
|
|
||||||
dec(NumeralBase.dec),
|
|
||||||
hex(NumeralBase.hex);
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final NumeralBase numeralBase;
|
|
||||||
|
|
||||||
private NumeralBaseUnitType(@NotNull NumeralBase numeralBase) {
|
|
||||||
this.numeralBase = numeralBase;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Unit<String> createUnit(@NotNull String value) {
|
|
||||||
return UnitImpl.newInstance(value, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Class<String> getUnitValueClass() {
|
|
||||||
return String.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static final Converter converter = new Converter();
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static Converter getConverter() {
|
|
||||||
return converter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Converter implements UnitConverter<String> {
|
|
||||||
|
|
||||||
private Converter() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
|
||||||
return NumeralBaseUnitType.class.isAssignableFrom(from.getClass()) && NumeralBaseUnitType.class.isAssignableFrom(to.getClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
|
||||||
if (!isSupported(from.getUnitType(), toType)) {
|
|
||||||
throw new IllegalArgumentException("Types are not supported!");
|
|
||||||
}
|
|
||||||
|
|
||||||
final NumeralBaseUnitType fromType = (NumeralBaseUnitType) from.getUnitType();
|
|
||||||
final NumeralBase fromNumeralBase = fromType.numeralBase;
|
|
||||||
final NumeralBase toNumeralBase = ((NumeralBaseUnitType) toType).numeralBase;
|
|
||||||
final String fromValue = (String) from.getValue();
|
|
||||||
|
|
||||||
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
|
||||||
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (NumeralBaseUnitType) toType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static NumeralBaseUnitType valueOf(@NotNull NumeralBase nb ) {
|
|
||||||
for (NumeralBaseUnitType numeralBaseUnitType : values()) {
|
|
||||||
if ( numeralBaseUnitType.numeralBase == nb ) {
|
|
||||||
return numeralBaseUnitType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalArgumentException(nb + " is not supported numeral base!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,153 @@
|
|||||||
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import jscl.NumeralBase;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.solovyev.android.Unit;
|
||||||
|
import org.solovyev.android.UnitConverter;
|
||||||
|
import org.solovyev.android.UnitImpl;
|
||||||
|
import org.solovyev.android.UnitType;
|
||||||
|
import org.solovyev.android.view.drag.DirectionDragButton;
|
||||||
|
import org.solovyev.android.view.drag.DragDirection;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: serso
|
||||||
|
* Date: 4/21/12
|
||||||
|
* Time: 8:00 PM
|
||||||
|
*/
|
||||||
|
public enum AndroidNumeralBase implements UnitType<String> {
|
||||||
|
|
||||||
|
bin(NumeralBase.bin) {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<Integer> getButtonIds() {
|
||||||
|
return Arrays.asList(R.id.zeroDigitButton, R.id.oneDigitButton);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
oct(NumeralBase.oct) {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<Integer> getButtonIds() {
|
||||||
|
final List<Integer> result = new ArrayList<Integer>(bin.getButtonIds());
|
||||||
|
result.addAll(Arrays.asList(R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton, R.id.sevenDigitButton));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
dec(NumeralBase.dec) {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<Integer> getButtonIds() {
|
||||||
|
final List<Integer> result = new ArrayList<Integer>(oct.getButtonIds());
|
||||||
|
result.addAll(Arrays.asList(R.id.eightDigitButton, R.id.nineDigitButton));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
hex(NumeralBase.hex) {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private List<Integer> specialHexButtonIds = Arrays.asList(R.id.oneDigitButton, R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton);
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<Integer> getButtonIds() {
|
||||||
|
return dec.getButtonIds();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
|
||||||
|
super.toggleButton(show, button);
|
||||||
|
if (specialHexButtonIds.contains(button.getId())) {
|
||||||
|
button.showDirectionText(show, DragDirection.left);
|
||||||
|
button.invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final NumeralBase numeralBase;
|
||||||
|
|
||||||
|
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) {
|
||||||
|
this.numeralBase = numeralBase;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Unit<String> createUnit(@NotNull String value) {
|
||||||
|
return UnitImpl.newInstance(value, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public abstract List<Integer> getButtonIds();
|
||||||
|
|
||||||
|
public void toggleButtons(boolean show, @NotNull Activity activity) {
|
||||||
|
for (Integer buttonId : getButtonIds()) {
|
||||||
|
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(buttonId);
|
||||||
|
if (button != null) {
|
||||||
|
toggleButton(show, button);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
|
||||||
|
button.setShowText(show);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Class<String> getUnitValueClass() {
|
||||||
|
return String.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static final Converter converter = new Converter();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Converter getConverter() {
|
||||||
|
return converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Converter implements UnitConverter<String> {
|
||||||
|
|
||||||
|
private Converter() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
|
||||||
|
return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
|
||||||
|
if (!isSupported(from.getUnitType(), toType)) {
|
||||||
|
throw new IllegalArgumentException("Types are not supported!");
|
||||||
|
}
|
||||||
|
|
||||||
|
final AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType();
|
||||||
|
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
|
||||||
|
final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase;
|
||||||
|
final String fromValue = (String) from.getValue();
|
||||||
|
|
||||||
|
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
|
||||||
|
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
|
||||||
|
for (AndroidNumeralBase androidNumeralBase : values()) {
|
||||||
|
if (androidNumeralBase.numeralBase == nb) {
|
||||||
|
return androidNumeralBase;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException(nb + " is not supported numeral base!");
|
||||||
|
}
|
||||||
|
}
|
@ -202,13 +202,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
toggleButtonDirectionText(R.id.plusButton, false, DragDirection.down, DragDirection.up);
|
toggleButtonDirectionText(R.id.plusButton, false, DragDirection.down, DragDirection.up);
|
||||||
}
|
}
|
||||||
|
|
||||||
numeralBaseButtons.clear();
|
|
||||||
numeralBaseButtons.addButtonId(R.id.oneDigitButton);
|
|
||||||
numeralBaseButtons.addButtonId(R.id.twoDigitButton);
|
|
||||||
numeralBaseButtons.addButtonId(R.id.threeDigitButton);
|
|
||||||
numeralBaseButtons.addButtonId(R.id.fourDigitButton);
|
|
||||||
numeralBaseButtons.addButtonId(R.id.fiveDigitButton);
|
|
||||||
numeralBaseButtons.addButtonId(R.id.sixDigitButton);
|
|
||||||
numeralBaseButtons.toggleNumericDigits(this, preferences);
|
numeralBaseButtons.toggleNumericDigits(this, preferences);
|
||||||
|
|
||||||
toggleEqualsButton(preferences);
|
toggleEqualsButton(preferences);
|
||||||
@ -549,8 +542,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
@SuppressWarnings({"UnusedDeclaration"})
|
@SuppressWarnings({"UnusedDeclaration"})
|
||||||
public void digitButtonClickHandler(@NotNull View v) {
|
public void digitButtonClickHandler(@NotNull View v) {
|
||||||
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
|
Log.d(String.valueOf(v.getId()), "digitButtonClickHandler() for: " + v.getId() + ". Pressed: " + v.isPressed());
|
||||||
calculatorModel.processDigitButtonAction(((DirectionDragButton) v).getText().toString());
|
if (((ColorButton) v).isShowText()) {
|
||||||
}
|
calculatorModel.processDigitButtonAction(((ColorButton) v).getText().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"UnusedDeclaration"})
|
@SuppressWarnings({"UnusedDeclaration"})
|
||||||
public void functionsButtonClickHandler(@NotNull View v) {
|
public void functionsButtonClickHandler(@NotNull View v) {
|
||||||
|
@ -5,11 +5,6 @@ import android.content.SharedPreferences;
|
|||||||
import jscl.NumeralBase;
|
import jscl.NumeralBase;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.solovyev.android.calculator.model.CalculatorEngine;
|
import org.solovyev.android.calculator.model.CalculatorEngine;
|
||||||
import org.solovyev.android.view.drag.DirectionDragButton;
|
|
||||||
import org.solovyev.android.view.drag.DragDirection;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -18,46 +13,18 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class NumeralBaseButtons {
|
public class NumeralBaseButtons {
|
||||||
|
|
||||||
@NotNull
|
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull NumeralBase currentNumeralBase) {
|
||||||
private final List<Integer> buttonIds = new ArrayList<Integer>();
|
for (NumeralBase numeralBase : NumeralBase.values()) {
|
||||||
|
if ( currentNumeralBase != numeralBase ) {
|
||||||
public void addButton(@NotNull DirectionDragButton button) {
|
AndroidNumeralBase.valueOf(numeralBase).toggleButtons(false, activity);
|
||||||
buttonIds.add(button.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addButtonId(@NotNull Integer buttonId) {
|
|
||||||
buttonIds.add(buttonId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void removeNumeralDigits(@NotNull Activity activity) {
|
|
||||||
for (Integer id : buttonIds) {
|
|
||||||
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(id);
|
|
||||||
if (button != null) {
|
|
||||||
button.showDirectionText(false, DragDirection.left);
|
|
||||||
button.invalidate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void showNumeralDigits(@NotNull Activity activity) {
|
AndroidNumeralBase.valueOf(currentNumeralBase).toggleButtons(true, activity);
|
||||||
for (Integer id : buttonIds) {
|
|
||||||
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(id);
|
|
||||||
if (button != null) {
|
|
||||||
button.showDirectionText(true, DragDirection.left);
|
|
||||||
button.invalidate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clear() {
|
|
||||||
buttonIds.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull SharedPreferences preferences) {
|
public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull SharedPreferences preferences) {
|
||||||
if (CalculatorEngine.Preferences.numeralBase.getPreference(preferences) != NumeralBase.hex) {
|
final NumeralBase nb = CalculatorEngine.Preferences.numeralBase.getPreference(preferences);
|
||||||
this.removeNumeralDigits(activity);
|
this.toggleNumericDigits(activity, nb);
|
||||||
} else {
|
|
||||||
this.showNumeralDigits(activity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import android.view.WindowManager;
|
|||||||
import jscl.NumeralBase;
|
import jscl.NumeralBase;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.solovyev.android.NumeralBaseUnitType;
|
import org.solovyev.android.calculator.AndroidNumeralBase;
|
||||||
import org.solovyev.android.Unit;
|
import org.solovyev.android.Unit;
|
||||||
import org.solovyev.android.UnitImpl;
|
import org.solovyev.android.UnitImpl;
|
||||||
import org.solovyev.android.calculator.CalculatorModel;
|
import org.solovyev.android.calculator.CalculatorModel;
|
||||||
@ -36,22 +36,22 @@ public class NumeralBaseConverterDialog {
|
|||||||
|
|
||||||
public void show(@NotNull Context context) {
|
public void show(@NotNull Context context) {
|
||||||
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
final UnitConverterViewBuilder b = new UnitConverterViewBuilder();
|
||||||
b.setFromUnitTypes(Arrays.asList(NumeralBaseUnitType.values()));
|
b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||||
b.setToUnitTypes(Arrays.asList(NumeralBaseUnitType.values()));
|
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
|
||||||
|
|
||||||
if (!StringUtils.isEmpty(initialFromValue)) {
|
if (!StringUtils.isEmpty(initialFromValue)) {
|
||||||
String value = initialFromValue;
|
String value = initialFromValue;
|
||||||
try {
|
try {
|
||||||
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
value = ToJsclTextProcessor.getInstance().process(value).getExpression();
|
||||||
b.setFromValue(UnitImpl.newInstance(value, NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||||
} catch (CalculatorParseException e) {
|
} catch (CalculatorParseException e) {
|
||||||
b.setFromValue(UnitImpl.newInstance(value, NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
b.setFromValue(UnitImpl.newInstance("", NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
|
||||||
}
|
}
|
||||||
|
|
||||||
b.setConverter(NumeralBaseUnitType.getConverter());
|
b.setConverter(AndroidNumeralBase.getConverter());
|
||||||
|
|
||||||
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
final MutableObject<AlertDialog> alertDialogHolder = new MutableObject<AlertDialog>();
|
||||||
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
b.setOkButtonOnClickListener(new View.OnClickListener() {
|
||||||
@ -69,9 +69,9 @@ public class NumeralBaseConverterDialog {
|
|||||||
public void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits) {
|
public void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits) {
|
||||||
String toUnitsValue = toUnits.getValue();
|
String toUnitsValue = toUnits.getValue();
|
||||||
|
|
||||||
if (!toUnits.getUnitType().equals(NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
|
||||||
for (NumeralBase nb : NumeralBase.values()) {
|
for (NumeralBase nb : NumeralBase.values()) {
|
||||||
if (NumeralBaseUnitType.valueOf(nb).equals(toUnits.getUnitType())) {
|
if (AndroidNumeralBase.valueOf(nb).equals(toUnits.getUnitType())) {
|
||||||
toUnitsValue = nb.getJsclPrefix() + toUnitsValue;
|
toUnitsValue = nb.getJsclPrefix() + toUnitsValue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package org.solovyev.android;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.solovyev.android.calculator.AndroidNumeralBase;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: serso
|
||||||
|
* Date: 4/21/12
|
||||||
|
* Time: 8:24 PM
|
||||||
|
*/
|
||||||
|
public class AndroidNumeralBaseTest {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final UnitConverter c = AndroidNumeralBase.getConverter();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSupported() throws Exception {
|
||||||
|
Assert.assertTrue(c.isSupported(AndroidNumeralBase.bin, AndroidNumeralBase.dec));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertFromDec() throws Exception {
|
||||||
|
|
||||||
|
Assert.assertEquals("101", c.convert(AndroidNumeralBase.dec.createUnit("5"), AndroidNumeralBase.bin).getValue());
|
||||||
|
Assert.assertEquals("1", c.convert(AndroidNumeralBase.dec.createUnit("1"), AndroidNumeralBase.bin).getValue());
|
||||||
|
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.bin).getValue());
|
||||||
|
Assert.assertEquals("1111100111", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.bin).getValue());
|
||||||
|
|
||||||
|
Assert.assertEquals("A23", c.convert(AndroidNumeralBase.dec.createUnit("2595"), AndroidNumeralBase.hex).getValue());
|
||||||
|
Assert.assertEquals("AEE", c.convert(AndroidNumeralBase.dec.createUnit("2798"), AndroidNumeralBase.hex).getValue());
|
||||||
|
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("21"), AndroidNumeralBase.hex).getValue());
|
||||||
|
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.hex).getValue());
|
||||||
|
Assert.assertEquals("3E7", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.hex).getValue());
|
||||||
|
|
||||||
|
Assert.assertEquals("76", c.convert(AndroidNumeralBase.dec.createUnit("62"), AndroidNumeralBase.oct).getValue());
|
||||||
|
Assert.assertEquals("12", c.convert(AndroidNumeralBase.dec.createUnit("10"), AndroidNumeralBase.oct).getValue());
|
||||||
|
Assert.assertEquals("15", c.convert(AndroidNumeralBase.dec.createUnit("13"), AndroidNumeralBase.oct).getValue());
|
||||||
|
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.oct).getValue());
|
||||||
|
Assert.assertEquals("10445", c.convert(AndroidNumeralBase.dec.createUnit("4389"), AndroidNumeralBase.oct).getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRandomConvert() throws Exception {
|
||||||
|
final Random random = new Random(new Date().getTime());
|
||||||
|
for (int i = 0; i < 100000; i++) {
|
||||||
|
final String value = String.valueOf(random.nextInt());
|
||||||
|
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.oct, AndroidNumeralBase.oct, AndroidNumeralBase.bin, AndroidNumeralBase.dec));
|
||||||
|
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.bin, AndroidNumeralBase.hex, AndroidNumeralBase.dec, AndroidNumeralBase.dec));
|
||||||
|
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.oct, AndroidNumeralBase.dec));
|
||||||
|
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.bin, AndroidNumeralBase.oct, AndroidNumeralBase.dec));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String convertChain(@NotNull String value, @NotNull AndroidNumeralBase baseAndroid, @NotNull AndroidNumeralBase... typeAndroids) {
|
||||||
|
Unit<String> unit = baseAndroid.createUnit(value);
|
||||||
|
|
||||||
|
for (AndroidNumeralBase typeAndroid : typeAndroids) {
|
||||||
|
unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return unit.getValue();
|
||||||
|
}
|
||||||
|
}
|
@ -1,69 +0,0 @@
|
|||||||
package org.solovyev.android;
|
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 4/21/12
|
|
||||||
* Time: 8:24 PM
|
|
||||||
*/
|
|
||||||
public class NumeralBaseUnitTypeTest {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final UnitConverter c = NumeralBaseUnitType.getConverter();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testIsSupported() throws Exception {
|
|
||||||
Assert.assertTrue(c.isSupported(NumeralBaseUnitType.bin, NumeralBaseUnitType.dec));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testConvertFromDec() throws Exception {
|
|
||||||
|
|
||||||
Assert.assertEquals("101", c.convert(NumeralBaseUnitType.dec.createUnit("5"), NumeralBaseUnitType.bin).getValue());
|
|
||||||
Assert.assertEquals("1", c.convert(NumeralBaseUnitType.dec.createUnit("1"), NumeralBaseUnitType.bin).getValue());
|
|
||||||
Assert.assertEquals("0", c.convert(NumeralBaseUnitType.dec.createUnit("0"), NumeralBaseUnitType.bin).getValue());
|
|
||||||
Assert.assertEquals("1111100111", c.convert(NumeralBaseUnitType.dec.createUnit("999"), NumeralBaseUnitType.bin).getValue());
|
|
||||||
|
|
||||||
Assert.assertEquals("A23", c.convert(NumeralBaseUnitType.dec.createUnit("2595"), NumeralBaseUnitType.hex).getValue());
|
|
||||||
Assert.assertEquals("AEE", c.convert(NumeralBaseUnitType.dec.createUnit("2798"), NumeralBaseUnitType.hex).getValue());
|
|
||||||
Assert.assertEquals("15", c.convert(NumeralBaseUnitType.dec.createUnit("21"), NumeralBaseUnitType.hex).getValue());
|
|
||||||
Assert.assertEquals("0", c.convert(NumeralBaseUnitType.dec.createUnit("0"), NumeralBaseUnitType.hex).getValue());
|
|
||||||
Assert.assertEquals("3E7", c.convert(NumeralBaseUnitType.dec.createUnit("999"), NumeralBaseUnitType.hex).getValue());
|
|
||||||
|
|
||||||
Assert.assertEquals("76", c.convert(NumeralBaseUnitType.dec.createUnit("62"), NumeralBaseUnitType.oct).getValue());
|
|
||||||
Assert.assertEquals("12", c.convert(NumeralBaseUnitType.dec.createUnit("10"), NumeralBaseUnitType.oct).getValue());
|
|
||||||
Assert.assertEquals("15", c.convert(NumeralBaseUnitType.dec.createUnit("13"), NumeralBaseUnitType.oct).getValue());
|
|
||||||
Assert.assertEquals("0", c.convert(NumeralBaseUnitType.dec.createUnit("0"), NumeralBaseUnitType.oct).getValue());
|
|
||||||
Assert.assertEquals("10445", c.convert(NumeralBaseUnitType.dec.createUnit("4389"), NumeralBaseUnitType.oct).getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testRandomConvert() throws Exception {
|
|
||||||
final Random random = new Random(new Date().getTime());
|
|
||||||
for (int i = 0; i < 100000; i++) {
|
|
||||||
final String value = String.valueOf(random.nextInt());
|
|
||||||
Assert.assertEquals(value, convertChain(value, NumeralBaseUnitType.dec, NumeralBaseUnitType.oct, NumeralBaseUnitType.oct, NumeralBaseUnitType.bin, NumeralBaseUnitType.dec));
|
|
||||||
Assert.assertEquals(value, convertChain(value, NumeralBaseUnitType.dec, NumeralBaseUnitType.bin, NumeralBaseUnitType.hex, NumeralBaseUnitType.dec, NumeralBaseUnitType.dec));
|
|
||||||
Assert.assertEquals(value, convertChain(value, NumeralBaseUnitType.dec, NumeralBaseUnitType.dec, NumeralBaseUnitType.hex, NumeralBaseUnitType.oct, NumeralBaseUnitType.dec));
|
|
||||||
Assert.assertEquals(value, convertChain(value, NumeralBaseUnitType.dec, NumeralBaseUnitType.hex, NumeralBaseUnitType.bin, NumeralBaseUnitType.oct, NumeralBaseUnitType.dec));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private String convertChain(@NotNull String value, @NotNull NumeralBaseUnitType baseUnitType, @NotNull NumeralBaseUnitType... types) {
|
|
||||||
Unit<String> unit = baseUnitType.createUnit(value);
|
|
||||||
|
|
||||||
for (NumeralBaseUnitType type : types) {
|
|
||||||
unit = NumeralBaseUnitType.getConverter().convert(unit, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return unit.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user