mirror of
https://github.com/gosticks/iota.lib.java.git
synced 2025-10-16 11:45:37 +00:00
implemented convertUnits
This commit is contained in:
parent
090b6f13d5
commit
606dc34c4b
16
src/main/java/jota/utils/IotaUnitConverter.java
Normal file
16
src/main/java/jota/utils/IotaUnitConverter.java
Normal file
@ -0,0 +1,16 @@
|
||||
package jota.utils;
|
||||
|
||||
/**
|
||||
* Created by pinpong on 30.11.16.
|
||||
*/
|
||||
public class IotaUnitConverter {
|
||||
|
||||
public static long convertUnits(long amount, IotaUnits fromUnit, IotaUnits toUnit) {
|
||||
long amountInSource = (long) (amount * Math.pow(10, fromUnit.getValue()));
|
||||
return convertUnits(amountInSource, toUnit);
|
||||
}
|
||||
|
||||
private static long convertUnits(long amount, IotaUnits toUnit) {
|
||||
return (long) (amount / Math.pow(10, toUnit.getValue()));
|
||||
}
|
||||
}
|
||||
29
src/main/java/jota/utils/IotaUnits.java
Normal file
29
src/main/java/jota/utils/IotaUnits.java
Normal file
@ -0,0 +1,29 @@
|
||||
package jota.utils;
|
||||
|
||||
/**
|
||||
* Created by pinpong on 30.11.16.
|
||||
*/
|
||||
public enum IotaUnits {
|
||||
IOTA("i", 0),
|
||||
KILO_IOTA("Ki", 3),
|
||||
MEGA_IOTA("Mi", 6),
|
||||
GIGA_IOTA("Gi", 9),
|
||||
TERRA_IOTA("Ti", 12),
|
||||
PETA_IOTA("Pi", 15);
|
||||
|
||||
private String unit;
|
||||
private long value;
|
||||
|
||||
IotaUnits(String unit, long value) {
|
||||
this.unit = unit;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
39
src/test/java/jota/IotaUnitConverterTest.java
Normal file
39
src/test/java/jota/IotaUnitConverterTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
package jota;
|
||||
|
||||
import jota.utils.IotaUnitConverter;
|
||||
import jota.utils.IotaUnits;
|
||||
import org.hamcrest.core.IsNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by pinpong on 30.11.16.
|
||||
*/
|
||||
public class IotaUnitConverterTest {
|
||||
|
||||
@Test
|
||||
public void shouldConvertUnitItoKi() {
|
||||
assertThat(IotaUnitConverter.convertUnits(1000, IotaUnits.IOTA, IotaUnits.KILO_IOTA), IsNull.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUnitKiToMi() {
|
||||
assertThat(IotaUnitConverter.convertUnits(1000, IotaUnits.KILO_IOTA, IotaUnits.MEGA_IOTA), IsNull.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUnitMiToGi() {
|
||||
assertThat(IotaUnitConverter.convertUnits(1000, IotaUnits.MEGA_IOTA, IotaUnits.GIGA_IOTA), IsNull.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUnitGiToTi() {
|
||||
assertThat(IotaUnitConverter.convertUnits(1000, IotaUnits.GIGA_IOTA, IotaUnits.TERRA_IOTA), IsNull.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConvertUnitTiToPi() {
|
||||
assertThat(IotaUnitConverter.convertUnits(1000, IotaUnits.TERRA_IOTA, IotaUnits.PETA_IOTA), IsNull.notNullValue());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user