Calculator++ core module

This commit is contained in:
serso 2012-09-20 12:07:55 +04:00
parent f909b49c7c
commit 33db715776
12 changed files with 1140 additions and 1091 deletions

36
calculatorpp-core/pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp-parent</artifactId>
<version>1.3.1</version>
</parent>
<groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp-core</artifactId>
<version>1.3.1</version>
<name>Calculator++ Application Core</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>annotations</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,17 +1,17 @@
package org.solovyev.android; package org.solovyev.math.units;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* User: serso * User: serso
* Date: 4/21/12 * Date: 4/21/12
* Time: 7:54 PM * Time: 7:54 PM
*/ */
public interface Unit<V> { public interface Unit<V> {
@NotNull @NotNull
V getValue(); V getValue();
@NotNull @NotNull
UnitType<V> getUnitType(); UnitType<V> getUnitType();
} }

View File

@ -1,42 +1,42 @@
package org.solovyev.android; package org.solovyev.math.units;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* User: serso * User: serso
* Date: 4/21/12 * Date: 4/21/12
* Time: 7:53 PM * Time: 7:53 PM
*/ */
public interface UnitConverter<T> { public interface UnitConverter<T> {
boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<T> to); boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<T> to);
@NotNull @NotNull
Unit<T> convert(@NotNull Unit<?> from, @NotNull UnitType<T> toType); Unit<T> convert(@NotNull Unit<?> from, @NotNull UnitType<T> toType);
public static class Dummy implements UnitConverter<Object> { public static class Dummy implements UnitConverter<Object> {
@NotNull @NotNull
private static final Dummy instance = new Dummy(); private static final Dummy instance = new Dummy();
@NotNull @NotNull
public static <T> UnitConverter<T> getInstance() { public static <T> UnitConverter<T> getInstance() {
return (UnitConverter<T>)instance; return (UnitConverter<T>)instance;
} }
private Dummy() { private Dummy() {
} }
@Override @Override
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<Object> to) { public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<Object> to) {
return false; return false;
} }
@NotNull @NotNull
@Override @Override
public Unit<Object> convert(@NotNull Unit<?> from, @NotNull UnitType<Object> toType) { public Unit<Object> convert(@NotNull Unit<?> from, @NotNull UnitType<Object> toType) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
} }

View File

@ -1,42 +1,42 @@
package org.solovyev.android; package org.solovyev.math.units;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* User: serso * User: serso
* Date: 4/21/12 * Date: 4/21/12
* Time: 8:01 PM * Time: 8:01 PM
*/ */
public class UnitImpl<V> implements Unit<V> { public class UnitImpl<V> implements Unit<V> {
@NotNull @NotNull
private V value; private V value;
@NotNull @NotNull
private UnitType<V> unitType; private UnitType<V> unitType;
private UnitImpl() { private UnitImpl() {
} }
@NotNull @NotNull
public static <V> Unit<V> newInstance(@NotNull V value, @NotNull UnitType<V> unitType) { public static <V> Unit<V> newInstance(@NotNull V value, @NotNull UnitType<V> unitType) {
final UnitImpl<V> result = new UnitImpl<V>(); final UnitImpl<V> result = new UnitImpl<V>();
result.value = value; result.value = value;
result.unitType = unitType; result.unitType = unitType;
return result; return result;
} }
@NotNull @NotNull
@Override @Override
public V getValue() { public V getValue() {
return this.value; return this.value;
} }
@NotNull @NotNull
@Override @Override
public UnitType<V> getUnitType() { public UnitType<V> getUnitType() {
return unitType; return unitType;
} }
} }

View File

@ -1,16 +1,16 @@
package org.solovyev.android; package org.solovyev.math.units;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* User: serso * User: serso
* Date: 4/21/12 * Date: 4/21/12
* Time: 7:55 PM * Time: 7:55 PM
*/ */
public interface UnitType<V> { public interface UnitType<V> {
@NotNull @NotNull
Class<V> getUnitValueClass(); Class<V> getUnitValueClass();
boolean equals(@NotNull Object o); boolean equals(@NotNull Object o);
} }

View File

@ -1,36 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<groupId>org.solovyev.android</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp-parent</artifactId> <artifactId>calculatorpp-parent</artifactId>
<version>1.3.1</version> <version>1.3.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.solovyev.android</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp-service</artifactId> <artifactId>calculatorpp-service</artifactId>
<version>0.1</version> <version>0.1</version>
<packaging>apklib</packaging> <packaging>apklib</packaging>
<name>Calculator++ Service</name> <name>Calculator++ Service</name>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.intellij</groupId> <groupId>com.intellij</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>7.0.3</version> </dependency>
</dependency>
<dependency>
<dependency> <groupId>com.google.android</groupId>
<groupId>com.google.android</groupId> <artifactId>android</artifactId>
<artifactId>android</artifactId> <scope>provided</scope>
<scope>provided</scope> </dependency>
</dependency>
</dependencies>
</dependencies>
</project> </project>

View File

