Skip to content

Commit 5fc468d

Browse files
authored
Improve quantity value (#126)
* Fix Quantity, improve QuantityValue ### Added - Add static factory methods `QuantityValue.of(value, unit)` with various value argument types that can be used to instantiate the `BigDecimal` value used internally. - Add dynamic factory methods in `Unit.quantityValue(value)` with various value argument types that can be used to instantiate the `BigDecimal` value used internally. - Add delegate methods for many `BigDecimal` methods to `QuantityValue` to allow for mathematical operations with automatic unit conversion, for example: ``` QuantityValue mVal = Qudt.Units.M.quantityValue(20); QuantityValue ftVal = Qudt.Units.FT.quantityValue(1); QuantityValue mValResult = mVal.add(ftVal); ``` ### Fixed - Fix wrong definition of `Quantity`. It used to encapsulate a Set of `QuantityValue`, which never made sense. With this change, it encapsulates a `QuantityValue` and a `QuantityKind`, conforming to the definition of the concept in QUDT. * Upgrade RDF4J to latest major version 4 * Upgrade central-publishing-maven-plugin * Upgrade dependencies * Upgrade spotless * Upgrade java version for github actions
1 parent 740834b commit 5fc468d

13 files changed

Lines changed: 762 additions & 37 deletions

File tree

.github/workflows/maven.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18-
- uses: actions/checkout@v3
19-
- name: Set up JDK 17
20-
uses: actions/setup-java@v3
18+
- uses: actions/checkout@v6
19+
- name: Set up JDK 21
20+
uses: actions/setup-java@v5
2121
with:
22-
java-version: '17'
22+
java-version: '21'
2323
distribution: 'temurin'
2424
cache: maven
2525
- name: Build with Maven

.github/workflows/release-and-deploy.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ jobs:
6161
exit 1
6262
6363
# Set up java with maven cache
64-
- uses: actions/checkout@v3
65-
- name: Set up JDK 17
66-
uses: actions/setup-java@v3
64+
- uses: actions/checkout@v6
65+
- name: Set up JDK 21
66+
uses: actions/setup-java@v5
6767
with:
6868
distribution: 'temurin'
69-
java-version: '17'
69+
java-version: '21'
7070
cache: 'maven'
7171

7272
# import the secret key
7373
- name: Set up Apache Maven Central
74-
uses: actions/setup-java@v3
74+
uses: actions/setup-java@v5
7575
with: # running setup-java again overwrites the settings.xml
7676
distribution: 'temurin'
77-
java-version: '17'
77+
java-version: '21'
7878
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
7979
server-username: MAVEN_USERNAME # env variable for username in deploy
8080
server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add static factory methods `QuantityValue.of(value, unit)` with various value argument types that can be used to instantiate the
13+
`BigDecimal` value used internally.
14+
15+
- Add dynamic factory methods in `Unit.quantityValue(value)` with various value argument types that can be used to instantiate the
16+
`BigDecimal` value used internally.
17+
18+
- Add delegate methods for many `BigDecimal` methods to `QuantityValue` to allow for mathematical operations with
19+
automatic unit conversion, for example:
20+
21+
```
22+
QuantityValue mVal = Qudt.Units.M.quantityValue(20);
23+
QuantityValue ftVal = Qudt.Units.FT.quantityValue(1);
24+
QuantityValue mValResult = mVal.add(ftVal);
25+
```
26+
27+
### Fixed
28+
29+
- Fix wrong definition of `Quantity`. It used to encapsulate a Set of `QuantityValue`, which never made sense. With
30+
this change, it encapsulates a `QuantityValue` and a `QuantityKind`, conforming to the definition of the concept in QUDT.
31+
32+
- Upgrade various dependencies.
33+
1034
## [7.1.1] - 2025-10-09
1135

1236
## [7.1.0] - 2025-09-01

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Makes all conversions and related functionality defined by the excellent [QUDT o
77

88
The library offers
99
* 1745 units, such as second, Fahrenheit, or light year
10-
* 881 quantityKinds, such as width, pressure ratio or currency
10+
* 881 quantityKinds, such as width, pressure ratio, or currency
1111
* 29 prefixes, such as mega, kibi, or atto
1212

1313
...all of which the library converts if possible.
@@ -46,7 +46,7 @@ The main Model classes are:
4646
* `QuantityValue`: value and unit. Values are always `BigDecimal` (using `MathContext.DECIMAL128`) and there are no convenience methods allowing you
4747
to provide other numeric types. This is intentiaonal so as not to mask any conversion problems. You'll be fine. (If you need a different `MathContext`, make an issue)
4848

49-
All units, quantityKinds and prefixes are avalable as constants:
49+
All units, quantityKinds, and prefixes are avalable as constants:
5050
* `Qudt.Units`: all units, such as `Qudt.Units.KiloM__PER__SEC`
5151
* `Qudt.QuantityKinds:`: all quantityKinds, such as `Qudt.QuantityKinds.BloodGlucoseLevel`
5252
* `Qudt.Prefixes`: all prefixes, such as `Qudt.Prefixes.Atto`

pom.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<maven.compiler.target>17</maven.compiler.target>
5151
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5252
<qudt.release.version>3.1.5</qudt.release.version>
53-
<spotless.format.version>1.25.0</spotless.format.version>
53+
<spotless.format.version>1.33.0</spotless.format.version>
5454
</properties>
5555

5656
<scm>
@@ -65,35 +65,41 @@
6565
<dependency>
6666
<groupId>org.junit.jupiter</groupId>
6767
<artifactId>junit-jupiter</artifactId>
68-
<version>5.9.1</version>
68+
<version>6.1.0-M1</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.assertj</groupId>
73+
<artifactId>assertj-core</artifactId>
74+
<version>4.0.0-M1</version>
6975
<scope>test</scope>
7076
</dependency>
7177
<dependency>
7278
<groupId>org.slf4j</groupId>
7379
<artifactId>slf4j-reload4j</artifactId>
74-
<version>2.0.3</version>
80+
<version>2.1.0-alpha1</version>
7581
</dependency>
7682
<dependency>
7783
<groupId>org.eclipse.rdf4j</groupId>
7884
<artifactId>rdf4j-bom</artifactId>
7985
<type>pom</type>
80-
<version>4.2.0</version>
86+
<version>5.3.0-M1</version>
8187
<scope>import</scope>
8288
</dependency>
8389
<dependency>
8490
<groupId>org.freemarker</groupId>
8591
<artifactId>freemarker</artifactId>
86-
<version>2.3.31</version>
92+
<version>2.3.34</version>
8793
</dependency>
8894
<dependency>
8995
<groupId>org.hamcrest</groupId>
9096
<artifactId>hamcrest-library</artifactId>
91-
<version>2.2</version>
97+
<version>3.0</version>
9298
</dependency>
9399
<dependency>
94100
<groupId>org.apache.commons</groupId>
95101
<artifactId>commons-collections4</artifactId>
96-
<version>4.5.0-M2</version>
102+
<version>4.5.0</version>
97103
</dependency>
98104
</dependencies>
99105
</dependencyManagement>
@@ -162,7 +168,7 @@
162168
<plugin>
163169
<groupId>com.diffplug.spotless</groupId>
164170
<artifactId>spotless-maven-plugin</artifactId>
165-
<version>2.44.0.BETA3</version>
171+
<version>3.2.1</version>
166172
</plugin>
167173
<plugin>
168174
<groupId>org.codehaus.mojo</groupId>
@@ -278,7 +284,7 @@
278284
<plugin>
279285
<groupId>org.sonatype.central</groupId>
280286
<artifactId>central-publishing-maven-plugin</artifactId>
281-
<version>0.8.0</version>
287+
<version>0.10.0</version>
282288
<extensions>true</extensions>
283289
<configuration>
284290
<publishingServerId>ossrh</publishingServerId>

qudtlib-main-rdf/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<version>7.1.2-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
9-
<name>qudtlib-main-rdf&gt;</name>
9+
<name>qudtlib-main-rdf</name>
1010
<artifactId>qudtlib-main-rdf</artifactId>
1111
<packaging>pom</packaging>
1212

qudtlib-model/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@
99
<modelVersion>4.0.0</modelVersion>
1010
<packaging>jar</packaging>
1111
<artifactId>qudtlib-model</artifactId>
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.junit.jupiter</groupId>
15+
<artifactId>junit-jupiter-api</artifactId>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.assertj</groupId>
19+
<artifactId>assertj-core</artifactId>
20+
</dependency>
21+
</dependencies>
1222
</project>

qudtlib-model/src/main/java/io/github/qudtlib/model/DimensionVector.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,55 @@ public class DimensionVector {
4444

4545
private String dimensionVectorIri;
4646

47+
public static Builder builder() {
48+
return new Builder();
49+
}
50+
51+
public static class Builder {
52+
private float[] values = new float[8];
53+
54+
public Builder() {}
55+
56+
public Builder amountOfSubstance(float exp) {
57+
values[INDEX_AMOUNT_OF_SUBSTANCE] = exp;
58+
return this;
59+
}
60+
61+
public Builder electricCurrent(float exp) {
62+
values[INDEX_ELECTRIC_CURRENT] = exp;
63+
return this;
64+
}
65+
66+
public Builder length(float exp) {
67+
values[INDEX_LENGTH] = exp;
68+
return this;
69+
}
70+
71+
public Builder luminousIntensity(float exp) {
72+
values[INDEX_LUMINOUS_INTENSITY] = exp;
73+
return this;
74+
}
75+
76+
public Builder mass(float exp) {
77+
values[INDEX_MASS] = exp;
78+
return this;
79+
}
80+
81+
public Builder temperature(float exp) {
82+
values[INDEX_TEMPERATURE] = exp;
83+
return this;
84+
}
85+
86+
public Builder time(float exp) {
87+
values[INDEX_TIME] = exp;
88+
return this;
89+
}
90+
91+
public DimensionVector build() {
92+
return new DimensionVector(values);
93+
}
94+
}
95+
4796
private final float[] values;
4897

4998
public static Optional<DimensionVector> of(String dimensionVectorIri) {
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package io.github.qudtlib.model;
22

3-
import java.util.Collections;
4-
import java.util.Objects;
5-
import java.util.Set;
6-
import java.util.stream.Collectors;
7-
83
/**
9-
* Represents a QUDT Quantity - a set of {@link QuantityValue}s.
4+
* Represents a QUDT Quantity - A QuantityValue with a QuantityKind
105
*
116
* @author Florian Kleedorfer
127
* @version 1.0
138
*/
149
public class Quantity {
15-
final Set<QuantityValue> quantityValues;
10+
final QuantityValue quantityValue;
11+
12+
final QuantityKind quantityKind;
13+
14+
public Quantity(QuantityValue quantityValue, QuantityKind quantityKind) {
15+
this.quantityValue = quantityValue;
16+
this.quantityKind = quantityKind;
17+
}
1618

17-
public Quantity(Set<QuantityValue> quantityValues) {
18-
this.quantityValues = quantityValues;
19+
public QuantityValue getQuantityValue() {
20+
return quantityValue;
1921
}
2022

21-
public Set<QuantityValue> getQuantityValues() {
22-
return Collections.unmodifiableSet(quantityValues);
23+
public QuantityKind getQuantityKind() {
24+
return quantityKind;
2325
}
2426

2527
@Override
2628
public String toString() {
27-
return "Quantity{"
28-
+ quantityValues.stream().map(Objects::toString).collect(Collectors.joining(", "))
29-
+ '}';
29+
return quantityKind.toString() + " of " + quantityValue.toString();
3030
}
3131
}

0 commit comments

Comments
 (0)