From 556d044df7c2086ffe6dc723824f0f1e790cdc8d Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sun, 22 Apr 2012 14:05:31 +0400 Subject: [PATCH] Digits are numeral base dependant now (show/hide digit buttons) --- .../solovyev/android/NumeralBaseUnitType.java | 83 ---------- .../calculator/AndroidNumeralBase.java | 153 ++++++++++++++++++ .../calculator/CalculatorActivity.java | 13 +- .../calculator/NumeralBaseButtons.java | 47 +----- .../view/NumeralBaseConverterDialog.java | 18 +-- .../android/AndroidNumeralBaseTest.java | 70 ++++++++ .../android/NumeralBaseUnitTypeTest.java | 69 -------- 7 files changed, 243 insertions(+), 210 deletions(-) delete mode 100644 calculatorpp/src/main/java/org/solovyev/android/NumeralBaseUnitType.java create mode 100644 calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java create mode 100644 calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java delete mode 100644 calculatorpp/src/test/java/org/solovyev/android/NumeralBaseUnitTypeTest.java diff --git a/calculatorpp/src/main/java/org/solovyev/android/NumeralBaseUnitType.java b/calculatorpp/src/main/java/org/solovyev/android/NumeralBaseUnitType.java deleted file mode 100644 index a0a222bf..00000000 --- a/calculatorpp/src/main/java/org/solovyev/android/NumeralBaseUnitType.java +++ /dev/null @@ -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 { - - 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 createUnit(@NotNull String value) { - return UnitImpl.newInstance(value, this); - } - - @NotNull - @Override - public Class 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 { - - private Converter() { - } - - @Override - public boolean isSupported(@NotNull UnitType from, @NotNull UnitType to) { - return NumeralBaseUnitType.class.isAssignableFrom(from.getClass()) && NumeralBaseUnitType.class.isAssignableFrom(to.getClass()); - } - - @NotNull - @Override - public Unit convert(@NotNull Unit from, @NotNull UnitType 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!"); - } -} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java new file mode 100644 index 00000000..d7407dec --- /dev/null +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java @@ -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 { + + bin(NumeralBase.bin) { + @NotNull + @Override + public List getButtonIds() { + return Arrays.asList(R.id.zeroDigitButton, R.id.oneDigitButton); + } + }, + + oct(NumeralBase.oct) { + @NotNull + @Override + public List getButtonIds() { + final List result = new ArrayList(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 getButtonIds() { + final List result = new ArrayList(oct.getButtonIds()); + result.addAll(Arrays.asList(R.id.eightDigitButton, R.id.nineDigitButton)); + return result; + } + }, + + hex(NumeralBase.hex) { + + @NotNull + private List 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 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 createUnit(@NotNull String value) { + return UnitImpl.newInstance(value, this); + } + + @NotNull + public abstract List 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 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 { + + private Converter() { + } + + @Override + public boolean isSupported(@NotNull UnitType from, @NotNull UnitType to) { + return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass()); + } + + @NotNull + @Override + public Unit convert(@NotNull Unit from, @NotNull UnitType 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!"); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index c338d0bd..c8974e67 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -202,13 +202,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh 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); toggleEqualsButton(preferences); @@ -549,8 +542,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh @SuppressWarnings({"UnusedDeclaration"}) public void digitButtonClickHandler(@NotNull View v) { 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"}) public void functionsButtonClickHandler(@NotNull View v) { diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java index 0dac2cdd..c2b7d088 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/NumeralBaseButtons.java @@ -5,11 +5,6 @@ import android.content.SharedPreferences; import jscl.NumeralBase; import org.jetbrains.annotations.NotNull; 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 @@ -18,46 +13,18 @@ import java.util.List; */ public class NumeralBaseButtons { - @NotNull - private final List buttonIds = new ArrayList(); - - public void addButton(@NotNull DirectionDragButton button) { - 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 toggleNumericDigits(@NotNull Activity activity, @NotNull NumeralBase currentNumeralBase) { + for (NumeralBase numeralBase : NumeralBase.values()) { + if ( currentNumeralBase != numeralBase ) { + AndroidNumeralBase.valueOf(numeralBase).toggleButtons(false, activity); } } - } - public synchronized void showNumeralDigits(@NotNull Activity 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(); + AndroidNumeralBase.valueOf(currentNumeralBase).toggleButtons(true, activity); } public synchronized void toggleNumericDigits(@NotNull Activity activity, @NotNull SharedPreferences preferences) { - if (CalculatorEngine.Preferences.numeralBase.getPreference(preferences) != NumeralBase.hex) { - this.removeNumeralDigits(activity); - } else { - this.showNumeralDigits(activity); - } + final NumeralBase nb = CalculatorEngine.Preferences.numeralBase.getPreference(preferences); + this.toggleNumericDigits(activity, nb); } } diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java index 27d66ed4..f10bda21 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java @@ -7,7 +7,7 @@ import android.view.WindowManager; import jscl.NumeralBase; import org.jetbrains.annotations.NotNull; 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.UnitImpl; import org.solovyev.android.calculator.CalculatorModel; @@ -36,22 +36,22 @@ public class NumeralBaseConverterDialog { public void show(@NotNull Context context) { final UnitConverterViewBuilder b = new UnitConverterViewBuilder(); - b.setFromUnitTypes(Arrays.asList(NumeralBaseUnitType.values())); - b.setToUnitTypes(Arrays.asList(NumeralBaseUnitType.values())); + b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values())); + b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.values())); if (!StringUtils.isEmpty(initialFromValue)) { String value = initialFromValue; try { 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) { - b.setFromValue(UnitImpl.newInstance(value, NumeralBaseUnitType.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); + b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); } } 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 alertDialogHolder = new MutableObject(); b.setOkButtonOnClickListener(new View.OnClickListener() { @@ -69,9 +69,9 @@ public class NumeralBaseConverterDialog { public void onClick(@NotNull Unit fromUnits, @NotNull Unit toUnits) { 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()) { - if (NumeralBaseUnitType.valueOf(nb).equals(toUnits.getUnitType())) { + if (AndroidNumeralBase.valueOf(nb).equals(toUnits.getUnitType())) { toUnitsValue = nb.getJsclPrefix() + toUnitsValue; break; } diff --git a/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java b/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java new file mode 100644 index 00000000..fc198068 --- /dev/null +++ b/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java @@ -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 unit = baseAndroid.createUnit(value); + + for (AndroidNumeralBase typeAndroid : typeAndroids) { + unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid); + } + + return unit.getValue(); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/NumeralBaseUnitTypeTest.java b/calculatorpp/src/test/java/org/solovyev/android/NumeralBaseUnitTypeTest.java deleted file mode 100644 index e59a17ef..00000000 --- a/calculatorpp/src/test/java/org/solovyev/android/NumeralBaseUnitTypeTest.java +++ /dev/null @@ -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 unit = baseUnitType.createUnit(value); - - for (NumeralBaseUnitType type : types) { - unit = NumeralBaseUnitType.getConverter().convert(unit, type); - } - - return unit.getValue(); - } -}