@ -1,353 +1,358 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<groupId>org.solovyev.android</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp-parent</artifactId> <artifactId>calculatorpp-parent</artifactId>
<version>1.3.1</version> <version>1.3.1</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.solovyev.android</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>calculatorpp</artifactId> <artifactId>calculatorpp</artifactId>
<packaging>apk</packaging> <packaging>apk</packaging>
<name>Calculator++ Application</name> <name>Calculator++ Application</name>
<dependencies> <dependencies>
<!-- OWN --> <!-- OWN -->
<dependency>
<groupId>org.solovyev</groupId> <dependency>
<artifactId>common-core</artifactId> <groupId>org.solovyev.android</groupId>
</dependency> <artifactId>calculatorpp-core</artifactId>
<version>1.3.1</version>
<dependency> </dependency>
<groupId>org.solovyev</groupId>
<artifactId>common-text</artifactId> <dependency>
</dependency> <groupId>org.solovyev</groupId>
<artifactId>common-core</artifactId>
<dependency> </dependency>
<groupId>org.solovyev.android</groupId>
<artifactId>android-common-core</artifactId> <dependency>
<type>apklib</type> <groupId>org.solovyev</groupId>
</dependency> <artifactId>common-text</artifactId>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>android-common-ads</artifactId> <groupId>org.solovyev.android</groupId>
<type>apklib</type> <artifactId>android-common-core</artifactId>
</dependency> <type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>android-common-view</artifactId> <groupId>org.solovyev.android</groupId>
<type>apklib</type> <artifactId>android-common-ads</artifactId>
</dependency> <type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>android-common-preferences</artifactId> <groupId>org.solovyev.android</groupId>
<type>apklib</type> <artifactId>android-common-view</artifactId>
</dependency> <type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>android-common-other</artifactId> <groupId>org.solovyev.android</groupId>
<type>apklib</type> <artifactId>android-common-preferences</artifactId>
</dependency> <type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>android-common-menu</artifactId> <groupId>org.solovyev.android</groupId>
<type>apklib</type> <artifactId>android-common-other</artifactId>
</dependency> <type>apklib</type>
</dependency>
<dependency>
<groupId>org.solovyev.android</groupId> <dependency>
<artifactId>calculatorpp-service</artifactId> <groupId>org.solovyev.android</groupId>
<version>0.1</version> <artifactId>android-common-menu</artifactId>
<type>apklib</type> <type>apklib</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.solovyev</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>jscl</artifactId> <artifactId>calculatorpp-service</artifactId>
<version>0.0.2</version> <version>0.1</version>
<exclusions> <type>apklib</type>
<exclusion> </dependency>
<artifactId>xercesImpl</artifactId>
<groupId>xerces</groupId> <dependency>
</exclusion> <groupId>org.solovyev</groupId>
</exclusions> <artifactId>jscl</artifactId>
</dependency> <version>0.0.2</version>
<exclusions>
<!--OTHER--> <exclusion>
<artifactId>xercesImpl</artifactId>
<dependency> <groupId>xerces</groupId>
<groupId>com.google.android</groupId> </exclusion>
<artifactId>android</artifactId> </exclusions>
<scope>provided</scope> </dependency>
</dependency>
<!--OTHER-->
<dependency>
<groupId>junit</groupId> <dependency>
<artifactId>junit</artifactId> <groupId>com.google.android</groupId>
<version>4.8.2</version> <artifactId>android</artifactId>
<scope>test</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.sf.opencsv</groupId> <groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId> <artifactId>opencsv</artifactId>
<version>2.0</version> <version>2.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.simpleframework</groupId> <groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId> <artifactId>simple-xml</artifactId>
<version>2.6.1</version> <version>2.6.1</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>stax-api</artifactId> <artifactId>stax-api</artifactId>
<groupId>stax</groupId> <groupId>stax</groupId>
</exclusion> </exclusion>
<exclusion> <exclusion>
<artifactId>xpp3</artifactId> <artifactId>xpp3</artifactId>
<groupId>xpp3</groupId> <groupId>xpp3</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>achartengine</groupId> <groupId>achartengine</groupId>
<artifactId>achartengine</artifactId> <artifactId>achartengine</artifactId>
<version>0.7.0</version> <version>0.7.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>admob</groupId> <groupId>admob</groupId>
<artifactId>admob</artifactId> <artifactId>admob</artifactId>
<version>6.1.0</version> <version>6.1.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.solovyev.android</groupId> <groupId>org.solovyev.android</groupId>
<artifactId>billing</artifactId> <artifactId>billing</artifactId>
<version>0.1</version> <version>0.1</version>
<!--<type>apklib</type>--> <!--<type>apklib</type>-->
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>11.0.2</version> <version>11.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.intellij</groupId> <groupId>junit</groupId>
<artifactId>annotations</artifactId> <artifactId>junit</artifactId>
<version>7.0.3</version> <scope>test</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>com.intellij</groupId>
<build> <artifactId>annotations</artifactId>
</dependency>
<plugins> </dependencies>
<plugin> <build>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<extensions>true</extensions> <plugins>
<configuration>
<manifest> <plugin>
<debuggable>true</debuggable> <groupId>com.jayway.maven.plugins.android.generation2</groupId>
</manifest> <artifactId>android-maven-plugin</artifactId>
</configuration> <extensions>true</extensions>
<executions> <configuration>
<execution> <manifest>
<id>manifestUpdate</id> <debuggable>true</debuggable>
<phase>process-resources</phase> </manifest>
<goals> </configuration>
<goal>manifest-update</goal> <executions>
</goals> <execution>
</execution> <id>manifestUpdate</id>
<execution> <phase>process-resources</phase>
<id>alignApk</id> <goals>
<phase>package</phase> <goal>manifest-update</goal>
<goals> </goals>
<goal>zipalign</goal> </execution>
</goals> <execution>
</execution> <id>alignApk</id>
</executions> <phase>package</phase>
</plugin> <goals>
<goal>zipalign</goal>
</plugins> </goals>
</execution>
</build> </executions>
</plugin>
<profiles>
</plugins>
<profile>
<id>release</id> </build>
<!-- via this activation the profile is automatically used when the release is done with the maven release
plugin --> <profiles>
<activation>
<property> <profile>
<name>performRelease</name> <id>release</id>
<value>true</value> <!-- via this activation the profile is automatically used when the release is done with the maven release
</property> plugin -->
</activation> <activation>
<property>
<build> <name>performRelease</name>
<plugins> <value>true</value>
</property>
<plugin> </activation>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId> <build>
<plugins>
<executions>
<execution> <plugin>
<id>alignApk</id> <groupId>com.jayway.maven.plugins.android.generation2</groupId>
<phase>package</phase> <artifactId>android-maven-plugin</artifactId>
<goals>
<goal>zipalign</goal> <executions>
</goals> <execution>
</execution> <id>alignApk</id>
</executions> <phase>package</phase>
<goals>
</plugin> <goal>zipalign</goal>
</goals>
<plugin> </execution>
<groupId>org.codehaus.mojo</groupId> </executions>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version> </plugin>
<executions>
<execution> <plugin>
<phase>initialize</phase> <groupId>org.codehaus.mojo</groupId>
<goals> <artifactId>properties-maven-plugin</artifactId>
<goal>read-project-properties</goal> <version>1.0-alpha-2</version>
</goals> <executions>
<configuration> <execution>
<files> <phase>initialize</phase>
<file>${project.basedir}/misc/env/jarsigner.properties</file> <goals>
</files> <goal>read-project-properties</goal>
</configuration> </goals>
</execution> <configuration>
</executions> <files>
</plugin> <file>${project.basedir}/misc/env/jarsigner.properties</file>
</files>
<plugin> </configuration>
<groupId>org.apache.maven.plugins</groupId> </execution>
<artifactId>maven-jarsigner-plugin</artifactId> </executions>
<executions> </plugin>
<execution>
<id>signing</id> <plugin>
<goals> <groupId>org.apache.maven.plugins</groupId>
<goal>sign</goal> <artifactId>maven-jarsigner-plugin</artifactId>
<goal>verify</goal> <executions>
</goals> <execution>
<phase>package</phase> <id>signing</id>
<inherited>true</inherited> <goals>
<configuration> <goal>sign</goal>
<removeExistingSignatures>true</removeExistingSignatures> <goal>verify</goal>
<archiveDirectory/> </goals>
<includes> <phase>package</phase>
<include>${project.build.directory}/${project.artifactId}-${project.version}.apk</include> <inherited>true</inherited>
</includes> <configuration>
<keystore>${sign.keystore}</keystore> <removeExistingSignatures>true</removeExistingSignatures>
<alias>${sign.alias}</alias> <archiveDirectory/>
<storepass>${sign.storepass}</storepass> <includes>
<keypass>${sign.keypass}</keypass> <include>${project.build.directory}/${project.artifactId}-${project.version}.apk</include>
<verbose>false</verbose> </includes>
</configuration> <keystore>${sign.keystore}</keystore>
</execution> <alias>${sign.alias}</alias>
</executions> <storepass>${sign.storepass}</storepass>
</plugin> <keypass>${sign.keypass}</keypass>
<verbose>false</verbose>
<!-- the signed apk then needs to be zipaligned and we activate proguard and we run the manifest </configuration>
update --> </execution>
<plugin> </executions>
<groupId>com.jayway.maven.plugins.android.generation2</groupId> </plugin>
<artifactId>android-maven-plugin</artifactId>
<inherited>true</inherited> <!-- the signed apk then needs to be zipaligned and we activate proguard and we run the manifest
<configuration> update -->
<plugin>
<sign> <groupId>com.jayway.maven.plugins.android.generation2</groupId>
<debug>false</debug> <artifactId>android-maven-plugin</artifactId>
</sign> <inherited>true</inherited>
<configuration>
<zipalign>
<verbose>false</verbose> <sign>
<inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk> <debug>false</debug>
<outputApk>${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk</outputApk> </sign>
</zipalign>
<zipalign>
<manifest> <verbose>false</verbose>
<debuggable>false</debuggable> <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>
<versionCodeAutoIncrement>true</versionCodeAutoIncrement> <outputApk>${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk</outputApk>
</manifest> </zipalign>
<proguard> <manifest>
<skip>true</skip> <debuggable>false</debuggable>
</proguard> <versionCodeAutoIncrement>true</versionCodeAutoIncrement>
</configuration> </manifest>
<executions> <proguard>
<execution> <skip>true</skip>
<id>manifestUpdate</id> </proguard>
<phase>process-resources</phase> </configuration>
<goals>
<goal>manifest-update</goal> <executions>
</goals> <execution>
</execution> <id>manifestUpdate</id>
<execution> <phase>process-resources</phase>
<id>alignApk</id> <goals>
<phase>package</phase> <goal>manifest-update</goal>
<goals> </goals>
<goal>zipalign</goal> </execution>
</goals> <execution>
</execution> <id>alignApk</id>
</executions> <phase>package</phase>
</plugin> <goals>
<goal>zipalign</goal>
<plugin> </goals>
<groupId>org.codehaus.mojo</groupId> </execution>
<artifactId>build-helper-maven-plugin</artifactId> </executions>
<configuration> </plugin>
<artifacts>
<artifact> <plugin>
<file>${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk</file> <groupId>org.codehaus.mojo</groupId>
<type>apk</type> <artifactId>build-helper-maven-plugin</artifactId>
<classifier>signed-aligned</classifier> <configuration>
</artifact> <artifacts>
<artifact> <artifact>
<file>${project.build.directory}/proguard/mapping.txt</file> <file>${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk</file>
<type>map</type> <type>apk</type>
<classifier>release</classifier> <classifier>signed-aligned</classifier>
</artifact> </artifact>
</artifacts> <artifact>
</configuration> <file>${project.build.directory}/proguard/mapping.txt</file>
<executions> <type>map</type>
<execution> <classifier>release</classifier>
<id>attach-signed-aligned</id> </artifact>
<phase>package</phase> </artifacts>
<goals> </configuration>
<goal>attach-artifact</goal> <executions>
</goals> <execution>
</execution> <id>attach-signed-aligned</id>
</executions> <phase>package</phase>
</plugin> <goals>
<goal>attach-artifact</goal>
</plugins> </goals>
</build> </execution>
</profile> </executions>
</profiles> </plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -1,158 +1,158 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import jscl.NumeralBase; import jscl.NumeralBase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.Unit; import org.solovyev.math.units.Unit;
import org.solovyev.android.UnitConverter; import org.solovyev.math.units.UnitConverter;
import org.solovyev.android.UnitImpl; import org.solovyev.math.units.UnitImpl;
import org.solovyev.android.UnitType; import org.solovyev.math.units.UnitType;
import org.solovyev.android.view.drag.DirectionDragButton; import org.solovyev.android.view.drag.DirectionDragButton;
import org.solovyev.android.view.drag.DragDirection; import org.solovyev.android.view.drag.DragDirection;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* User: serso * User: serso
* Date: 4/21/12 * Date: 4/21/12
* Time: 8:00 PM * Time: 8:00 PM
*/ */
public enum AndroidNumeralBase implements UnitType<String> { public enum AndroidNumeralBase implements UnitType<String> {
bin(NumeralBase.bin) { bin(NumeralBase.bin) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
return Arrays.asList(R.id.zeroDigitButton, R.id.oneDigitButton); return Arrays.asList(R.id.zeroDigitButton, R.id.oneDigitButton);
} }
}, },
oct(NumeralBase.oct) { oct(NumeralBase.oct) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
final List<Integer> result = new ArrayList<Integer>(bin.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)); result.addAll(Arrays.asList(R.id.twoDigitButton, R.id.threeDigitButton, R.id.fourDigitButton, R.id.fiveDigitButton, R.id.sixDigitButton, R.id.sevenDigitButton));
return result; return result;
} }
}, },
dec(NumeralBase.dec) { dec(NumeralBase.dec) {
@NotNull @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
final List<Integer> result = new ArrayList<Integer>(oct.getButtonIds()); final List<Integer> result = new ArrayList<Integer>(oct.getButtonIds());
result.addAll(Arrays.asList(R.id.eightDigitButton, R.id.nineDigitButton)); result.addAll(Arrays.asList(R.id.eightDigitButton, R.id.nineDigitButton));
return result; return result;
} }
}, },
hex(NumeralBase.hex) { hex(NumeralBase.hex) {
@NotNull @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); 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 @NotNull
@Override @Override
public List<Integer> getButtonIds() { public List<Integer> getButtonIds() {
return dec.getButtonIds(); return dec.getButtonIds();
} }
@Override @Override
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) { protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
super.toggleButton(show, button); super.toggleButton(show, button);
if (specialHexButtonIds.contains(button.getId())) { if (specialHexButtonIds.contains(button.getId())) {
button.showDirectionText(show, DragDirection.left); button.showDirectionText(show, DragDirection.left);
button.invalidate(); button.invalidate();
} }
} }
}; };
@NotNull @NotNull
private final NumeralBase numeralBase; private final NumeralBase numeralBase;
private AndroidNumeralBase(@NotNull NumeralBase numeralBase) { private AndroidNumeralBase(@NotNull NumeralBase numeralBase) {
this.numeralBase = numeralBase; this.numeralBase = numeralBase;
} }
@NotNull @NotNull
public Unit<String> createUnit(@NotNull String value) { public Unit<String> createUnit(@NotNull String value) {
return UnitImpl.newInstance(value, this); return UnitImpl.newInstance(value, this);
} }
@NotNull @NotNull
public abstract List<Integer> getButtonIds(); public abstract List<Integer> getButtonIds();
public void toggleButtons(boolean show, @NotNull Activity activity) { public void toggleButtons(boolean show, @NotNull Activity activity) {
for (Integer buttonId : getButtonIds()) { for (Integer buttonId : getButtonIds()) {
final DirectionDragButton button = (DirectionDragButton) activity.findViewById(buttonId); final DirectionDragButton button = (DirectionDragButton) activity.findViewById(buttonId);
if (button != null) { if (button != null) {
toggleButton(show, button); toggleButton(show, button);
} }
} }
} }
protected void toggleButton(boolean show, @NotNull DirectionDragButton button) { protected void toggleButton(boolean show, @NotNull DirectionDragButton button) {
button.setShowText(show); button.setShowText(show);
} }
@NotNull @NotNull
public NumeralBase getNumeralBase() { public NumeralBase getNumeralBase() {
return numeralBase; return numeralBase;
} }
@NotNull @NotNull
@Override @Override
public Class<String> getUnitValueClass() { public Class<String> getUnitValueClass() {
return String.class; return String.class;
} }
@NotNull @NotNull
private static final Converter converter = new Converter(); private static final Converter converter = new Converter();
@NotNull @NotNull
public static Converter getConverter() { public static Converter getConverter() {
return converter; return converter;
} }
public static class Converter implements UnitConverter<String> { public static class Converter implements UnitConverter<String> {
private Converter() { private Converter() {
} }
@Override @Override
public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) { public boolean isSupported(@NotNull UnitType<?> from, @NotNull UnitType<String> to) {
return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass()); return AndroidNumeralBase.class.isAssignableFrom(from.getClass()) && AndroidNumeralBase.class.isAssignableFrom(to.getClass());
} }
@NotNull @NotNull
@Override @Override
public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) { public Unit<String> convert(@NotNull Unit<?> from, @NotNull UnitType<String> toType) {
if (!isSupported(from.getUnitType(), toType)) { if (!isSupported(from.getUnitType(), toType)) {
throw new IllegalArgumentException("Types are not supported!"); throw new IllegalArgumentException("Types are not supported!");
} }
final AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType(); final AndroidNumeralBase fromTypeAndroid = (AndroidNumeralBase) from.getUnitType();
final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase; final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase;
final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase; final NumeralBase toNumeralBase = ((AndroidNumeralBase) toType).numeralBase;
final String fromValue = (String) from.getValue(); final String fromValue = (String) from.getValue();
final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue); final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue);
return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType); return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), (AndroidNumeralBase) toType);
} }
} }
@NotNull @NotNull
public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) { public static AndroidNumeralBase valueOf(@NotNull NumeralBase nb) {
for (AndroidNumeralBase androidNumeralBase : values()) { for (AndroidNumeralBase androidNumeralBase : values()) {
if (androidNumeralBase.numeralBase == nb) { if (androidNumeralBase.numeralBase == nb) {
return androidNumeralBase; return androidNumeralBase;
} }
} }
throw new IllegalArgumentException(nb + " is not supported numeral base!"); throw new IllegalArgumentException(nb + " is not supported numeral base!");
} }
} }

