From 33db715776a198fc62b24c90a7321c2f46749fcf Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 20 Sep 2012 12:07:55 +0400 Subject: [PATCH] Calculator++ core module --- calculatorpp-core/pom.xml | 36 + .../java/org/solovyev/math/units}/Unit.java | 34 +- .../solovyev/math/units}/UnitConverter.java | 84 +-- .../org/solovyev/math/units}/UnitImpl.java | 84 +-- .../org/solovyev/math/units}/UnitType.java | 32 +- calculatorpp-service/pom.xml | 69 +- calculatorpp/pom.xml | 709 +++++++++--------- .../calculator/AndroidNumeralBase.java | 316 ++++---- .../view/NumeralBaseConverterDialog.java | 198 ++--- .../view/UnitConverterViewBuilder.java | 518 ++++++------- .../android/AndroidNumeralBaseTest.java | 142 ++-- pom.xml | 9 +- 12 files changed, 1140 insertions(+), 1091 deletions(-) create mode 100644 calculatorpp-core/pom.xml rename {calculatorpp/src/main/java/org/solovyev/android => calculatorpp-core/src/main/java/org/solovyev/math/units}/Unit.java (80%) rename {calculatorpp/src/main/java/org/solovyev/android => calculatorpp-core/src/main/java/org/solovyev/math/units}/UnitConverter.java (92%) rename {calculatorpp/src/main/java/org/solovyev/android => calculatorpp-core/src/main/java/org/solovyev/math/units}/UnitImpl.java (90%) rename {calculatorpp/src/main/java/org/solovyev/android => calculatorpp-core/src/main/java/org/solovyev/math/units}/UnitType.java (81%) diff --git a/calculatorpp-core/pom.xml b/calculatorpp-core/pom.xml new file mode 100644 index 00000000..7f59b53d --- /dev/null +++ b/calculatorpp-core/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + + org.solovyev.android + calculatorpp-parent + 1.3.1 + + + org.solovyev.android + calculatorpp-core + 1.3.1 + Calculator++ Application Core + + jar + + + + + junit + junit + test + + + + com.intellij + annotations + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/Unit.java b/calculatorpp-core/src/main/java/org/solovyev/math/units/Unit.java similarity index 80% rename from calculatorpp/src/main/java/org/solovyev/android/Unit.java rename to calculatorpp-core/src/main/java/org/solovyev/math/units/Unit.java index c3a2d8a5..23921ea8 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/Unit.java +++ b/calculatorpp-core/src/main/java/org/solovyev/math/units/Unit.java @@ -1,17 +1,17 @@ -package org.solovyev.android; - -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 4/21/12 - * Time: 7:54 PM - */ -public interface Unit { - - @NotNull - V getValue(); - - @NotNull - UnitType getUnitType(); -} +package org.solovyev.math.units; + +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 4/21/12 + * Time: 7:54 PM + */ +public interface Unit { + + @NotNull + V getValue(); + + @NotNull + UnitType getUnitType(); +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/UnitConverter.java b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitConverter.java similarity index 92% rename from calculatorpp/src/main/java/org/solovyev/android/UnitConverter.java rename to calculatorpp-core/src/main/java/org/solovyev/math/units/UnitConverter.java index 51785cc7..2d56bebd 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/UnitConverter.java +++ b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitConverter.java @@ -1,42 +1,42 @@ -package org.solovyev.android; - -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 4/21/12 - * Time: 7:53 PM - */ -public interface UnitConverter { - - boolean isSupported(@NotNull UnitType from, @NotNull UnitType to); - - @NotNull - Unit convert(@NotNull Unit from, @NotNull UnitType toType); - - public static class Dummy implements UnitConverter { - - @NotNull - private static final Dummy instance = new Dummy(); - - @NotNull - public static UnitConverter getInstance() { - return (UnitConverter)instance; - } - - private Dummy() { - } - - @Override - public boolean isSupported(@NotNull UnitType from, @NotNull UnitType to) { - return false; - } - - @NotNull - @Override - public Unit convert(@NotNull Unit from, @NotNull UnitType toType) { - throw new IllegalArgumentException(); - } - } - -} +package org.solovyev.math.units; + +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 4/21/12 + * Time: 7:53 PM + */ +public interface UnitConverter { + + boolean isSupported(@NotNull UnitType from, @NotNull UnitType to); + + @NotNull + Unit convert(@NotNull Unit from, @NotNull UnitType toType); + + public static class Dummy implements UnitConverter { + + @NotNull + private static final Dummy instance = new Dummy(); + + @NotNull + public static UnitConverter getInstance() { + return (UnitConverter)instance; + } + + private Dummy() { + } + + @Override + public boolean isSupported(@NotNull UnitType from, @NotNull UnitType to) { + return false; + } + + @NotNull + @Override + public Unit convert(@NotNull Unit from, @NotNull UnitType toType) { + throw new IllegalArgumentException(); + } + } + +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/UnitImpl.java b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitImpl.java similarity index 90% rename from calculatorpp/src/main/java/org/solovyev/android/UnitImpl.java rename to calculatorpp-core/src/main/java/org/solovyev/math/units/UnitImpl.java index b1c08ef0..647e0c6f 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/UnitImpl.java +++ b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitImpl.java @@ -1,42 +1,42 @@ -package org.solovyev.android; - -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 4/21/12 - * Time: 8:01 PM - */ -public class UnitImpl implements Unit { - - @NotNull - private V value; - - @NotNull - private UnitType unitType; - - private UnitImpl() { - } - - @NotNull - public static Unit newInstance(@NotNull V value, @NotNull UnitType unitType) { - final UnitImpl result = new UnitImpl(); - - result.value = value; - result.unitType = unitType; - - return result; - } - - @NotNull - @Override - public V getValue() { - return this.value; - } - - @NotNull - @Override - public UnitType getUnitType() { - return unitType; - } -} +package org.solovyev.math.units; + +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 4/21/12 + * Time: 8:01 PM + */ +public class UnitImpl implements Unit { + + @NotNull + private V value; + + @NotNull + private UnitType unitType; + + private UnitImpl() { + } + + @NotNull + public static Unit newInstance(@NotNull V value, @NotNull UnitType unitType) { + final UnitImpl result = new UnitImpl(); + + result.value = value; + result.unitType = unitType; + + return result; + } + + @NotNull + @Override + public V getValue() { + return this.value; + } + + @NotNull + @Override + public UnitType getUnitType() { + return unitType; + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/UnitType.java b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitType.java similarity index 81% rename from calculatorpp/src/main/java/org/solovyev/android/UnitType.java rename to calculatorpp-core/src/main/java/org/solovyev/math/units/UnitType.java index 82175bdf..457442c2 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/UnitType.java +++ b/calculatorpp-core/src/main/java/org/solovyev/math/units/UnitType.java @@ -1,16 +1,16 @@ -package org.solovyev.android; - -import org.jetbrains.annotations.NotNull; - -/** - * User: serso - * Date: 4/21/12 - * Time: 7:55 PM - */ -public interface UnitType { - - @NotNull - Class getUnitValueClass(); - - boolean equals(@NotNull Object o); -} +package org.solovyev.math.units; + +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 4/21/12 + * Time: 7:55 PM + */ +public interface UnitType { + + @NotNull + Class getUnitValueClass(); + + boolean equals(@NotNull Object o); +} diff --git a/calculatorpp-service/pom.xml b/calculatorpp-service/pom.xml index 236f1951..220ae027 100644 --- a/calculatorpp-service/pom.xml +++ b/calculatorpp-service/pom.xml @@ -1,36 +1,35 @@ - - - - - org.solovyev.android - calculatorpp-parent - 1.3.1 - - - 4.0.0 - - org.solovyev.android - calculatorpp-service - 0.1 - apklib - Calculator++ Service - - - - - com.intellij - annotations - 7.0.3 - - - - com.google.android - android - provided - - - - + + + + + org.solovyev.android + calculatorpp-parent + 1.3.1 + + + 4.0.0 + + org.solovyev.android + calculatorpp-service + 0.1 + apklib + Calculator++ Service + + + + + com.intellij + annotations + + + + com.google.android + android + provided + + + + \ No newline at end of file diff --git a/calculatorpp/pom.xml b/calculatorpp/pom.xml index e83dc844..ea6c981a 100644 --- a/calculatorpp/pom.xml +++ b/calculatorpp/pom.xml @@ -1,353 +1,358 @@ - - - - - org.solovyev.android - calculatorpp-parent - 1.3.1 - - - 4.0.0 - - org.solovyev.android - calculatorpp - apk - Calculator++ Application - - - - - - org.solovyev - common-core - - - - org.solovyev - common-text - - - - org.solovyev.android - android-common-core - apklib - - - - org.solovyev.android - android-common-ads - apklib - - - - org.solovyev.android - android-common-view - apklib - - - - org.solovyev.android - android-common-preferences - apklib - - - - org.solovyev.android - android-common-other - apklib - - - - org.solovyev.android - android-common-menu - apklib - - - - org.solovyev.android - calculatorpp-service - 0.1 - apklib - - - - org.solovyev - jscl - 0.0.2 - - - xercesImpl - xerces - - - - - - - - com.google.android - android - provided - - - - junit - junit - 4.8.2 - test - - - - net.sf.opencsv - opencsv - 2.0 - test - - - - org.simpleframework - simple-xml - 2.6.1 - - - stax-api - stax - - - xpp3 - xpp3 - - - - - - achartengine - achartengine - 0.7.0 - - - - admob - admob - 6.1.0 - - - - org.solovyev.android - billing - 0.1 - - - - - com.google.guava - guava - 11.0.2 - - - - com.intellij - annotations - 7.0.3 - - - - - - - - - - - com.jayway.maven.plugins.android.generation2 - android-maven-plugin - true - - - true - - - - - manifestUpdate - process-resources - - manifest-update - - - - alignApk - package - - zipalign - - - - - - - - - - - - - release - - - - performRelease - true - - - - - - - - com.jayway.maven.plugins.android.generation2 - android-maven-plugin - - - - alignApk - package - - zipalign - - - - - - - - org.codehaus.mojo - properties-maven-plugin - 1.0-alpha-2 - - - initialize - - read-project-properties - - - - ${project.basedir}/misc/env/jarsigner.properties - - - - - - - - org.apache.maven.plugins - maven-jarsigner-plugin - - - signing - - sign - verify - - package - true - - true - - - ${project.build.directory}/${project.artifactId}-${project.version}.apk - - ${sign.keystore} - ${sign.alias} - ${sign.storepass} - ${sign.keypass} - false - - - - - - - - com.jayway.maven.plugins.android.generation2 - android-maven-plugin - true - - - - false - - - - false - ${project.build.directory}/${project.artifactId}-${project.version}.apk - ${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk - - - - false - true - - - - true - - - - - - manifestUpdate - process-resources - - manifest-update - - - - alignApk - package - - zipalign - - - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - - ${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk - apk - signed-aligned - - - ${project.build.directory}/proguard/mapping.txt - map - release - - - - - - attach-signed-aligned - package - - attach-artifact - - - - - - - - - - - + + + + + org.solovyev.android + calculatorpp-parent + 1.3.1 + + + 4.0.0 + + org.solovyev.android + calculatorpp + apk + Calculator++ Application + + + + + + + org.solovyev.android + calculatorpp-core + 1.3.1 + + + + org.solovyev + common-core + + + + org.solovyev + common-text + + + + org.solovyev.android + android-common-core + apklib + + + + org.solovyev.android + android-common-ads + apklib + + + + org.solovyev.android + android-common-view + apklib + + + + org.solovyev.android + android-common-preferences + apklib + + + + org.solovyev.android + android-common-other + apklib + + + + org.solovyev.android + android-common-menu + apklib + + + + org.solovyev.android + calculatorpp-service + 0.1 + apklib + + + + org.solovyev + jscl + 0.0.2 + + + xercesImpl + xerces + + + + + + + + com.google.android + android + provided + + + + net.sf.opencsv + opencsv + 2.0 + test + + + + org.simpleframework + simple-xml + 2.6.1 + + + stax-api + stax + + + xpp3 + xpp3 + + + + + + achartengine + achartengine + 0.7.0 + + + + admob + admob + 6.1.0 + + + + org.solovyev.android + billing + 0.1 + + + + + com.google.guava + guava + 11.0.2 + + + + junit + junit + test + + + + com.intellij + annotations + + + + + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + true + + + true + + + + + manifestUpdate + process-resources + + manifest-update + + + + alignApk + package + + zipalign + + + + + + + + + + + + + release + + + + performRelease + true + + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + + + + alignApk + package + + zipalign + + + + + + + + org.codehaus.mojo + properties-maven-plugin + 1.0-alpha-2 + + + initialize + + read-project-properties + + + + ${project.basedir}/misc/env/jarsigner.properties + + + + + + + + org.apache.maven.plugins + maven-jarsigner-plugin + + + signing + + sign + verify + + package + true + + true + + + ${project.build.directory}/${project.artifactId}-${project.version}.apk + + ${sign.keystore} + ${sign.alias} + ${sign.storepass} + ${sign.keypass} + false + + + + + + + + com.jayway.maven.plugins.android.generation2 + android-maven-plugin + true + + + + false + + + + false + ${project.build.directory}/${project.artifactId}-${project.version}.apk + ${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk + + + + false + true + + + + true + + + + + + manifestUpdate + process-resources + + manifest-update + + + + alignApk + package + + zipalign + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + + ${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk + apk + signed-aligned + + + ${project.build.directory}/proguard/mapping.txt + map + release + + + + + + attach-signed-aligned + package + + attach-artifact + + + + + + + + + + + \ No newline at end of file diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java index 7e9262fe..4abf638d 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/AndroidNumeralBase.java @@ -1,158 +1,158 @@ -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 - public NumeralBase getNumeralBase() { - return numeralBase; - } - - @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!"); - } -} +package org.solovyev.android.calculator; + +import android.app.Activity; +import jscl.NumeralBase; +import org.jetbrains.annotations.NotNull; +import org.solovyev.math.units.Unit; +import org.solovyev.math.units.UnitConverter; +import org.solovyev.math.units.UnitImpl; +import org.solovyev.math.units.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 + public NumeralBase getNumeralBase() { + return numeralBase; + } + + @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/view/NumeralBaseConverterDialog.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/NumeralBaseConverterDialog.java index 2dfa459f..a8fa7a46 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 @@ -1,99 +1,99 @@ -package org.solovyev.android.calculator.view; - -import android.app.AlertDialog; -import android.content.Context; -import android.view.View; -import android.view.WindowManager; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.Unit; -import org.solovyev.android.UnitImpl; -import org.solovyev.android.calculator.AndroidNumeralBase; -import org.solovyev.android.calculator.CalculatorModel; -import org.solovyev.android.calculator.R; -import org.solovyev.android.calculator.model.CalculatorEngine; -import org.solovyev.android.calculator.model.CalculatorParseException; -import org.solovyev.android.calculator.model.ToJsclTextProcessor; -import org.solovyev.common.MutableObject; -import org.solovyev.common.text.StringUtils; - -import java.util.Arrays; - -/** - * User: serso - * Date: 4/22/12 - * Time: 12:20 AM - */ -public class NumeralBaseConverterDialog { - - @Nullable - private String initialFromValue; - - public NumeralBaseConverterDialog(String initialFromValue) { - this.initialFromValue = initialFromValue; - } - - public void show(@NotNull Context context) { - final UnitConverterViewBuilder b = new UnitConverterViewBuilder(); - 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, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); - } catch (CalculatorParseException e) { - b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); - } - } else { - b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); - } - - b.setConverter(AndroidNumeralBase.getConverter()); - - final MutableObject alertDialogHolder = new MutableObject(); - b.setOkButtonOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final AlertDialog alertDialog = alertDialogHolder.getObject(); - if (alertDialog != null) { - alertDialog.dismiss(); - } - } - }); - - b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() { - @Override - public void onClick(@NotNull Unit fromUnits, @NotNull Unit toUnits) { - String toUnitsValue = toUnits.getValue(); - - if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) { - toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue; - } - - CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false); - final AlertDialog alertDialog = alertDialogHolder.getObject(); - if (alertDialog != null) { - alertDialog.dismiss(); - } - } - })); - - final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); - alertBuilder.setView(b.build(context)); - alertBuilder.setTitle(R.string.c_conversion_tool); - - final AlertDialog alertDialog = alertBuilder.create(); - - final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); - lp.copyFrom(alertDialog.getWindow().getAttributes()); - - lp.width = WindowManager.LayoutParams.FILL_PARENT; - lp.height = WindowManager.LayoutParams.WRAP_CONTENT; - - alertDialogHolder.setObject(alertDialog); - alertDialog.show(); - alertDialog.getWindow().setAttributes(lp); - } -} +package org.solovyev.android.calculator.view; + +import android.app.AlertDialog; +import android.content.Context; +import android.view.View; +import android.view.WindowManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.math.units.Unit; +import org.solovyev.math.units.UnitImpl; +import org.solovyev.android.calculator.AndroidNumeralBase; +import org.solovyev.android.calculator.CalculatorModel; +import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.model.CalculatorEngine; +import org.solovyev.android.calculator.model.CalculatorParseException; +import org.solovyev.android.calculator.model.ToJsclTextProcessor; +import org.solovyev.common.MutableObject; +import org.solovyev.common.text.StringUtils; + +import java.util.Arrays; + +/** + * User: serso + * Date: 4/22/12 + * Time: 12:20 AM + */ +public class NumeralBaseConverterDialog { + + @Nullable + private String initialFromValue; + + public NumeralBaseConverterDialog(String initialFromValue) { + this.initialFromValue = initialFromValue; + } + + public void show(@NotNull Context context) { + final UnitConverterViewBuilder b = new UnitConverterViewBuilder(); + 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, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); + } catch (CalculatorParseException e) { + b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); + } + } else { + b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); + } + + b.setConverter(AndroidNumeralBase.getConverter()); + + final MutableObject alertDialogHolder = new MutableObject(); + b.setOkButtonOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final AlertDialog alertDialog = alertDialogHolder.getObject(); + if (alertDialog != null) { + alertDialog.dismiss(); + } + } + }); + + b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() { + @Override + public void onClick(@NotNull Unit fromUnits, @NotNull Unit toUnits) { + String toUnitsValue = toUnits.getValue(); + + if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) { + toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue; + } + + CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false); + final AlertDialog alertDialog = alertDialogHolder.getObject(); + if (alertDialog != null) { + alertDialog.dismiss(); + } + } + })); + + final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); + alertBuilder.setView(b.build(context)); + alertBuilder.setTitle(R.string.c_conversion_tool); + + final AlertDialog alertDialog = alertBuilder.create(); + + final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); + lp.copyFrom(alertDialog.getWindow().getAttributes()); + + lp.width = WindowManager.LayoutParams.FILL_PARENT; + lp.height = WindowManager.LayoutParams.WRAP_CONTENT; + + alertDialogHolder.setObject(alertDialog); + alertDialog.show(); + alertDialog.getWindow().setAttributes(lp); + } +} diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java index 4113d63e..d75c7240 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/view/UnitConverterViewBuilder.java @@ -1,259 +1,259 @@ -package org.solovyev.android.calculator.view; - -import android.app.Activity; -import android.content.Context; -import android.text.ClipboardManager; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.View; -import android.view.ViewGroup; -import android.widget.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.solovyev.android.Unit; -import org.solovyev.android.UnitConverter; -import org.solovyev.android.UnitImpl; -import org.solovyev.android.UnitType; -import org.solovyev.android.calculator.R; -import org.solovyev.android.view.ViewBuilder; -import org.solovyev.android.view.ViewFromLayoutBuilder; -import org.solovyev.common.text.StringUtils; - -import java.util.Collections; -import java.util.List; - -/** - * User: serso - * Date: 4/20/12 - * Time: 4:50 PM - */ -public class UnitConverterViewBuilder implements ViewBuilder { - - @NotNull - private List> fromUnitTypes = Collections.emptyList(); - - @NotNull - private List> toUnitTypes = Collections.emptyList(); - - @Nullable - private Unit fromValue; - - @NotNull - private UnitConverter converter = UnitConverter.Dummy.getInstance(); - - @Nullable - private View.OnClickListener okButtonOnClickListener; - - @Nullable - private CustomButtonData customButtonData; - - public void setFromUnitTypes(@NotNull List> fromUnitTypes) { - this.fromUnitTypes = fromUnitTypes; - } - - public void setToUnitTypes(@NotNull List> toUnitTypes) { - this.toUnitTypes = toUnitTypes; - } - - public void setFromValue(@Nullable Unit fromValue) { - this.fromValue = fromValue; - } - - public void setConverter(@NotNull UnitConverter converter) { - this.converter = converter; - } - - public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) { - this.okButtonOnClickListener = okButtonOnClickListener; - } - - public void setCustomButtonData(@Nullable CustomButtonData customButtonData) { - this.customButtonData = customButtonData; - } - - @NotNull - @Override - public View build(@NotNull final Context context) { - final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context); - - final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); - final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); - fromEditText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - doConversion(main, context, UnitConverterViewBuilder.this.converter); - } - }); - - fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes); - fillSpinner(main, context, R.id.unit_types_to, toUnitTypes); - - if (fromValue != null) { - fromEditText.setText(fromValue.getValue()); - - int i = fromUnitTypes.indexOf(fromValue.getUnitType()); - if ( i >= 0 ) { - fromSpinner.setSelection(i); - } - } - - final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button); - copyButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final EditText toEditText = (EditText) main.findViewById(R.id.units_to); - - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(toEditText.getText().toString()); - Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show(); - } - }); - - final Button okButton = (Button) main.findViewById(R.id.unit_converter_ok_button); - if ( okButtonOnClickListener == null ) { - ((ViewGroup) okButton.getParent()).removeView(okButton); - } else { - okButton.setOnClickListener(this.okButtonOnClickListener); - } - - final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button); - if ( customButtonData == null ) { - ((ViewGroup) customButton.getParent()).removeView(customButton); - } else { - customButton.setText(customButtonData.text); - customButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main)); - } - }); - } - - - - return main; - } - - private void fillSpinner(@NotNull final View main, - @NotNull final Context context, - final int spinnerId, - @NotNull List> unitTypes) { - final Spinner spinner = (Spinner) main.findViewById(spinnerId); - - final ArrayAdapter> adapter = new ArrayAdapter>(context, android.R.layout.simple_spinner_item); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - for (UnitType fromUnitType : unitTypes) { - adapter.add(fromUnitType); - } - spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - doConversion(main, context, UnitConverterViewBuilder.this.converter); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); - spinner.setAdapter(adapter); - } - - private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter converter) { - final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); - - final EditText toEditText = (EditText) main.findViewById(R.id.units_to); - - final String from = fromEditText.getText().toString(); - try { - toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main))); - } catch (ConversionException e) { - toEditText.setText(context.getString(R.string.c_error)); - } - } - - public static final class ConversionException extends Exception { - private ConversionException() { - } - - private ConversionException(Throwable throwable) { - super(throwable); - } - } - - @NotNull - public static String doConversion(@NotNull UnitConverter converter, - @Nullable String from, - @NotNull UnitType fromUnitType, - @NotNull UnitType toUnitType) throws ConversionException{ - final String result; - - if (StringUtils.isEmpty(from)) { - result = ""; - } else { - - String to = null; - try { - if (converter.isSupported(fromUnitType, toUnitType)) { - to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue(); - } - } catch (RuntimeException e) { - throw new ConversionException(e); - } - - result = to; - } - - return result; - } - - @NotNull - private static Unit getToUnit(@NotNull View main) { - final EditText toUnits = (EditText) main.findViewById(R.id.units_to); - return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main)); - } - - @NotNull - private static UnitType getToUnitType(@NotNull View main) { - final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to); - return (UnitType) toSpinner.getSelectedItem(); - } - - @NotNull - private static Unit getFromUnit(@NotNull View main) { - final EditText fromUnits = (EditText) main.findViewById(R.id.units_from); - return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main)); - } - - @NotNull - private static UnitType getFromUnitType(@NotNull View main) { - final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); - return (UnitType) fromSpinner.getSelectedItem(); - } - - public static class CustomButtonData { - - @NotNull - private String text; - - @NotNull - private CustomButtonOnClickListener clickListener; - - - public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) { - this.text = text; - this.clickListener = clickListener; - } - } - - public static interface CustomButtonOnClickListener { - void onClick(@NotNull Unit fromUnits, @NotNull Unit toUnits); - } -} +package org.solovyev.android.calculator.view; + +import android.app.Activity; +import android.content.Context; +import android.text.ClipboardManager; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.solovyev.math.units.Unit; +import org.solovyev.math.units.UnitConverter; +import org.solovyev.math.units.UnitImpl; +import org.solovyev.math.units.UnitType; +import org.solovyev.android.calculator.R; +import org.solovyev.android.view.ViewBuilder; +import org.solovyev.android.view.ViewFromLayoutBuilder; +import org.solovyev.common.text.StringUtils; + +import java.util.Collections; +import java.util.List; + +/** + * User: serso + * Date: 4/20/12 + * Time: 4:50 PM + */ +public class UnitConverterViewBuilder implements ViewBuilder { + + @NotNull + private List> fromUnitTypes = Collections.emptyList(); + + @NotNull + private List> toUnitTypes = Collections.emptyList(); + + @Nullable + private Unit fromValue; + + @NotNull + private UnitConverter converter = UnitConverter.Dummy.getInstance(); + + @Nullable + private View.OnClickListener okButtonOnClickListener; + + @Nullable + private CustomButtonData customButtonData; + + public void setFromUnitTypes(@NotNull List> fromUnitTypes) { + this.fromUnitTypes = fromUnitTypes; + } + + public void setToUnitTypes(@NotNull List> toUnitTypes) { + this.toUnitTypes = toUnitTypes; + } + + public void setFromValue(@Nullable Unit fromValue) { + this.fromValue = fromValue; + } + + public void setConverter(@NotNull UnitConverter converter) { + this.converter = converter; + } + + public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) { + this.okButtonOnClickListener = okButtonOnClickListener; + } + + public void setCustomButtonData(@Nullable CustomButtonData customButtonData) { + this.customButtonData = customButtonData; + } + + @NotNull + @Override + public View build(@NotNull final Context context) { + final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context); + + final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); + final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); + fromEditText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + doConversion(main, context, UnitConverterViewBuilder.this.converter); + } + }); + + fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes); + fillSpinner(main, context, R.id.unit_types_to, toUnitTypes); + + if (fromValue != null) { + fromEditText.setText(fromValue.getValue()); + + int i = fromUnitTypes.indexOf(fromValue.getUnitType()); + if ( i >= 0 ) { + fromSpinner.setSelection(i); + } + } + + final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button); + copyButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText toEditText = (EditText) main.findViewById(R.id.units_to); + + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(toEditText.getText().toString()); + Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show(); + } + }); + + final Button okButton = (Button) main.findViewById(R.id.unit_converter_ok_button); + if ( okButtonOnClickListener == null ) { + ((ViewGroup) okButton.getParent()).removeView(okButton); + } else { + okButton.setOnClickListener(this.okButtonOnClickListener); + } + + final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button); + if ( customButtonData == null ) { + ((ViewGroup) customButton.getParent()).removeView(customButton); + } else { + customButton.setText(customButtonData.text); + customButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main)); + } + }); + } + + + + return main; + } + + private void fillSpinner(@NotNull final View main, + @NotNull final Context context, + final int spinnerId, + @NotNull List> unitTypes) { + final Spinner spinner = (Spinner) main.findViewById(spinnerId); + + final ArrayAdapter> adapter = new ArrayAdapter>(context, android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + for (UnitType fromUnitType : unitTypes) { + adapter.add(fromUnitType); + } + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + doConversion(main, context, UnitConverterViewBuilder.this.converter); + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + spinner.setAdapter(adapter); + } + + private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter converter) { + final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); + + final EditText toEditText = (EditText) main.findViewById(R.id.units_to); + + final String from = fromEditText.getText().toString(); + try { + toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main))); + } catch (ConversionException e) { + toEditText.setText(context.getString(R.string.c_error)); + } + } + + public static final class ConversionException extends Exception { + private ConversionException() { + } + + private ConversionException(Throwable throwable) { + super(throwable); + } + } + + @NotNull + public static String doConversion(@NotNull UnitConverter converter, + @Nullable String from, + @NotNull UnitType fromUnitType, + @NotNull UnitType toUnitType) throws ConversionException{ + final String result; + + if (StringUtils.isEmpty(from)) { + result = ""; + } else { + + String to = null; + try { + if (converter.isSupported(fromUnitType, toUnitType)) { + to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue(); + } + } catch (RuntimeException e) { + throw new ConversionException(e); + } + + result = to; + } + + return result; + } + + @NotNull + private static Unit getToUnit(@NotNull View main) { + final EditText toUnits = (EditText) main.findViewById(R.id.units_to); + return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main)); + } + + @NotNull + private static UnitType getToUnitType(@NotNull View main) { + final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to); + return (UnitType) toSpinner.getSelectedItem(); + } + + @NotNull + private static Unit getFromUnit(@NotNull View main) { + final EditText fromUnits = (EditText) main.findViewById(R.id.units_from); + return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main)); + } + + @NotNull + private static UnitType getFromUnitType(@NotNull View main) { + final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); + return (UnitType) fromSpinner.getSelectedItem(); + } + + public static class CustomButtonData { + + @NotNull + private String text; + + @NotNull + private CustomButtonOnClickListener clickListener; + + + public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) { + this.text = text; + this.clickListener = clickListener; + } + } + + public static interface CustomButtonOnClickListener { + void onClick(@NotNull Unit fromUnits, @NotNull Unit toUnits); + } +} diff --git a/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java b/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java index fc198068..5ad54751 100644 --- a/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java +++ b/calculatorpp/src/test/java/org/solovyev/android/AndroidNumeralBaseTest.java @@ -1,70 +1,72 @@ -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(); - } -} +package org.solovyev.android; + +import junit.framework.Assert; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; +import org.solovyev.android.calculator.AndroidNumeralBase; +import org.solovyev.math.units.Unit; +import org.solovyev.math.units.UnitConverter; + +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/pom.xml b/pom.xml index a4352372..93bf95b9 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ calculatorpp calculatorpp-service calculatorpp-test + calculatorpp-core @@ -81,7 +82,13 @@ junit junit - 4.8.1 + 4.8.2 + + + + com.intellij + annotations + 7.0.3