Skip to content

Commit

Permalink
113: Update Valhalla demos
Browse files Browse the repository at this point in the history
Task-Url: #113
  • Loading branch information
keilw committed Oct 4, 2024
1 parent 88bdffa commit 88e8d3a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package tech.uom.demo.java17.format;

import static javax.measure.Quantity.Scale.ABSOLUTE;
import static javax.measure.Quantity.Scale.*;
import static tech.units.indriya.unit.Units.CELSIUS;

import java.text.NumberFormat;

import javax.measure.Quantity;
import javax.measure.Unit;
import javax.measure.quantity.Temperature;

import tech.units.indriya.quantity.Quantities;
import tech.units.indriya.format.EBNFUnitFormat;
import tech.units.indriya.format.NumberDelimiterQuantityFormat;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.units.indriya.quantity.Quantities;

public class TemperatureQuantityConverterDemo {
public static void main(String[] args) {
//Unit<Temperature> cByTen = CELSIUS.divide(10);
//var format = EBNFUnitFormat.getInstance();
var format = SimpleUnitFormat.getInstance();
Unit<Temperature> cByTen = format.parse("°C/10").asType(Temperature.class);
Quantity<Temperature> absT = Quantities.getQuantity(0d, CELSIUS, ABSOLUTE);
Quantity<Temperature> absT2 = Quantities.getQuantity(0d, CELSIUS, ABSOLUTE);
Quantity<Temperature> absT3 = Quantities.getQuantity(0d, cByTen, ABSOLUTE);
Quantity<Temperature> sum = absT.add(absT2);
System.out.println(sum);
System.out.println(absT3);
System.out.println(sum.to(cByTen));
Quantity<Temperature> relT = Quantities.getQuantity(0d, CELSIUS, RELATIVE);
Quantity<Temperature> relT2 = Quantities.getQuantity(0d, CELSIUS, RELATIVE);
Quantity<Temperature> sumAbs = absT.add(absT2);
Quantity<Temperature> sumRel = relT.add(relT2);
System.out.println(sumAbs);
System.out.println(sumRel);
System.out.printf("Abs: %s%s", sumAbs.to(cByTen), System.lineSeparator());
System.out.printf("Rel: %s%s", sumRel.to(cByTen), System.lineSeparator());

var qformat = NumberDelimiterQuantityFormat.getInstance(NumberFormat.getInstance(), format);
var source = format.parse("K/10");
var intermediary = format.parse("K");

var qs = Quantities.getQuantity(10, source, ABSOLUTE);
System.out.println(qformat.format(qs));
var qs3 = Quantities.getQuantity(1, intermediary, ABSOLUTE);
var qs4 = qs.multiply(qs3);
System.out.println(qformat.format(qs4));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,36 @@

import static javax.measure.Quantity.Scale.*;

import java.text.NumberFormat;

import javax.measure.IncommensurableException;
import javax.measure.UnconvertibleException;
import javax.measure.UnitConverter;
import javax.measure.spi.ServiceProvider;

import tech.units.indriya.AbstractUnit;
import tech.units.indriya.format.EBNFUnitFormat;
import tech.units.indriya.format.NumberDelimiterQuantityFormat;
import tech.units.indriya.quantity.Quantities;

public class TemperatureUnitConverterDemo {

public static void main(String[] args) {
//var format = ServiceProvider.current().getFormatService().getUnitFormat();
var format = EBNFUnitFormat.getInstance();
var format2 = NumberDelimiterQuantityFormat.getInstance(NumberFormat.getInstance(), format);
var source = format.parse("K/10");
var intermediary = format.parse("K");
var dest = format.parse("K");
var source2 = format.parse("°C/10");
var dest = format.parse("°C");
var dest2 = format.parse("°C");
UnitConverter converter = null;
UnitConverter converter2 = null;
UnitConverter converter3 = null;
try {
converter = source.getConverterToAny(intermediary);
converter2 = dest.getConverterToAny(intermediary);
converter3 = ((AbstractUnit<?>)source2).getConverterToAny(dest, RELATIVE);
} catch (UnconvertibleException | IncommensurableException e) {
converter = source.getConverterToAny(dest);
converter2 = ((AbstractUnit<?>)source2).getConverterToAny(dest2, RELATIVE);
} catch (IncommensurableException e) {
e.printStackTrace();
}
if (converter != null) {
var toK = converter2.convert(1);
var converted = converter.convert(toK);
var converted = converter.convert(10);
System.out.println(converted);
}
if (converter3 != null) {
var converted = converter3.convert(10);
System.out.println(converted);
}

var qs = Quantities.getQuantity(10, source, ABSOLUTE);
System.out.println(format2.format(qs));
//var qs2 = Quantities.getQuantity(10, source2, Scale.ABSOLUTE);
//System.out.println(format2.format(qs2));
var qs3 = Quantities.getQuantity(1, intermediary, ABSOLUTE);
var qs4 = qs.multiply(qs3);
System.out.println(format2.format(qs4));
//var qs5 = qs.to(dest);
if (converter2 != null) {
var converted2 = converter2.convert(10);
System.out.println(converted2);
}
}

}
19 changes: 13 additions & 6 deletions lang/valhalla/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,40 @@
<artifactId>valhalla</artifactId>
<name>Project Valhalla Demos</name>
<properties>
<java.version>14</java.version>
<java.version>23</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--
<source>${java.version}</source>
<target>${java.version}</target>
<verbose>true</verbose>
<fork>true</fork>
<executable>${java.bin}/javac.exe</executable>
<compilerVersion>${java.version}</compilerVersion>
-->
<release>${java.version}</release>
<enablePreview>true</enablePreview>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<jvm>${java.bin}/java.exe</jvm>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<version>1.6.0</version>
<executions>
<execution>
<goals>
Expand All @@ -49,11 +53,16 @@
</execution>
</executions>
<configuration>
<executable>${java.bin}/java.exe</executable>
<mainClass>tech.uom.demo.valhalla.ValhallaDemo</mainClass>
<!-- executable>${java.bin}/java.exe</executable -->
<commandlineArgs>--enable-preview</commandlineArgs>
<arguments>
<argument>--enable-preview</argument>
<!--
<argument>-classpath</argument>
<classpath />
<argument>tech.uom.demo.valhalla.ValhallaDemo</argument>
-->
</arguments>
</configuration>
</plugin>
Expand All @@ -71,12 +80,10 @@
<dependency>
<groupId>tech.uom.lib</groupId>
<artifactId>uom-lib-common</artifactId>
<version>${lib.version}</version>
</dependency>
<dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
<version>${ri.version}</version>
</dependency>
</dependencies>

Expand Down
74 changes: 43 additions & 31 deletions lang/valhalla/src/main/java/tech/uom/demo/valhalla/VUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import javax.measure.*;
import java.util.Map;

public inline class VUnit<Q extends Quantity<Q>> implements Unit?<Q> {

public value class VUnit<Q extends Quantity<Q>> implements Unit<Q> {

private final String x;
private final String y;
Expand Down Expand Up @@ -44,21 +43,6 @@ public Map<? extends Unit<?>, Integer> getBaseUnits() {
return null;
}

@Override
public boolean isCompatible(Unit<?> that) {
return false;
}

@Override
public <T extends Quantity<T>> Unit<T> asType(Class<T> type) throws ClassCastException {
return null;
}

@Override
public UnitConverter getConverterToAny(Unit<?> that) throws IncommensurableException, UnconvertibleException {
return null;
}

@Override
public Unit<Q> alternate(String symbol) {
return null;
Expand All @@ -84,11 +68,6 @@ public Unit<Q> multiply(double multiplier) {
return null;
}

@Override
public Unit<?> multiply(Unit<?> multiplier) {
return null;
}

@Override
public Unit<?> inverse() {
return null;
Expand All @@ -104,10 +83,6 @@ public Unit<Q> divide(double divisor) {
return null;
}

@Override
public Unit<?> divide(Unit<?> divisor) {
return null;
}

@Override
public Unit<?> root(int n) {
Expand All @@ -129,14 +104,51 @@ public Unit<Q> prefix(Prefix prefix) {
return null;
}

@Override
public UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException {
return null;
}

@Override
public String toString() {
return "VUnit [symbol=" + x + ", name=" + y + "]";
}

@Override
public boolean isEquivalentTo(Unit that) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isCompatible(Unit that) {
// TODO Auto-generated method stub
return false;
}

@Override
public Unit asType(Class type) throws ClassCastException {
// TODO Auto-generated method stub
return null;
}

@Override
public UnitConverter getConverterTo(Unit that) throws UnconvertibleException {
// TODO Auto-generated method stub
return null;
}

@Override
public UnitConverter getConverterToAny(Unit that) throws IncommensurableException, UnconvertibleException {
// TODO Auto-generated method stub
return null;
}

@Override
public Unit multiply(Unit multiplier) {
// TODO Auto-generated method stub
return null;
}

@Override
public Unit divide(Unit divisor) {
// TODO Auto-generated method stub
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package tech.uom.demo.valhalla;

import javax.measure.quantity.Mass;

public class ValhallaDemo {
private static final String PATTERN = "%s and %s are %s.";
private static final String SAME = "the same";
private static final String NOT_SAME = "not the same";

public static void main(String[] args) {
VUnit m1 = VUnit.of("kg","kilogram");
VUnit m2 = VUnit.of("kg","kilogram");
VUnit<Mass> m1 = VUnit.of("kg","kilogram");
VUnit<Mass> m2 = VUnit.of("kg","kilogram");
System.out.println(m1);
if (m1 == m2) {
System.out.println(String.format(PATTERN, m1, m2, SAME));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package tech.uom.demo.valhalla;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/*
* @test
* @summary Test inline classes for equality
* @summary Test value classes for equality
*/
public class EqualityTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package tech.uom.demo.valhalla;

import static org.junit.Assert.*;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

import org.junit.Test;

/*
* @test
Expand All @@ -14,17 +12,17 @@
*/
public class InlineReferenceTest {

@Test(expected = IllegalArgumentException.class)
public void test1() {
VUnit? p = VUnit.of("k","kilogram");
WeakReference<VUnit?> r = new WeakReference<>(p);
}

@Test(expected = IllegalArgumentException.class)
public void test2() {
ReferenceQueue<Object> q = new ReferenceQueue<>();
VUnit? p = VUnit.of("k","kilogram");
WeakReference<VUnit?> r = new WeakReference<>(p, q);
}
// @Test(expected = IllegalArgumentException.class)
// public void test1() {
// VUnit? p = VUnit.of("k","kilogram");
// WeakReference<VUnit?> r = new WeakReference<>(p);
// }
//
// @Test(expected = IllegalArgumentException.class)
// public void test2() {
// ReferenceQueue<Object> q = new ReferenceQueue<>();
// VUnit? p = VUnit.of("k","kilogram");
// WeakReference<VUnit?> r = new WeakReference<>(p, q);
// }

}
Loading

0 comments on commit 88e8d3a

Please sign in to comment.