View File

@ -1,99 +1,99 @@
package org.solovyev.android.calculator.view; package org.solovyev.android.calculator.view;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.Unit; import org.solovyev.math.units.Unit;
import org.solovyev.android.UnitImpl; import org.solovyev.math.units.UnitImpl;
import org.solovyev.android.calculator.AndroidNumeralBase; import org.solovyev.android.calculator.AndroidNumeralBase;
import org.solovyev.android.calculator.CalculatorModel; import org.solovyev.android.calculator.CalculatorModel;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.calculator.model.CalculatorEngine;
import org.solovyev.android.calculator.model.CalculatorParseException; import org.solovyev.android.calculator.model.CalculatorParseException;
import org.solovyev.android.calculator.model.ToJsclTextProcessor; import org.solovyev.android.calculator.model.ToJsclTextProcessor;
import org.solovyev.common.MutableObject; import org.solovyev.common.MutableObject;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
import java.util.Arrays; import java.util.Arrays;
/** /**
* User: serso * User: serso
* Date: 4/22/12 * Date: 4/22/12
* Time: 12:20 AM * Time: 12:20 AM
*/ */
public class NumeralBaseConverterDialog { public class NumeralBaseConverterDialog {
@Nullable @Nullable
private String initialFromValue; private String initialFromValue;
public NumeralBaseConverterDialog(String initialFromValue) { public NumeralBaseConverterDialog(String initialFromValue) {
this.initialFromValue = initialFromValue; this.initialFromValue = initialFromValue;
} }
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(AndroidNumeralBase.values())); b.setFromUnitTypes(Arrays.asList(AndroidNumeralBase.values()));
b.setToUnitTypes(Arrays.asList(AndroidNumeralBase.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, AndroidNumeralBase.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, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); b.setFromValue(UnitImpl.newInstance(value, AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
} }
} else { } else {
b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))); b.setFromValue(UnitImpl.newInstance("", AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase())));
} }
b.setConverter(AndroidNumeralBase.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() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final AlertDialog alertDialog = alertDialogHolder.getObject(); final AlertDialog alertDialog = alertDialogHolder.getObject();
if (alertDialog != null) { if (alertDialog != null) {
alertDialog.dismiss(); alertDialog.dismiss();
} }
} }
}); });
b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() { b.setCustomButtonData(new UnitConverterViewBuilder.CustomButtonData(context.getString(R.string.c_use_short), new UnitConverterViewBuilder.CustomButtonOnClickListener() {
@Override @Override
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(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) { if (!toUnits.getUnitType().equals(AndroidNumeralBase.valueOf(CalculatorEngine.instance.getEngine().getNumeralBase()))) {
toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue; toUnitsValue = ((AndroidNumeralBase) toUnits.getUnitType()).getNumeralBase().getJsclPrefix() + toUnitsValue;
} }
CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false); CalculatorModel.instance.processDigitButtonAction(toUnitsValue, false);
final AlertDialog alertDialog = alertDialogHolder.getObject(); final AlertDialog alertDialog = alertDialogHolder.getObject();
if (alertDialog != null) { if (alertDialog != null) {
alertDialog.dismiss(); alertDialog.dismiss();
} }
} }
})); }));
final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setView(b.build(context)); alertBuilder.setView(b.build(context));
alertBuilder.setTitle(R.string.c_conversion_tool); alertBuilder.setTitle(R.string.c_conversion_tool);
final AlertDialog alertDialog = alertBuilder.create(); final AlertDialog alertDialog = alertBuilder.create();
final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes()); lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
alertDialogHolder.setObject(alertDialog); alertDialogHolder.setObject(alertDialog);
alertDialog.show(); alertDialog.show();
alertDialog.getWindow().setAttributes(lp); alertDialog.getWindow().setAttributes(lp);
} }
} }

