Skip to content

Commit

Permalink
2: Integrate Health with the SPI to demonstrate how to add a new system
Browse files Browse the repository at this point in the history
Task-Url: #2
  • Loading branch information
keilw committed Jan 5, 2021
1 parent e7f3bd6 commit 6a4d8be
Show file tree
Hide file tree
Showing 15 changed files with 467 additions and 29 deletions.
34 changes: 34 additions & 0 deletions health/health/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@
<artifactId>uom-health-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Domain Specific Units of Measurement Extensions
* Copyright (c) 2018, Units of Measurement
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Unit-API, Units of Measurement nor the names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.domain.health.spi;

import static tech.uom.domain.health.spi.ServiceConstants.*;

import javax.measure.spi.ServiceProvider;
import javax.measure.spi.SystemOfUnitsService;

import jakarta.inject.Named;
import tech.units.indriya.spi.AbstractServiceProvider;

/**
* This class implements the {@link ServiceProvider} interface and hereby uses
* the JDK {@link java.util.ServiceLoader} to load the services required.
*
* @author Werner Keil
* @version 0.3
*/
@Named(NAME)
public class HealthServiceProvider extends AbstractServiceProvider {

public int getPriority() {
return PRIO;
}

@Override
public SystemOfUnitsService getSystemOfUnitsService() {
return new HealthSystemService();
}

@Override
public String toString() {
return NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Domain Specific Units of Measurement Extensions
* Copyright (c) 2018-2021, Units of Measurement
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Unit-API, Units of Measurement nor the names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.domain.health.spi;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.measure.spi.SystemOfUnits;
import javax.measure.spi.SystemOfUnitsService;

import tech.uom.domain.health.unit.Health;
import tech.uom.lib.common.function.IntPrioritySupplier;

/**
* @author <a href="mailto:[email protected]">Werner Keil</a>
* @version 0.9, January 05, 2021
*/
public class HealthSystemService implements SystemOfUnitsService, IntPrioritySupplier {
private static final int PRIO = 20;
private static final String DEFAULT_SYSTEM_NAME = Health.getInstance().getName();
private final Map<String, SystemOfUnits> souMap = new HashMap<>();
private final Map<String, String> aliases = new HashMap<>();

public HealthSystemService() {
souMap.put(Health.getInstance().getName(), Health.getInstance());
aliases.put("Digital Imaging", DEFAULT_SYSTEM_NAME);
}

public Collection<SystemOfUnits> getAvailableSystemsOfUnits() {
return souMap.values();
}

@Override
public SystemOfUnits getSystemOfUnits() {
return getSystemOfUnits(Health.getInstance().getName());
}

@Override
public SystemOfUnits getSystemOfUnits(String name) {
String alias = aliases.get(name);
if (alias != null && alias.length() > 0) {
return souMap.get(alias);
}
return souMap.get(name);
}

@Override
public int getPriority() {
return PRIO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Domain Specific Units of Measurement Extensions
* Copyright (c) 2018-2021, Units of Measurement
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Unit-API, Units of Measurement nor the names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.domain.health.spi;

class ServiceConstants {
/** Name of the service provider
*/
static final String NAME = "Health";
/** This priority is set to <CODE>12000</CODE>.
*/
static final int PRIO = 12000;
}
11 changes: 10 additions & 1 deletion health/health/src/main/jdk9/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Units of Measurement Health Library for Java
* Copyright (c) 2015-2020, Werner Keil and others.
* Copyright (c) 2015-2021, Werner Keil and others.
*
* All rights reserved.
*
Expand Down Expand Up @@ -28,11 +28,20 @@
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
module tech.uom.health {
requires transitive java.logging;
requires transitive java.measure;
requires tech.units.indriya;
requires tech.uom.health.api;
requires tech.uom.lib.common;
requires jakarta.inject;

exports tech.uom.domain.health.spi;
exports tech.uom.domain.health.types;
exports tech.uom.domain.health.unit;

provides javax.measure.spi.ServiceProvider with
tech.uom.domain.health.spi.HealthServiceProvider;

provides javax.measure.spi.SystemOfUnitsService with
tech.uom.domain.health.spi.HealthSystemService;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.uom.domain.imaging;
package tech.uom.domain.health.spi;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.measure.spi.ServiceProvider;
import javax.measure.spi.SystemOfUnits;
Expand All @@ -42,10 +44,15 @@
import org.junit.jupiter.api.Test;

public class SystemOfUnitsServiceTest {
private static final String EXPECTED_SYSTEM_NAME = "Imaging";
//private static final int NUM_OF_UNITS_IMG = 3;
private static final int NUM_OF_PROVIDERS = 2;

private static final Logger LOGGER = Logger.getLogger(SystemOfUnitsServiceTest.class.getName());
private static final Level LOG_LEVEL = Level.FINER;

private static final String EXPECTED_SYSTEM_NAME = "Health";
private static final String EXP_SYSTEM_LONG_NAME = "tech.uom.domain.health.unit.Health";
private static final int NUM_OF_UNITS_CURR = 4;
private static final int NUM_OF_PROVIDERS_CURR = 2;
private static final String EXP_SERVICE_NAME = "tech.uom.domain.health.spi.HealthSystemService";

private static SystemOfUnitsService currService;

@BeforeAll
Expand All @@ -54,25 +61,25 @@ public static void setUp() {
}

@Test
public void testDefaultUnitSystemService() {
public void testCurrentUnitSystemService() {
assertNotNull(currService);
assertEquals("systems.uom.unicode.spi.CLDRSystemService", currService.getClass().getName());
assertEquals(EXP_SERVICE_NAME, currService.getClass().getName());
SystemOfUnits system = currService.getSystemOfUnits();
assertNotNull(system);
assertEquals("systems.uom.unicode.CLDR", system.getClass().getName());
assertEquals("Unicode CLDR", system.getName());
assertEquals(EXP_SYSTEM_LONG_NAME, system.getClass().getName());
assertEquals(EXPECTED_SYSTEM_NAME, system.getName());
assertNotNull(system.getUnits());
assertEquals(96, system.getUnits().size());
assertEquals(NUM_OF_UNITS_CURR, system.getUnits().size());
}

@Test
// TODO consolidate asserts
public void testUnitSystemServiceAlias() {
assertNotNull(currService);
assertEquals("systems.uom.unicode.spi.CLDRSystemService", currService.getClass().getName());
assertEquals(EXP_SERVICE_NAME, currService.getClass().getName());
SystemOfUnits system = currService.getSystemOfUnits(EXPECTED_SYSTEM_NAME);
//assertNotNull(system);
// assertEquals("tech.uom.domain.imaging.Imaging", system.getClass().getName());
// assertEquals("tech.uom.domain.health.spi.Imaging", system.getClass().getName());
// assertEquals("Imaging", system.getName());
// assertNotNull(system.getUnits());
// assertEquals(NUM_OF_UNITS_IMG, system.getUnits().size());
Expand All @@ -84,15 +91,15 @@ public void testUnitSystemServiceAlias() {
public void testOtherUnitSystemServiceNumber() {
final Collection<ServiceProvider> providers = ServiceProvider.available();
assertNotNull(providers);
assertEquals(NUM_OF_PROVIDERS, providers.size());
assertEquals(NUM_OF_PROVIDERS_CURR, providers.size());
}

@Test
public void testOtherUnitSystemServices() {
final Collection<ServiceProvider> providers = ServiceProvider.available();
assertNotNull(providers);
for (ServiceProvider sp : providers) {
//System.out.println(sp);
LOGGER.log(LOG_LEVEL, String.valueOf(sp));
}
}

Expand Down
4 changes: 2 additions & 2 deletions imaging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-quantity</artifactId>
<version>2.0.2</version>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-unicode</artifactId>
<version>2.0.2</version>
<version>2.1-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
package tech.uom.domain.imaging.spi;

import static tech.uom.domain.imaging.spi.ServiceConstants.*;

import javax.measure.spi.ServiceProvider;
import javax.measure.spi.SystemOfUnitsService;
import tech.units.indriya.spi.AbstractServiceProvider;
Expand All @@ -38,12 +40,12 @@
* the JDK {@link java.util.ServiceLoader} to load the services required.
*
* @author Werner Keil
* @version 0.3
* @version 0.4
*/
public class ImagingServiceProvider extends AbstractServiceProvider {

public int getPriority() {
return 800;
return PRIO;
}

@Override
Expand All @@ -53,8 +55,6 @@ public SystemOfUnitsService getSystemOfUnitsService() {

@Override
public String toString() {
return "ImagingServiceProvider";
return NAME;
}


}
Loading

0 comments on commit 6a4d8be

Please sign in to comment.