tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wk...@apache.org
Subject [1/2] incubator-tamaya-sandbox git commit: TAMAYA-234: https://issues.apache.org/jira/browse/TAMAYA-234
Date Tue, 31 Jan 2017 22:19:16 GMT
Repository: incubator-tamaya-sandbox
Updated Branches:
  refs/heads/master 6b7959bb3 -> d32ff6ba0


TAMAYA-234: https://issues.apache.org/jira/browse/TAMAYA-234

Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/36eaf43d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/36eaf43d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/36eaf43d

Branch: refs/heads/master
Commit: 36eaf43d45d149f0d152eec28f292c4caf0525e4
Parents: 7f853db
Author: Werner Keil <wkeil@apache.org>
Authored: Tue Jan 31 23:18:45 2017 +0100
Committer: Werner Keil <wkeil@apache.org>
Committed: Tue Jan 31 23:18:45 2017 +0100

----------------------------------------------------------------------
 uom/pom.xml                                     | 74 +++++++++++++++-----
 .../org/apache/tamaya/uom/UnitConverter.java    | 64 +++++++++++++++++
 .../org.apache.tamaya.spi.PropertyConverter     | 19 +++++
 .../apache/tamaya/uom/UnitConverterTest.java    | 47 +++++++++++++
 4 files changed, 186 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/36eaf43d/uom/pom.xml
----------------------------------------------------------------------
diff --git a/uom/pom.xml b/uom/pom.xml
index 72f9a98..a532db6 100644
--- a/uom/pom.xml
+++ b/uom/pom.xml
@@ -1,22 +1,14 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!--
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements.  See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership.  The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License.  You may obtain a copy current the License at
-
-   http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied.  See the License for the
-specific language governing permissions and limitations
-under the License.
--->
+<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
+    license agreements. See the NOTICE file distributed with this work for additional 
+    information regarding copyright ownership. The ASF licenses this file to 
+    you under the Apache License, Version 2.0 (the "License"); you may not use 
+    this file except in compliance with the License. You may obtain a copy current 
+    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
+    by applicable law or agreed to in writing, software distributed under the 
+    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
+    OF ANY KIND, either express or implied. See the License for the specific 
+    language governing permissions and limitations under the License. -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
@@ -33,6 +25,52 @@ under the License.
 
     <properties>
         <jsr363.version>1.0</jsr363.version>
+        <jsr363.ri.version>1.0.1</jsr363.ri.version>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tamaya</groupId>
+            <artifactId>tamaya-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.tamaya</groupId>
+            <artifactId>tamaya-core</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>java-hamcrest</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>javax.measure</groupId>
+            <artifactId>unit-api</artifactId>
+            <version>${jsr363.version}</version>
+        </dependency>
+        
+        <!-- TODO could use scope test maybe -->
+        <dependency>
+            <groupId>tec.units</groupId>
+            <artifactId>unit-ri</artifactId>
+            <version>${jsr363.ri.version}</version>
+        </dependency>
+    </dependencies>
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/36eaf43d/uom/src/main/java/org/apache/tamaya/uom/UnitConverter.java
----------------------------------------------------------------------
diff --git a/uom/src/main/java/org/apache/tamaya/uom/UnitConverter.java b/uom/src/main/java/org/apache/tamaya/uom/UnitConverter.java
new file mode 100644
index 0000000..d1baa43
--- /dev/null
+++ b/uom/src/main/java/org/apache/tamaya/uom/UnitConverter.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.uom;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.apache.tamaya.spi.PropertyConverter;
+import javax.measure.Unit;
+import javax.measure.format.UnitFormat;
+import javax.measure.spi.ServiceProvider;
+
+import java.util.regex.Pattern;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Converter from {@code String} to a JSR 363 {@code Unit}.
+ *
+ * @see Unit
+ * @author wkeil
+ */
+// TODO not sure, if this could clash with JSR 363's own UnitConverter, but unless that's
used here, it might be OK
+public class UnitConverter implements PropertyConverter<Unit> {
+	private static final String PATTERN_REGEX = "(\\+|-)?\\d+";
+	private static final Pattern IS_INTEGER_VALUE = Pattern.compile(PATTERN_REGEX);
+
+	@Override
+	public Unit convert(String value, ConversionContext context) {
+		String trimmed = requireNonNull(value).trim();
+		addSupportedFormats(context);
+		UnitFormat format = ServiceProvider.current().getUnitFormatService().getUnitFormat();
+
+		Unit result = null;
+
+		try {
+			result = format.parse(trimmed);
+
+		} catch (RuntimeException e) {
+			result = null; // Give the next converter a change. Read the JavaDoc
+							// of convert
+		}
+
+		return result;
+	}
+
+	private void addSupportedFormats(ConversionContext context) {
+		context.addSupportedFormats(UnitConverter.class, "All Units supported by JSR 363");
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/36eaf43d/uom/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
----------------------------------------------------------------------
diff --git a/uom/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
b/uom/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
new file mode 100644
index 0000000..8ddb69f
--- /dev/null
+++ b/uom/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy current the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+org.apache.tamaya.uom.UnitConverter

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/36eaf43d/uom/src/test/java/org/apache/tamaya/uom/UnitConverterTest.java
----------------------------------------------------------------------
diff --git a/uom/src/test/java/org/apache/tamaya/uom/UnitConverterTest.java b/uom/src/test/java/org/apache/tamaya/uom/UnitConverterTest.java
new file mode 100644
index 0000000..6e64fb1
--- /dev/null
+++ b/uom/src/test/java/org/apache/tamaya/uom/UnitConverterTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.uom;
+
+import org.apache.tamaya.spi.ConversionContext;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import tec.units.ri.unit.Units;
+
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.*;
+
+import javax.measure.Unit;
+
+public class UnitConverterTest {
+	private UnitConverter converter = new UnitConverter();
+
+	@Test
+	public void canConvertUnitInformation() {
+
+		ConversionContext context = Mockito.mock(ConversionContext.class);
+
+		Unit<?> unit = converter.convert("m", context);
+
+		assertThat("Converter failed to convert input value " + unit, notNullValue());
+		assertEquals(unit, Units.METRE);
+	}
+
+}


Mime
View raw message