View File

@ -1,259 +1,259 @@
package org.solovyev.android.calculator.view; package org.solovyev.android.calculator.view;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.*; import android.widget.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.Unit; import org.solovyev.math.units.Unit;
import org.solovyev.android.UnitConverter; import org.solovyev.math.units.UnitConverter;
import org.solovyev.android.UnitImpl; import org.solovyev.math.units.UnitImpl;
import org.solovyev.android.UnitType; import org.solovyev.math.units.UnitType;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.view.ViewBuilder; import org.solovyev.android.view.ViewBuilder;
import org.solovyev.android.view.ViewFromLayoutBuilder; import org.solovyev.android.view.ViewFromLayoutBuilder;
import org.solovyev.common.text.StringUtils; import org.solovyev.common.text.StringUtils;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* User: serso * User: serso
* Date: 4/20/12 * Date: 4/20/12
* Time: 4:50 PM * Time: 4:50 PM
*/ */
public class UnitConverterViewBuilder implements ViewBuilder<View> { public class UnitConverterViewBuilder implements ViewBuilder<View> {
@NotNull @NotNull
private List<? extends UnitType<String>> fromUnitTypes = Collections.emptyList(); private List<? extends UnitType<String>> fromUnitTypes = Collections.emptyList();
@NotNull @NotNull
private List<? extends UnitType<String>> toUnitTypes = Collections.emptyList(); private List<? extends UnitType<String>> toUnitTypes = Collections.emptyList();
@Nullable @Nullable
private Unit<String> fromValue; private Unit<String> fromValue;
@NotNull @NotNull
private UnitConverter<String> converter = UnitConverter.Dummy.getInstance(); private UnitConverter<String> converter = UnitConverter.Dummy.getInstance();
@Nullable @Nullable
private View.OnClickListener okButtonOnClickListener; private View.OnClickListener okButtonOnClickListener;
@Nullable @Nullable
private CustomButtonData customButtonData; private CustomButtonData customButtonData;
public void setFromUnitTypes(@NotNull List<? extends UnitType<String>> fromUnitTypes) { public void setFromUnitTypes(@NotNull List<? extends UnitType<String>> fromUnitTypes) {
this.fromUnitTypes = fromUnitTypes; this.fromUnitTypes = fromUnitTypes;
} }
public void setToUnitTypes(@NotNull List<? extends UnitType<String>> toUnitTypes) { public void setToUnitTypes(@NotNull List<? extends UnitType<String>> toUnitTypes) {
this.toUnitTypes = toUnitTypes; this.toUnitTypes = toUnitTypes;
} }
public void setFromValue(@Nullable Unit<String> fromValue) { public void setFromValue(@Nullable Unit<String> fromValue) {
this.fromValue = fromValue; this.fromValue = fromValue;
} }
public void setConverter(@NotNull UnitConverter<String> converter) { public void setConverter(@NotNull UnitConverter<String> converter) {
this.converter = converter; this.converter = converter;
} }
public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) { public void setOkButtonOnClickListener(@Nullable View.OnClickListener okButtonOnClickListener) {
this.okButtonOnClickListener = okButtonOnClickListener; this.okButtonOnClickListener = okButtonOnClickListener;
} }
public void setCustomButtonData(@Nullable CustomButtonData customButtonData) { public void setCustomButtonData(@Nullable CustomButtonData customButtonData) {
this.customButtonData = customButtonData; this.customButtonData = customButtonData;
} }
@NotNull @NotNull
@Override @Override
public View build(@NotNull final Context context) { public View build(@NotNull final Context context) {
final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context); final View main = ViewFromLayoutBuilder.newInstance(R.layout.unit_converter).build(context);
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
fromEditText.addTextChangedListener(new TextWatcher() { fromEditText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
doConversion(main, context, UnitConverterViewBuilder.this.converter); doConversion(main, context, UnitConverterViewBuilder.this.converter);
} }
}); });
fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes); fillSpinner(main, context, R.id.unit_types_from, fromUnitTypes);
fillSpinner(main, context, R.id.unit_types_to, toUnitTypes); fillSpinner(main, context, R.id.unit_types_to, toUnitTypes);
if (fromValue != null) { if (fromValue != null) {
fromEditText.setText(fromValue.getValue()); fromEditText.setText(fromValue.getValue());
int i = fromUnitTypes.indexOf(fromValue.getUnitType()); int i = fromUnitTypes.indexOf(fromValue.getUnitType());
if ( i >= 0 ) { if ( i >= 0 ) {
fromSpinner.setSelection(i); fromSpinner.setSelection(i);
} }
} }
final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button); final Button copyButton = (Button) main.findViewById(R.id.unit_converter_copy_button);
copyButton.setOnClickListener(new View.OnClickListener() { copyButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final EditText toEditText = (EditText) main.findViewById(R.id.units_to); final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(toEditText.getText().toString()); clipboard.setText(toEditText.getText().toString());
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show(); 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); final Button okButton = (Button) main.findViewById(R.id.unit_converter_ok_button);
if ( okButtonOnClickListener == null ) { if ( okButtonOnClickListener == null ) {
((ViewGroup) okButton.getParent()).removeView(okButton); ((ViewGroup) okButton.getParent()).removeView(okButton);
} else { } else {
okButton.setOnClickListener(this.okButtonOnClickListener); okButton.setOnClickListener(this.okButtonOnClickListener);
} }
final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button); final Button customButton = (Button) main.findViewById(R.id.unit_converter_custom_button);
if ( customButtonData == null ) { if ( customButtonData == null ) {
((ViewGroup) customButton.getParent()).removeView(customButton); ((ViewGroup) customButton.getParent()).removeView(customButton);
} else { } else {
customButton.setText(customButtonData.text); customButton.setText(customButtonData.text);
customButton.setOnClickListener(new View.OnClickListener() { customButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main)); customButtonData.clickListener.onClick(getFromUnit(main), getToUnit(main));
} }
}); });
} }
return main; return main;
} }
private void fillSpinner(@NotNull final View main, private void fillSpinner(@NotNull final View main,
@NotNull final Context context, @NotNull final Context context,
final int spinnerId, final int spinnerId,
@NotNull List<? extends UnitType<String>> unitTypes) { @NotNull List<? extends UnitType<String>> unitTypes) {
final Spinner spinner = (Spinner) main.findViewById(spinnerId); final Spinner spinner = (Spinner) main.findViewById(spinnerId);
final ArrayAdapter<UnitType<String>> adapter = new ArrayAdapter<UnitType<String>>(context, android.R.layout.simple_spinner_item); final ArrayAdapter<UnitType<String>> adapter = new ArrayAdapter<UnitType<String>>(context, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (UnitType<String> fromUnitType : unitTypes) { for (UnitType<String> fromUnitType : unitTypes) {
adapter.add(fromUnitType); adapter.add(fromUnitType);
} }
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
doConversion(main, context, UnitConverterViewBuilder.this.converter); doConversion(main, context, UnitConverterViewBuilder.this.converter);
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
} }
}); });
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
} }
private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter<String> converter) { private static void doConversion(@NotNull View main, @NotNull Context context, @NotNull UnitConverter<String> converter) {
final EditText fromEditText = (EditText) main.findViewById(R.id.units_from); final EditText fromEditText = (EditText) main.findViewById(R.id.units_from);
final EditText toEditText = (EditText) main.findViewById(R.id.units_to); final EditText toEditText = (EditText) main.findViewById(R.id.units_to);
final String from = fromEditText.getText().toString(); final String from = fromEditText.getText().toString();
try { try {
toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main))); toEditText.setText(doConversion(converter, from, getFromUnitType(main), getToUnitType(main)));
} catch (ConversionException e) { } catch (ConversionException e) {
toEditText.setText(context.getString(R.string.c_error)); toEditText.setText(context.getString(R.string.c_error));
} }
} }
public static final class ConversionException extends Exception { public static final class ConversionException extends Exception {
private ConversionException() { private ConversionException() {
} }
private ConversionException(Throwable throwable) { private ConversionException(Throwable throwable) {
super(throwable); super(throwable);
} }
} }
@NotNull @NotNull
public static String doConversion(@NotNull UnitConverter<String> converter, public static String doConversion(@NotNull UnitConverter<String> converter,
@Nullable String from, @Nullable String from,
@NotNull UnitType<String> fromUnitType, @NotNull UnitType<String> fromUnitType,
@NotNull UnitType<String> toUnitType) throws ConversionException{ @NotNull UnitType<String> toUnitType) throws ConversionException{
final String result; final String result;
if (StringUtils.isEmpty(from)) { if (StringUtils.isEmpty(from)) {
result = ""; result = "";
} else { } else {
String to = null; String to = null;
try { try {
if (converter.isSupported(fromUnitType, toUnitType)) { if (converter.isSupported(fromUnitType, toUnitType)) {
to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue(); to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue();
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new ConversionException(e); throw new ConversionException(e);
} }
result = to; result = to;
} }
return result; return result;
} }
@NotNull @NotNull
private static Unit<String> getToUnit(@NotNull View main) { private static Unit<String> getToUnit(@NotNull View main) {
final EditText toUnits = (EditText) main.findViewById(R.id.units_to); final EditText toUnits = (EditText) main.findViewById(R.id.units_to);
return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main)); return UnitImpl.newInstance(toUnits.getText().toString(), getToUnitType(main));
} }
@NotNull @NotNull
private static UnitType<String> getToUnitType(@NotNull View main) { private static UnitType<String> getToUnitType(@NotNull View main) {
final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to); final Spinner toSpinner = (Spinner) main.findViewById(R.id.unit_types_to);
return (UnitType<String>) toSpinner.getSelectedItem(); return (UnitType<String>) toSpinner.getSelectedItem();
} }
@NotNull @NotNull
private static Unit<String> getFromUnit(@NotNull View main) { private static Unit<String> getFromUnit(@NotNull View main) {
final EditText fromUnits = (EditText) main.findViewById(R.id.units_from); final EditText fromUnits = (EditText) main.findViewById(R.id.units_from);
return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main)); return UnitImpl.newInstance(fromUnits.getText().toString(), getFromUnitType(main));
} }
@NotNull @NotNull
private static UnitType<String> getFromUnitType(@NotNull View main) { private static UnitType<String> getFromUnitType(@NotNull View main) {
final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from); final Spinner fromSpinner = (Spinner) main.findViewById(R.id.unit_types_from);
return (UnitType<String>) fromSpinner.getSelectedItem(); return (UnitType<String>) fromSpinner.getSelectedItem();
} }
public static class CustomButtonData { public static class CustomButtonData {
@NotNull @NotNull
private String text; private String text;
@NotNull @NotNull
private CustomButtonOnClickListener clickListener; private CustomButtonOnClickListener clickListener;
public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) { public CustomButtonData(@NotNull String text, @NotNull CustomButtonOnClickListener clickListener) {
this.text = text; this.text = text;
this.clickListener = clickListener; this.clickListener = clickListener;
} }
} }
public static interface CustomButtonOnClickListener { public static interface CustomButtonOnClickListener {
void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits); void onClick(@NotNull Unit<String> fromUnits, @NotNull Unit<String> toUnits);
} }
} }

