Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
NeqModPlus
de.hft-stuttgart.cityunits
Commits
43248a84
Commit
43248a84
authored
3 years ago
by
Kai-Holger Brassel
Browse files
Options
Download
Email Patches
Plain Diff
Added units for carbon emissions correctly
parent
8c7a07e5
Pipeline
#4042
passed with stage
in 2 minutes and 54 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
de.hftstuttgart.cityunits.model/src/de/hftstuttgart/cityunits/model/CarbonEmission.java
+11
-0
...l/src/de/hftstuttgart/cityunits/model/CarbonEmission.java
de.hftstuttgart.cityunits.model/src/de/hftstuttgart/cityunits/model/UrbanSimulationUnits.java
+12
-5
...de/hftstuttgart/cityunits/model/UrbanSimulationUnits.java
de.hftstuttgart.cityunits.tests/src/de/hftstuttgart/cityunits/tests/QuantityTest.java
+5
-2
...sts/src/de/hftstuttgart/cityunits/tests/QuantityTest.java
with
28 additions
and
7 deletions
+28
-7
de.hftstuttgart.cityunits.model/src/de/hftstuttgart/cityunits/model/CarbonEmission.java
0 → 100644
+
11
-
0
View file @
43248a84
package
de.hftstuttgart.cityunits.model
;
import
javax.measure.Quantity
;
/**
* This interface represents total amounts of Carbon.
*/
public
interface
CarbonEmission
extends
Quantity
<
CarbonEmission
>
{
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
de.hftstuttgart.cityunits.model/src/de/hftstuttgart/cityunits/model/UrbanSimulationUnits.java
+
12
-
5
View file @
43248a84
...
...
@@ -20,6 +20,10 @@ import tech.units.indriya.unit.TransformedUnit;
import
tech.units.indriya.unit.UnitDimension
;
import
tech.units.indriya.unit.Units
;
/**
* See https://www.openhab.org/docs/concepts/units-of-measurement.html and
* https://github.com/unitsofmeasurement/uom-demos/blob/master/domain/energy/src/main/java/tech/uom/demo/energy/
*/
public
class
UrbanSimulationUnits
extends
AbstractSystemOfUnits
{
private
static
final
UrbanSimulationUnits
INSTANCE
=
new
UrbanSimulationUnits
();
...
...
@@ -28,8 +32,11 @@ public class UrbanSimulationUnits extends AbstractSystemOfUnits {
return
getClass
().
getSimpleName
();
}
public
static
final
Unit
<
Dimensionless
>
CO2EQ
=
addUnit
(
AbstractUnit
.
ONE
);
public
static
final
Unit
<
Mass
>
TON
=
addUnit
(
Units
.
KILOGRAM
.
multiply
(
1000
));
public
static
final
Unit
<
CarbonEmission
>
GRAM_CO2_EQUIVALENT
=
Units
.
GRAM
.
asType
(
CarbonEmission
.
class
);
public
static
final
Unit
<
CarbonEmission
>
KILOGRAM_CO2_EQUIVALENT
=
Units
.
KILOGRAM
.
asType
(
CarbonEmission
.
class
);
public
static
final
Unit
<
CarbonEmission
>
TON_CO2_EQUIVALENT
=
TON
.
asType
(
CarbonEmission
.
class
);
public
static
final
Unit
<
Dimensionless
>
PARTS_PER_MILLION
=
addUnit
(
new
TransformedUnit
<>(
AbstractUnit
.
ONE
,
MultiplyConverter
.
ofRational
(
BigInteger
.
ONE
,
BigInteger
.
valueOf
(
1000000
))));
public
static
final
Unit
<
Dimensionless
>
DECIBEL
=
addUnit
(
AbstractUnit
.
ONE
.
transform
(
...
...
@@ -38,16 +45,16 @@ public class UrbanSimulationUnits extends AbstractSystemOfUnits {
new
AlternateUnit
<
Intensity
>(
Units
.
WATT
.
divide
(
Units
.
SQUARE_METRE
),
"W/m²"
));
// To model costs I added monetary units quick and dirty as SI base units.
// According to JavaDoc of AbstractUnit,
// monetary units should rather be implemented in an extra type hierarchy below
// ComparableUnit.
// According to JavaDoc of AbstractUnit, monetary units should rather be implemented in an extra type hierarchy
// below ComparableUnit.
public
final
static
Dimension
MONEY_DIMENSION
=
UnitDimension
.
parse
(
'M'
);
public
final
static
Unit
<
Euro
>
EURO
=
new
BaseUnit
<
Euro
>(
"€"
,
MONEY_DIMENSION
);
public
final
static
Unit
<
Dollar
>
DOLLAR
=
new
BaseUnit
<
Dollar
>(
"$"
,
MONEY_DIMENSION
);
static
{
SimpleUnitFormat
.
getInstance
().
label
(
CO2EQ
,
"CO₂eq"
);
SimpleUnitFormat
.
getInstance
().
label
(
TON
,
"t"
);
SimpleUnitFormat
.
getInstance
().
label
(
KILOGRAM_CO2_EQUIVALENT
,
"kg CO₂eq"
);
SimpleUnitFormat
.
getInstance
().
label
(
TON_CO2_EQUIVALENT
,
"t CO₂eq"
);
SimpleUnitFormat
.
getInstance
().
label
(
DECIBEL
,
"dB"
);
SimpleUnitFormat
.
getInstance
().
label
(
PARTS_PER_MILLION
,
"ppm"
);
SimpleUnitFormat
.
getInstance
().
label
(
EURO
,
"€"
);
...
...
This diff is collapsed.
Click to expand it.
de.hftstuttgart.cityunits.tests/src/de/hftstuttgart/cityunits/tests/QuantityTest.java
+
5
-
2
View file @
43248a84
...
...
@@ -13,6 +13,7 @@ import javax.measure.quantity.Mass;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.Test
;
import
de.hftstuttgart.cityunits.model.CarbonEmission
;
import
de.hftstuttgart.cityunits.model.UrbanSimulationUnits
;
import
tech.units.indriya.quantity.Quantities
;
import
tech.units.indriya.unit.Units
;
...
...
@@ -68,10 +69,12 @@ class QuantityTest {
@Test
void
testCO2eq
()
{
Quantity
<
Mass
>
q
=
Quantities
.
getQuantity
((
String
)
"5200.
5
kg
·
CO₂eq"
).
asType
(
Mass
.
class
);
Quantity
<
CarbonEmission
>
q
=
Quantities
.
getQuantity
((
String
)
"5200.
7
kg
CO₂eq"
).
asType
(
CarbonEmission
.
class
);
assertEquals
(
"[M]"
,
q
.
getUnit
().
getDimension
().
toString
(),
"Wrong dimension!"
);
assertTrue
(
q
.
isEquivalentTo
(
Quantities
.
getQuantity
((
String
)
"5.200
5
t
·
CO₂eq"
).
asType
(
Mass
.
class
)),
assertTrue
(
q
.
isEquivalentTo
(
Quantities
.
getQuantity
((
String
)
"5.200
7
t
CO₂eq"
).
asType
(
CarbonEmission
.
class
)),
"Wrong conversion from kg of CO₂eq to ton of CO₂eq!"
);
assertTrue
(
q
.
isEquivalentTo
(
Quantities
.
getQuantity
((
String
)
"5200700 g CO₂eq"
).
asType
(
CarbonEmission
.
class
)),
"Wrong conversion from kg of CO₂eq to gram of CO₂eq!"
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets