Skip to content

Commit 316aeb0

Browse files
authored
feat: add energy, pressure, speed unit convertors (#197)
* feat: add energy, pressure, speed unit convertors * fix: use string constructor * fix: use sentence case Refs: #190, #191, #192
1 parent e1eefa6 commit 316aeb0

File tree

10 files changed

+451
-1
lines changed

10 files changed

+451
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Energy unit converter ([#192])
10+
- Pressure unit converter ([#191])
11+
- Speed unit converter ([#190])
812

913
### Fixed
1014
- Fixed issue that occurs after doing a percent operation ([#160])
@@ -64,6 +68,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6468
[#44]: https://github.com/FossifyOrg/Calculator/issues/44
6569
[#64]: https://github.com/FossifyOrg/Calculator/issues/64
6670
[#160]: https://github.com/FossifyOrg/Calculator/issues/160
71+
[#190]: https://github.com/FossifyOrg/Calculator/issues/190
72+
[#191]: https://github.com/FossifyOrg/Calculator/issues/191
73+
[#192]: https://github.com/FossifyOrg/Calculator/issues/192
6774

6875
[Unreleased]: https://github.com/FossifyOrg/Calculator/compare/1.2.0...HEAD
6976
[1.2.0]: https://github.com/FossifyOrg/Calculator/compare/1.1.0...1.2.0

app/src/main/kotlin/org/fossify/math/helpers/converters/Base.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ interface Converter {
1313
VolumeConverter,
1414
MassConverter,
1515
TemperatureConverter,
16-
TimeConverter
16+
TimeConverter,
17+
SpeedConverter,
18+
PressureConverter,
19+
EnergyConverter
1720
)
1821
}
1922

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.fossify.math.helpers.converters
2+
3+
import org.fossify.math.R
4+
import java.math.BigDecimal
5+
6+
/***
7+
* Base unit: joule.
8+
*
9+
* References:
10+
* - https://en.wikipedia.org/wiki/Joule
11+
* - https://en.wikipedia.org/wiki/Calorie
12+
* - https://en.wikipedia.org/wiki/Kilowatt-hour
13+
* - https://en.wikipedia.org/wiki/Electronvolt
14+
* - https://en.wikipedia.org/wiki/British_thermal_unit
15+
* - https://en.wikipedia.org/wiki/Foot-pound_(energy)
16+
* - https://en.wikipedia.org/wiki/Erg
17+
* - https://en.wikipedia.org/wiki/Therm
18+
*/
19+
object EnergyConverter : Converter {
20+
override val nameResId: Int = R.string.unit_energy
21+
override val imageResId: Int = R.drawable.ic_energy_vector
22+
override val key: String = "EnergyConverter"
23+
24+
sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) :
25+
Converter.Unit(nameResId, symbolResId, factor, key) {
26+
data object Joule : Unit(
27+
nameResId = R.string.unit_energy_joule,
28+
symbolResId = R.string.unit_energy_joule_symbol,
29+
factor = BigDecimal.ONE,
30+
key = "Joule"
31+
)
32+
33+
data object Kilojoule : Unit(
34+
nameResId = R.string.unit_energy_kilojoule,
35+
symbolResId = R.string.unit_energy_kilojoule_symbol,
36+
factor = BigDecimal("1000"),
37+
key = "Kilojoule"
38+
)
39+
40+
data object Megajoule : Unit(
41+
nameResId = R.string.unit_energy_megajoule,
42+
symbolResId = R.string.unit_energy_megajoule_symbol,
43+
factor = BigDecimal("1000000"),
44+
key = "Megajoule"
45+
)
46+
47+
data object Gigajoule : Unit(
48+
nameResId = R.string.unit_energy_gigajoule,
49+
symbolResId = R.string.unit_energy_gigajoule_symbol,
50+
factor = BigDecimal("1000000000"),
51+
key = "Gigajoule"
52+
)
53+
54+
data object Calorie : Unit(
55+
nameResId = R.string.unit_energy_calorie,
56+
symbolResId = R.string.unit_energy_calorie_symbol,
57+
factor = BigDecimal("4.184"),
58+
key = "Calorie"
59+
)
60+
61+
data object Kilocalorie : Unit(
62+
nameResId = R.string.unit_energy_kilocalorie,
63+
symbolResId = R.string.unit_energy_kilocalorie_symbol,
64+
factor = BigDecimal("4184"),
65+
key = "Kilocalorie"
66+
)
67+
68+
data object WattHour : Unit(
69+
nameResId = R.string.unit_energy_watt_hour,
70+
symbolResId = R.string.unit_energy_watt_hour_symbol,
71+
factor = BigDecimal("3600"),
72+
key = "WattHour"
73+
)
74+
75+
data object KilowattHour : Unit(
76+
nameResId = R.string.unit_energy_kilowatt_hour,
77+
symbolResId = R.string.unit_energy_kilowatt_hour_symbol,
78+
factor = BigDecimal("3600000"),
79+
key = "KilowattHour"
80+
)
81+
82+
data object MegawattHour : Unit(
83+
nameResId = R.string.unit_energy_megawatt_hour,
84+
symbolResId = R.string.unit_energy_megawatt_hour_symbol,
85+
factor = BigDecimal("3600000000"),
86+
key = "MegawattHour"
87+
)
88+
89+
data object Electronvolt : Unit(
90+
nameResId = R.string.unit_energy_electronvolt,
91+
symbolResId = R.string.unit_energy_electronvolt_symbol,
92+
factor = BigDecimal("1.602176634E-19"),
93+
key = "Electronvolt"
94+
)
95+
96+
data object BritishThermalUnit : Unit(
97+
nameResId = R.string.unit_energy_btu,
98+
symbolResId = R.string.unit_energy_btu_symbol,
99+
factor = BigDecimal("1055.05585262"),
100+
key = "BritishThermalUnit"
101+
)
102+
103+
data object Therm : Unit(
104+
nameResId = R.string.unit_energy_therm,
105+
symbolResId = R.string.unit_energy_therm_symbol,
106+
factor = BigDecimal("105505585.262"),
107+
key = "Therm"
108+
)
109+
110+
data object FootPound : Unit(
111+
nameResId = R.string.unit_energy_foot_pound,
112+
symbolResId = R.string.unit_energy_foot_pound_symbol,
113+
factor = BigDecimal("1.3558179483314004"),
114+
key = "FootPound"
115+
)
116+
117+
data object Erg : Unit(
118+
nameResId = R.string.unit_energy_erg,
119+
symbolResId = R.string.unit_energy_erg_symbol,
120+
factor = BigDecimal("0.0000001"),
121+
key = "Erg"
122+
)
123+
}
124+
125+
override val units: List<Unit> = listOf(
126+
Unit.Joule,
127+
Unit.Kilojoule,
128+
Unit.Megajoule,
129+
Unit.Gigajoule,
130+
Unit.Calorie,
131+
Unit.Kilocalorie,
132+
Unit.WattHour,
133+
Unit.KilowattHour,
134+
Unit.MegawattHour,
135+
Unit.Electronvolt,
136+
Unit.BritishThermalUnit,
137+
Unit.Therm,
138+
Unit.FootPound,
139+
Unit.Erg,
140+
)
141+
142+
override val defaultTopUnit: Unit = Unit.Kilocalorie
143+
override val defaultBottomUnit: Unit = Unit.Kilojoule
144+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package org.fossify.math.helpers.converters
2+
3+
import org.fossify.math.R
4+
import java.math.BigDecimal
5+
6+
/**
7+
* Base unit: pascal.
8+
*
9+
* Main references:
10+
* - https://en.wikipedia.org/wiki/Pascal_(unit)
11+
* - https://en.wikipedia.org/wiki/Bar_(unit)
12+
* - https://en.wikipedia.org/wiki/Atmosphere_(unit)
13+
* - https://en.wikipedia.org/wiki/Pounds_per_square_inch
14+
* - https://en.wikipedia.org/wiki/Torr
15+
* - https://en.wikipedia.org/wiki/Millimetre_of_mercury
16+
*/
17+
object PressureConverter : Converter {
18+
override val nameResId: Int = R.string.unit_pressure
19+
override val imageResId: Int = R.drawable.ic_pressure_vector
20+
override val key: String = "PressureConverter"
21+
22+
sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) :
23+
Converter.Unit(nameResId, symbolResId, factor, key) {
24+
data object Pascal : Unit(
25+
nameResId = R.string.unit_pressure_pascal,
26+
symbolResId = R.string.unit_pressure_pascal_symbol,
27+
factor = BigDecimal.ONE,
28+
key = "Pascal"
29+
)
30+
31+
data object Kilopascal : Unit(
32+
nameResId = R.string.unit_pressure_kilopascal,
33+
symbolResId = R.string.unit_pressure_kilopascal_symbol,
34+
factor = BigDecimal("1000"),
35+
key = "Kilopascal"
36+
)
37+
38+
data object Megapascal : Unit(
39+
nameResId = R.string.unit_pressure_megapascal,
40+
symbolResId = R.string.unit_pressure_megapascal_symbol,
41+
factor = BigDecimal("1000000"),
42+
key = "Megapascal"
43+
)
44+
45+
data object Bar : Unit(
46+
nameResId = R.string.unit_pressure_bar,
47+
symbolResId = R.string.unit_pressure_bar_symbol,
48+
factor = BigDecimal("100000"),
49+
key = "Bar"
50+
)
51+
52+
data object Millibar : Unit(
53+
nameResId = R.string.unit_pressure_millibar,
54+
symbolResId = R.string.unit_pressure_millibar_symbol,
55+
factor = BigDecimal("100"),
56+
key = "Millibar"
57+
)
58+
59+
data object Atmosphere : Unit(
60+
nameResId = R.string.unit_pressure_atmosphere,
61+
symbolResId = R.string.unit_pressure_atmosphere_symbol,
62+
factor = BigDecimal("101325"),
63+
key = "Atmosphere"
64+
)
65+
66+
data object Psi : Unit(
67+
nameResId = R.string.unit_pressure_psi,
68+
symbolResId = R.string.unit_pressure_psi_symbol,
69+
factor = BigDecimal("6894.757293168"),
70+
key = "Psi"
71+
)
72+
73+
data object Torr : Unit(
74+
nameResId = R.string.unit_pressure_torr,
75+
symbolResId = R.string.unit_pressure_torr_symbol,
76+
factor = BigDecimal("133.32236842105"),
77+
key = "Torr"
78+
)
79+
80+
data object MillimeterOfMercury : Unit(
81+
nameResId = R.string.unit_pressure_mmhg,
82+
symbolResId = R.string.unit_pressure_mmhg_symbol,
83+
factor = BigDecimal("133.322387415"),
84+
key = "MillimeterOfMercury"
85+
)
86+
87+
data object InchOfMercury : Unit(
88+
nameResId = R.string.unit_pressure_inhg,
89+
symbolResId = R.string.unit_pressure_inhg_symbol,
90+
factor = BigDecimal("3386.389"),
91+
key = "InchOfMercury"
92+
)
93+
}
94+
95+
override val units: List<Unit> = listOf(
96+
Unit.Pascal,
97+
Unit.Kilopascal,
98+
Unit.Megapascal,
99+
Unit.Bar,
100+
Unit.Millibar,
101+
Unit.Atmosphere,
102+
Unit.Psi,
103+
Unit.Torr,
104+
Unit.MillimeterOfMercury,
105+
Unit.InchOfMercury
106+
)
107+
override val defaultTopUnit: Unit = Unit.Bar
108+
override val defaultBottomUnit: Unit = Unit.Psi
109+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.fossify.math.helpers.converters
2+
3+
import org.fossify.commons.helpers.HOUR_SECONDS
4+
import org.fossify.math.R
5+
import org.fossify.math.helpers.MATH_CONTEXT
6+
import java.math.BigDecimal
7+
8+
/**
9+
* Base unit: meter per second.
10+
*
11+
* Main references:
12+
* - https://en.wikipedia.org/wiki/Metre_per_second
13+
* - https://en.wikipedia.org/wiki/Kilometres_per_hour
14+
* - https://en.wikipedia.org/wiki/Miles_per_hour
15+
* - https://en.wikipedia.org/wiki/Knot_(unit)
16+
* - https://en.wikipedia.org/wiki/Mach_number
17+
* - https://en.wikipedia.org/wiki/Speed_of_light
18+
*/
19+
object SpeedConverter : Converter {
20+
override val nameResId: Int = R.string.unit_speed
21+
override val imageResId: Int = R.drawable.ic_speed_vector
22+
override val key: String = "SpeedConverter"
23+
24+
sealed class Unit(nameResId: Int, symbolResId: Int, factor: BigDecimal, key: String) :
25+
Converter.Unit(nameResId, symbolResId, factor, key) {
26+
data object MeterPerSecond : Unit(
27+
nameResId = R.string.unit_speed_meter_per_second,
28+
symbolResId = R.string.unit_speed_meter_per_second_symbol,
29+
factor = BigDecimal.ONE,
30+
key = "MeterPerSecond"
31+
)
32+
33+
data object KilometerPerSecond : Unit(
34+
nameResId = R.string.unit_speed_kilometer_per_second,
35+
symbolResId = R.string.unit_speed_kilometer_per_second_symbol,
36+
factor = BigDecimal("1000"),
37+
key = "KilometerPerSecond"
38+
)
39+
40+
data object KilometerPerHour : Unit(
41+
nameResId = R.string.unit_speed_kilometer_per_hour,
42+
symbolResId = R.string.unit_speed_kilometer_per_hour_symbol,
43+
factor = BigDecimal("1000").divide(BigDecimal("3600"), MATH_CONTEXT),
44+
key = "KilometerPerHour"
45+
)
46+
47+
data object MilePerHour : Unit(
48+
nameResId = R.string.unit_speed_mile_per_hour,
49+
symbolResId = R.string.unit_speed_mile_per_hour_symbol,
50+
factor = BigDecimal("0.44704"),
51+
key = "MilePerHour"
52+
)
53+
54+
data object Knot : Unit(
55+
nameResId = R.string.unit_speed_knot,
56+
symbolResId = R.string.unit_speed_knot_symbol,
57+
factor = BigDecimal("1852").divide(BigDecimal("3600"), MATH_CONTEXT),
58+
key = "Knot"
59+
)
60+
61+
data object FootPerSecond : Unit(
62+
nameResId = R.string.unit_speed_foot_per_second,
63+
symbolResId = R.string.unit_speed_foot_per_second_symbol,
64+
factor = BigDecimal("0.3048"),
65+
key = "FootPerSecond"
66+
)
67+
68+
data object Mach : Unit(
69+
nameResId = R.string.unit_speed_mach,
70+
symbolResId = R.string.unit_speed_mach_symbol,
71+
factor = BigDecimal("340.27"), // ISA: dry air, 15 °C, sea level
72+
key = "Mach"
73+
)
74+
75+
data object SpeedOfLight : Unit(
76+
nameResId = R.string.unit_speed_speed_of_light,
77+
symbolResId = R.string.unit_speed_speed_of_light_symbol,
78+
factor = BigDecimal("299792458"),
79+
key = "SpeedOfLight"
80+
)
81+
}
82+
83+
override val units: List<Unit> = listOf(
84+
Unit.MeterPerSecond,
85+
Unit.KilometerPerSecond,
86+
Unit.KilometerPerHour,
87+
Unit.MilePerHour,
88+
Unit.Knot,
89+
Unit.FootPerSecond,
90+
Unit.Mach,
91+
Unit.SpeedOfLight
92+
)
93+
override val defaultTopUnit: Unit = Unit.KilometerPerHour
94+
override val defaultBottomUnit: Unit = Unit.MilePerHour
95+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960">
2+
<path android:pathData="M422 728l207-248H469l29-227-185 267h139l-30 208zm-62-128H236q-24 0-35.5-21.5T203 537l299-430q10-14 26-19.5t33 0.5q17 6 25 21t6 32l-32 259h155q26 0 36.5 23t-6.5 43L416 860q-11 13-27 17t-31-3q-15-7-23.5-21.5T328 821l32-221zm111-110z" android:fillColor="#FFFFFF"/>
3+
</vector>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960">
2+
<path android:pathData="M732 308q-5-5-8.5-12.5T720 280q0-11 6-19.5t15-15.5q14-11 31-17t34-11q4-1 8-1t7 3q4 4 2 15-5 17-11 34t-17 31q-7 9-15.5 15t-19.5 6q-8 0-15.5-3.5T732 308zM160 840q-33 0-56.5-23.5T80 760V200q0-33 23.5-56.5T160 120h240q33 0 56.5 23.5T480 200v327q9-3 19-5t21-2q50 0 85 35t35 85v80q0 17 11.5 28.5T680 760q17 0 28.5-11.5T720 720V520h-40v-57q-54-23-87-72t-33-111q0-83 58.5-141.5T760 80q83 0 141.5 58.5T960 280q0 62-33 111t-87 72v57h-40v200q0 50-35 85t-85 35q-50 0-85-35t-35-85v-80q0-17-11.5-28.5T520 600q-17 0-28.5 11.5T480 640v120q0 33-23.5 56.5T400 840H160zm600-440q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35zM160 760h240v-80l-32 32q-14 14-29.5 12.5T312 712q-11-11-12.5-27t12.5-30l88-88v-87l-32 32q-14 14-29.5 12.5T312 512q-11-11-12.5-27t12.5-30l88-88v-87l-32 32q-14 14-29.5 12.5T312 312q-11-11-12.5-27t12.5-30l55-55H193l55 55q14 14 12.5 29.5T248 311q-11 11-26.5 13T192 312l-32-32v87l88 88q14 14 12.5 29.5T248 511q-11 11-26.5 13T192 512l-32-32v87l88 88q14 14 12.5 29.5T248 711q-11 11-26.5 13T192 712l-32-32v80zm120-280z" android:fillColor="#FFFFFF"/>
3+
</vector>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="960" android:viewportHeight="960">
2+
<path android:pathData="M418 620q24 24 62 23.5t56-27.5l169-253q9-14-2.5-25.5T677 335L424 504q-27 18-28.5 55t22.5 61zm62-460q36 0 71 6t68 19q16 6 34 22.5t10 31.5q-8 15-36 20t-45-1q-25-9-50.5-13.5T480 240q-133 0-226.5 93.5T160 560q0 42 11.5 83t32.5 77h552q23-38 33.5-79t10.5-85q0-26-4.5-51T782 456q-6-17-2-33t18-27q13-10 28.5-6t21.5 18q15 35 23 71.5t9 74.5q1 57-13 109t-41 99q-11 18-30 28t-40 10H204q-21 0-40-10t-30-28q-26-45-40-95.5T80 560q0-83 31.5-155.5t86-127Q252 223 325 191.5T480 160zm7 313z" android:fillColor="#FFFFFF"/>
3+
</vector>

0 commit comments

Comments
 (0)