View File

@ -1,70 +1,72 @@
package org.solovyev.android; package org.solovyev.android;
import junit.framework.Assert; import junit.framework.Assert;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.Test;
import org.solovyev.android.calculator.AndroidNumeralBase; import org.solovyev.android.calculator.AndroidNumeralBase;
import org.solovyev.math.units.Unit;
import java.util.Date; import org.solovyev.math.units.UnitConverter;
import java.util.Random;
import java.util.Date;
/** import java.util.Random;
* User: serso
* Date: 4/21/12 /**
* Time: 8:24 PM * User: serso
*/ * Date: 4/21/12
public class AndroidNumeralBaseTest { * Time: 8:24 PM
*/
@NotNull public class AndroidNumeralBaseTest {
private final UnitConverter c = AndroidNumeralBase.getConverter();
@NotNull
@Test private final UnitConverter c = AndroidNumeralBase.getConverter();
public void testIsSupported() throws Exception {
Assert.assertTrue(c.isSupported(AndroidNumeralBase.bin, AndroidNumeralBase.dec)); @Test
} public void testIsSupported() throws Exception {
Assert.assertTrue(c.isSupported(AndroidNumeralBase.bin, AndroidNumeralBase.dec));
@Test }
public void testConvertFromDec() throws Exception {
@Test
Assert.assertEquals("101", c.convert(AndroidNumeralBase.dec.createUnit("5"), AndroidNumeralBase.bin).getValue()); public void testConvertFromDec() throws Exception {
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("101", c.convert(AndroidNumeralBase.dec.createUnit("5"), AndroidNumeralBase.bin).getValue());
Assert.assertEquals("1111100111", c.convert(AndroidNumeralBase.dec.createUnit("999"), 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("A23", c.convert(AndroidNumeralBase.dec.createUnit("2595"), AndroidNumeralBase.hex).getValue()); Assert.assertEquals("1111100111", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.bin).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("A23", c.convert(AndroidNumeralBase.dec.createUnit("2595"), AndroidNumeralBase.hex).getValue());
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.hex).getValue()); Assert.assertEquals("AEE", c.convert(AndroidNumeralBase.dec.createUnit("2798"), AndroidNumeralBase.hex).getValue());
Assert.assertEquals("3E7", c.convert(AndroidNumeralBase.dec.createUnit("999"), 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("76", c.convert(AndroidNumeralBase.dec.createUnit("62"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("3E7", c.convert(AndroidNumeralBase.dec.createUnit("999"), AndroidNumeralBase.hex).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("76", c.convert(AndroidNumeralBase.dec.createUnit("62"), AndroidNumeralBase.oct).getValue());
Assert.assertEquals("0", c.convert(AndroidNumeralBase.dec.createUnit("0"), AndroidNumeralBase.oct).getValue()); Assert.assertEquals("12", c.convert(AndroidNumeralBase.dec.createUnit("10"), AndroidNumeralBase.oct).getValue());
Assert.assertEquals("10445", c.convert(AndroidNumeralBase.dec.createUnit("4389"), 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()); @Test
for (int i = 0; i < 100000; i++) { public void testRandomConvert() throws Exception {
final String value = String.valueOf(random.nextInt()); final Random random = new Random(new Date().getTime());
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.oct, AndroidNumeralBase.oct, AndroidNumeralBase.bin, AndroidNumeralBase.dec)); for (int i = 0; i < 100000; i++) {
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.bin, AndroidNumeralBase.hex, AndroidNumeralBase.dec, AndroidNumeralBase.dec)); final String value = String.valueOf(random.nextInt());
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.oct, AndroidNumeralBase.dec)); Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.oct, AndroidNumeralBase.oct, AndroidNumeralBase.bin, AndroidNumeralBase.dec));
Assert.assertEquals(value, convertChain(value, AndroidNumeralBase.dec, AndroidNumeralBase.hex, AndroidNumeralBase.bin, AndroidNumeralBase.oct, 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); @NotNull
private String convertChain(@NotNull String value, @NotNull AndroidNumeralBase baseAndroid, @NotNull AndroidNumeralBase... typeAndroids) {
for (AndroidNumeralBase typeAndroid : typeAndroids) { Unit<String> unit = baseAndroid.createUnit(value);
unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid);
} for (AndroidNumeralBase typeAndroid : typeAndroids) {
unit = AndroidNumeralBase.getConverter().convert(unit, typeAndroid);
return unit.getValue(); }
}
} return unit.getValue();
}
}

View File

@ -15,6 +15,7 @@
<module>calculatorpp</module> <module>calculatorpp</module>
<module>calculatorpp-service</module> <module>calculatorpp-service</module>
<module>calculatorpp-test</module> <module>calculatorpp-test</module>
<module>calculatorpp-core</module>
</modules> </modules>
<properties> <properties>
@ -81,7 +82,13 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.8.1</version> <version>4.8.2</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>annotations</artifactId>
<version>7.0.3</version>
</dependency> </dependency>
<dependency> <dependency>