Commit 987f6d41 authored by Kai-Holger Brassel's avatar Kai-Holger Brassel
Browse files

Provide new (primitive) Ecore data type for time of day in ISO format

parent 44120a06
Pipeline #6096 passed with stage
in 8 minutes and 25 seconds
variables:
RELEASE_DIR: "public/release_target_110"
RELEASE_DIR: "public/p2repo"
pages:
stage: deploy
tags:
- docker # use shared runner
image: maven:3.8.2-adoptopenjdk-16
image: maven:3.8.4-eclipse-temurin-17-alpine
script:
- mvn --version
- mvn clean install # build p2 repo
......
# de.hftstuttgart.cityunits
Create Eclipse P2 repository with Ecore data types `QuantityDouble` and `QuantityLong` for units based on Indriya reference implementation of Units of Measurement Java specification (JSR 385) and some special units for urban simulation.
Create Eclipse P2 repository with Ecore data type `Quantity` for units based on Indriya reference implementation of Units of Measurement Java specification (JSR 385) and some special units for urban simulation.
To add OSGi bundles published in this P2 repository add site [https://transfer.hft-stuttgart.de/pages/neqmodplus/de.hft-stuttgart.cityunits/release_target_102/]() to a running Eclipse instance via `Eclipse -> Preferences -> Install/Update -> Available Software Sites -> Add...` or to a target platform definition via `Eclipse -> Preferences -> Plug-in Development -> Target Platform -> Edit...`.
Also adds another Ecore data type `TimeOfDay` useful to model schedules and the like.
To install this feature in an Eclipse application add site [https://transfer.hft-stuttgart.de/pages/neqmodplus/de.hft-stuttgart.cityunits/p2repo]() via `Eclipse -> Help -> Install New Software...` and select _City Units_ (If nothing can be selected, ensure that _Group items by category_ is ticked.)
For an introduction on dealing with units in Java, see
[Baeldung: Introduction to javax.measure](https://www.baeldung.com/javax-measure).
......
......@@ -2,4 +2,5 @@
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="quantities" nsURI="https://www.hftstuttgart.de/quantities" nsPrefix="quant">
<eClassifiers xsi:type="ecore:EDataType" name="Quantity" instanceClassName="de.hftstuttgart.cityunits.model.NullableQuantity"/>
<eClassifiers xsi:type="ecore:EDataType" name="LocalTime" instanceClassName="java.time.LocalTime"/>
</ecore:EPackage>
......@@ -8,5 +8,6 @@
<genPackages prefix="Quantities" basePackage="de.hftstuttgart.cityunits.model" disposableProviderFactory="true"
ecorePackage="Quantities.ecore#/">
<genDataTypes ecoreDataType="Quantities.ecore#//Quantity" create="return de.hftstuttgart.cityunits.model.NullableQuantity.create(it);"/>
<genDataTypes ecoreDataType="Quantities.ecore#//LocalTime" create="return java.time.LocalTime.parse(it);"/>
</genPackages>
</genmodel:GenModel>
......@@ -65,6 +65,17 @@ public interface QuantitiesPackage extends EPackage {
int QUANTITY = 0;
/**
* The meta object id for the '<em>Local Time</em>' data type.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see java.time.LocalTime
* @see de.hftstuttgart.cityunits.model.quantities.impl.QuantitiesPackageImpl#getLocalTime()
* @generated
*/
int LOCAL_TIME = 1;
/**
* Returns the meta object for data type '{@link de.hftstuttgart.cityunits.model.NullableQuantity <em>Quantity</em>}'.
* <!-- begin-user-doc -->
......@@ -76,6 +87,17 @@ public interface QuantitiesPackage extends EPackage {
*/
EDataType getQuantity();
/**
* Returns the meta object for data type '{@link java.time.LocalTime <em>Local Time</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for data type '<em>Local Time</em>'.
* @see java.time.LocalTime
* @model instanceClass="java.time.LocalTime"
* @generated
*/
EDataType getLocalTime();
/**
* Returns the factory that creates the instances of the model.
* <!-- begin-user-doc -->
......@@ -108,6 +130,15 @@ public interface QuantitiesPackage extends EPackage {
* @generated
*/
EDataType QUANTITY = eINSTANCE.getQuantity();
/**
* The meta object literal for the '<em>Local Time</em>' data type.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see java.time.LocalTime
* @see de.hftstuttgart.cityunits.model.quantities.impl.QuantitiesPackageImpl#getLocalTime()
* @generated
*/
EDataType LOCAL_TIME = eINSTANCE.getLocalTime();
}
......
......@@ -5,6 +5,7 @@ package de.hftstuttgart.cityunits.model.quantities.impl;
import de.hftstuttgart.cityunits.model.NullableQuantity;
import de.hftstuttgart.cityunits.model.quantities.*;
import java.time.LocalTime;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
......@@ -73,6 +74,8 @@ public class QuantitiesFactoryImpl extends EFactoryImpl implements QuantitiesFac
switch (eDataType.getClassifierID()) {
case QuantitiesPackage.QUANTITY:
return createQuantityFromString(eDataType, initialValue);
case QuantitiesPackage.LOCAL_TIME:
return createLocalTimeFromString(eDataType, initialValue);
default:
throw new IllegalArgumentException("The datatype '" + eDataType.getName() + "' is not a valid classifier");
}
......@@ -88,6 +91,8 @@ public class QuantitiesFactoryImpl extends EFactoryImpl implements QuantitiesFac
switch (eDataType.getClassifierID()) {
case QuantitiesPackage.QUANTITY:
return convertQuantityToString(eDataType, instanceValue);
case QuantitiesPackage.LOCAL_TIME:
return convertLocalTimeToString(eDataType, instanceValue);
default:
throw new IllegalArgumentException("The datatype '" + eDataType.getName() + "' is not a valid classifier");
}
......@@ -120,6 +125,33 @@ public class QuantitiesFactoryImpl extends EFactoryImpl implements QuantitiesFac
return super.convertToString(eDataType, instanceValue);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public LocalTime createLocalTime(final String it) {
return java.time.LocalTime.parse(it);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public LocalTime createLocalTimeFromString(EDataType eDataType, String initialValue) {
return createLocalTime(initialValue);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public String convertLocalTimeToString(EDataType eDataType, Object instanceValue) {
return super.convertToString(eDataType, instanceValue);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
......
......@@ -5,6 +5,7 @@ package de.hftstuttgart.cityunits.model.quantities.impl;
import de.hftstuttgart.cityunits.model.NullableQuantity;
import de.hftstuttgart.cityunits.model.quantities.QuantitiesFactory;
import de.hftstuttgart.cityunits.model.quantities.QuantitiesPackage;
import java.time.LocalTime;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.impl.EPackageImpl;
......@@ -23,6 +24,13 @@ public class QuantitiesPackageImpl extends EPackageImpl implements QuantitiesPac
*/
private EDataType quantityEDataType = null;
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private EDataType localTimeEDataType = null;
/**
* Creates an instance of the model <b>Package</b>, registered with
* {@link org.eclipse.emf.ecore.EPackage.Registry EPackage.Registry} by the package
......@@ -94,6 +102,16 @@ public class QuantitiesPackageImpl extends EPackageImpl implements QuantitiesPac
return quantityEDataType;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public EDataType getLocalTime() {
return localTimeEDataType;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
......@@ -124,6 +142,7 @@ public class QuantitiesPackageImpl extends EPackageImpl implements QuantitiesPac
// Create data types
quantityEDataType = createEDataType(QUANTITY);
localTimeEDataType = createEDataType(LOCAL_TIME);
}
/**
......@@ -151,6 +170,7 @@ public class QuantitiesPackageImpl extends EPackageImpl implements QuantitiesPac
// Initialize data types
initEDataType(quantityEDataType, NullableQuantity.class, "Quantity", IS_SERIALIZABLE, !IS_GENERATED_INSTANCE_CLASS);
initEDataType(localTimeEDataType, LocalTime.class, "LocalTime", IS_SERIALIZABLE, !IS_GENERATED_INSTANCE_CLASS);
// Create resource
createResource(eNS_URI);
......
......@@ -10,4 +10,5 @@ Import-Package: de.hftstuttgart.cityunits.model,
Require-Bundle: javax.measure.unit-api,
tech.units.indriya,
uom-lib-common;bundle-version="[2.1.0,3.0.0)",
org.eclipse.emf.ecore
org.eclipse.emf.ecore,
org.junit.jupiter.api
package de.hftstuttgart.cityunits.tests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class DayOfTimeTest {
@Test
void testCreationFromString() {
var time = LocalTime.parse("12:34:11");
assertEquals("12:34:11", time.toString(), "LocalTime conversion to and from ISO format not working!");
}
@Test
void testWrongString() {
Assertions.assertThrowsExactly(DateTimeParseException.class, () -> LocalTime.parse("12:34:60"),
"DateTimeParseException was expected!");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment