This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new 721e3c5ca1 refactor: Remove unused code and some minor code 
improvements (#2985)
721e3c5ca1 is described below

commit 721e3c5ca1e912f348055d1b9fb21fb9e34c5a06
Author: Philipp Zehnder <[email protected]>
AuthorDate: Mon Jul 1 10:30:27 2024 +0200

    refactor: Remove unused code and some minor code improvements (#2985)
---
 .../src/main/java/com/github/jqudt/Multiplier.java |   5 -
 .../src/main/java/com/github/jqudt/Quantity.java   |  20 ++-
 .../com/github/jqudt/uo/UnitOntologyFactory.java   | 156 ---------------------
 .../apache/streampipes/units/UnitCollector.java    |   4 +-
 .../org/apache/streampipes/units/UnitProvider.java |  23 +--
 .../streampipes/units/test/TestUnitProvider.java   |  10 +-
 6 files changed, 24 insertions(+), 194 deletions(-)

diff --git 
a/streampipes-measurement-units/src/main/java/com/github/jqudt/Multiplier.java 
b/streampipes-measurement-units/src/main/java/com/github/jqudt/Multiplier.java
index 46654c479c..f815e9c6a0 100644
--- 
a/streampipes-measurement-units/src/main/java/com/github/jqudt/Multiplier.java
+++ 
b/streampipes-measurement-units/src/main/java/com/github/jqudt/Multiplier.java
@@ -11,11 +11,6 @@ public class Multiplier implements Serializable{
        private double offset;
        private double multiplier;
 
-       public Multiplier(double offset, double multiplier) {
-               this.offset = offset;
-               this.multiplier = multiplier;
-       }
-
        public Multiplier() {}
 
        public double getOffset() {
diff --git 
a/streampipes-measurement-units/src/main/java/com/github/jqudt/Quantity.java 
b/streampipes-measurement-units/src/main/java/com/github/jqudt/Quantity.java
index e84d66dc5c..1a77989188 100644
--- a/streampipes-measurement-units/src/main/java/com/github/jqudt/Quantity.java
+++ b/streampipes-measurement-units/src/main/java/com/github/jqudt/Quantity.java
@@ -52,11 +52,16 @@ public class Quantity {
                if (unit.getResource().equals(newUnit.getResource())) return 
this; // nothing to be done
 
                if (!unit.getType().equals(newUnit.getType())) {
-                       LOG.error("The new unit does not have the same parent 
type " +
-                                       "(source: " + unit.getType() + "; 
target: " + newUnit.getType() + ")");
+      LOG.error(
+          "The new unit does not have the same parent type (source: {}; 
target: {})",
+          unit.getType(),
+          newUnit.getType()
+      );
                        throw new IllegalAccessException(
-                                       "The new unit does not have the same 
parent type " +
-                                                       "(source: " + 
unit.getType() + "; target: " + newUnit.getType() + ")"
+          "The new unit does not have the same parent type (source: %s; 
target: %s)".formatted(
+              unit.getType(),
+              newUnit.getType()
+          )
                        );
                }
 
@@ -73,8 +78,11 @@ public class Quantity {
                return newMeasurement;
        }
 
+       @Override
        public String toString() {
-               return "" + getValue() + " " + getUnit().toString();
+               return "Quantity{" +
+                               "value=" + value +
+                               ", unit=" + unit +
+                               '}';
        }
-
 }
diff --git 
a/streampipes-measurement-units/src/main/java/com/github/jqudt/uo/UnitOntologyFactory.java
 
b/streampipes-measurement-units/src/main/java/com/github/jqudt/uo/UnitOntologyFactory.java
deleted file mode 100644
index 76ac520524..0000000000
--- 
a/streampipes-measurement-units/src/main/java/com/github/jqudt/uo/UnitOntologyFactory.java
+++ /dev/null
@@ -1,156 +0,0 @@
-/* Copyright (C) 2012  Egon Willighagen <[email protected]>
- *
- * License: new BSD
- */
-package com.github.jqudt.uo;
-
-import com.github.jqudt.Unit;
-import com.github.jqudt.onto.UnitFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-public class UnitOntologyFactory {
-
-  private static UnitOntologyFactory factory = null;
-
-  private static final Logger LOG = 
LoggerFactory.getLogger(UnitOntologyFactory.class);
-
-  @SuppressWarnings("serial")
-  private Map<String, String> uo2qudt = new HashMap<String, String>() {
-    // a helper method
-    String longURI(String shortened) {
-      if (shortened.startsWith("uo:")) {
-        return "http://purl.obolibrary.org/obo/"; + shortened.substring(3);
-      }
-      if (shortened.startsWith("ops:")) {
-        return "http://www.openphacts.org/units/"; + shortened.substring(4);
-      }
-      return null;
-    }
-
-    // the next defines all mappings from the Unit Ontology to the QUDT
-    {
-      put(longURI("uo:EFO_0004374"), longURI("ops:MilligramPerDeciliter"));
-      put(longURI("uo:EFO_0004385"), longURI("ops:PicogramPerMilliliter"));
-      put(longURI("uo:UO_0000009"), longURI("qudt:Kilogram"));
-      put(longURI("uo:UO_0000010"), longURI("qudt:SecondTime"));
-      put(longURI("uo:UO_0000015"), longURI("qudt:Centimeter"));
-      put(longURI("uo:UO_0000016"), longURI("qudt:Millimeter"));
-      put(longURI("uo:UO_0000017"), longURI("qudt:Micrometer"));
-      put(longURI("uo:UO_0000018"), longURI("ops:Nanometer"));
-      put(longURI("uo:UO_0000021"), longURI("qudt:Gram"));
-      put(longURI("uo:UO_0000022"), longURI("ops:Milligram"));
-      put(longURI("uo:UO_0000023"), longURI("ops:Microgram"));
-      put(longURI("uo:UO_0000024"), longURI("ops:Nanogram"));
-      put(longURI("uo:UO_0000025"), longURI("ops:Picogram"));
-      put(longURI("uo:UO_0000026"), longURI("ops:Femtogram"));
-      put(longURI("uo:UO_0000027"), longURI("qudt:DegreeCelsius"));
-      put(longURI("uo:UO_0000028"), longURI("qudt:Millisecond"));
-      put(longURI("uo:UO_0000031"), longURI("qudt:MinuteTime"));
-      put(longURI("uo:UO_0000032"), longURI("qudt:Hour"));
-      put(longURI("uo:UO_0000033"), longURI("qudt:Day"));
-      put(longURI("uo:UO_0000039"), longURI("qudt:Micromole"));
-      put(longURI("uo:UO_0000040"), longURI("qudt:Millimole"));
-      put(longURI("uo:UO_0000041"), longURI("qudt:Nanomole"));
-      put(longURI("uo:UO_0000042"), longURI("qudt:Picomole"));
-      put(longURI("uo:UO_0000043"), longURI("qudt:Femtomole"));
-      put(longURI("uo:UO_0000062"), longURI("ops:Molar"));
-      put(longURI("uo:UO_0000063"), longURI("ops:Millimolar"));
-      put(longURI("uo:UO_0000064"), longURI("ops:Micromolar"));
-      put(longURI("uo:UO_0000065"), longURI("ops:Nanomolar"));
-      put(longURI("uo:UO_0000066"), longURI("ops:Picomolar"));
-      put(longURI("uo:UO_0000073"), longURI("ops:Femtomolar"));
-      put(longURI("uo:UO_0000098"), longURI("ops:Milliliter"));
-      put(longURI("uo:UO_0000099"), longURI("qudt:Liter"));
-      put(longURI("uo:UO_0000101"), longURI("ops:Microliter"));
-      put(longURI("uo:UO_0000169"), longURI("ops:PartsPerMillion"));
-      put(longURI("uo:UO_0000173"), longURI("ops:GramPerMilliliter"));
-      put(longURI("uo:UO_0000175"), longURI("ops:GramPerLiter"));
-      put(longURI("uo:UO_0000176"), longURI("ops:MilligramPerMilliliter"));
-      put(longURI("uo:UO_0000187"), longURI("qudt:Percent"));
-      put(longURI("uo:UO_0000197"), longURI("ops:LiterPerKilogram"));
-      put(longURI("uo:UO_0000198"), longURI("ops:MilliliterPerKilogram"));
-      put(longURI("uo:UO_0000271"), longURI("ops:MicroliterPerMinute"));
-      put(longURI("uo:UO_0000272"), longURI("qudt:MillimeterOfMercury"));
-      put(longURI("uo:UO_0000274"), longURI("ops:MicrogramPerMilliliter"));
-      put(longURI("uo:UO_0000275"), longURI("ops:NanogramPerMilliliter"));
-      put(longURI("uo:UO_0000308"), longURI("ops:MilligramPerKilogram"));
-//                 put(longURI("uo:UO_0000311"), longURI(""));
-    }
-  };
-  private Map<String, String> qudt2uo = null;
-
-  private UnitOntologyFactory() {
-    // not backed up by a formal ontology (at this moment; see UnitFactory's 
constructor)
-    // instead, it uses defined mappings in uo2qudt
-
-    // also make the reverse mapping table
-    qudt2uo = new HashMap<>();
-    for (String keyURI : uo2qudt.keySet()) {
-      qudt2uo.put(uo2qudt.get(keyURI), keyURI);
-    }
-  }
-
-  public static UnitOntologyFactory getInstance() {
-    if (factory == null) {
-      factory = new UnitOntologyFactory();
-    }
-    return factory;
-  }
-
-  private static URI asURI(String resource) {
-    try {
-      return new URI(resource);
-    } catch (URISyntaxException exception) {
-      return null;
-    }
-  }
-
-  public Unit getUnit(String resource) {
-    LOG.info("resource:" + resource);
-    return getUnit(asURI(resource));
-  }
-
-  public Unit getUnit(URI resource) {
-    if (resource == null) {
-      throw new IllegalArgumentException("The URI cannot be null");
-    }
-
-    URI mappedURI = asURI(uo2qudt.get(resource.toString()));
-    if (mappedURI != null) {
-      return UnitFactory.getInstance().getUnit(mappedURI);
-    } else {
-      return null;
-    }
-  }
-
-  public List<String> getURIs(String type) {
-    URI uri;
-    try {
-      uri = new URI(type);
-    } catch (URISyntaxException exception) {
-      throw new IllegalStateException("Invalid URI: " + type, exception);
-    }
-    return getURIs(uri);
-  }
-
-  public List<String> getURIs(URI type) {
-    List<String> uris = new ArrayList<>();
-
-    List<String> qudtURIs = UnitFactory.getInstance().getURIs(type);
-    for (String qudtString : qudtURIs) {
-      String uoURI = qudt2uo.get(qudtString);
-      if (uoURI != null) {
-        uris.add(uoURI);
-      }
-    }
-    return uris;
-  }
-}
diff --git 
a/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitCollector.java
 
b/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitCollector.java
index fd542e68c1..f0ae3f4dd1 100644
--- 
a/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitCollector.java
+++ 
b/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitCollector.java
@@ -98,8 +98,8 @@ public class UnitCollector {
       "AngularVelocityUnit"
   };
 
-  private Set<Unit> availableUnits = new HashSet<>();
-  private Set<Unit> availableUnitTypes = new HashSet<>();
+  private final Set<Unit> availableUnits = new HashSet<>();
+  private final Set<Unit> availableUnitTypes = new HashSet<>();
 
   public UnitCollector() {
     collect();
diff --git 
a/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitProvider.java
 
b/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitProvider.java
index 33473b4636..b2feca9330 100644
--- 
a/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitProvider.java
+++ 
b/streampipes-measurement-units/src/main/java/org/apache/streampipes/units/UnitProvider.java
@@ -20,25 +20,20 @@ package org.apache.streampipes.units;
 
 import com.github.jqudt.Unit;
 import com.github.jqudt.onto.UnitFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.NoSuchElementException;
 import java.util.stream.Collectors;
 
 public enum UnitProvider {
 
   INSTANCE;
 
-  private static final Logger LOG = 
LoggerFactory.getLogger(UnitCollector.class);
+  private final List<Unit> availableUnitTypes = new ArrayList<>();
+  private final List<Unit> availableUnits = new ArrayList<>();
 
-  private List<Unit> availableUnitTypes = new ArrayList<>();
-  private List<Unit> availableUnits = new ArrayList<>();
-
-  private UnitFactory factory;
+  private final UnitFactory factory;
 
   UnitProvider() {
     factory = UnitFactory.getInstance();
@@ -59,18 +54,6 @@ public enum UnitProvider {
     return factory.getUnit(resourceUri);
   }
 
-  public Unit getUnitByLabel(String label) {
-    try {
-      return availableUnits.stream()
-          .filter((Unit unit) -> unit.getLabel().equals(label))
-          .findFirst()
-          .get();
-    } catch (NoSuchElementException e) {
-      LOG.error("No unit with label \"" + label + "\" found");
-      return null;
-    }
-  }
-
   public List<Unit> getUnitsByType(URI type) {
     return availableUnits
         .stream()
diff --git 
a/streampipes-measurement-units/src/test/java/org/apache/streampipes/units/test/TestUnitProvider.java
 
b/streampipes-measurement-units/src/test/java/org/apache/streampipes/units/test/TestUnitProvider.java
index 2c2c1172aa..a21ba5c3cd 100644
--- 
a/streampipes-measurement-units/src/test/java/org/apache/streampipes/units/test/TestUnitProvider.java
+++ 
b/streampipes-measurement-units/src/test/java/org/apache/streampipes/units/test/TestUnitProvider.java
@@ -20,16 +20,16 @@ package org.apache.streampipes.units.test;
 
 import org.apache.streampipes.units.UnitProvider;
 
-import com.github.jqudt.Unit;
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 public class TestUnitProvider {
 
   @Test
   public void testUnitProvider() {
-    Unit unit = 
UnitProvider.INSTANCE.getUnit("http://qudt.org/vocab/unit#KilometerPerHour";);
-    Assertions.assertEquals("Kilometer per Hour", unit.getLabel());
-    Assertions.assertEquals("km/hr", unit.getAbbreviation());
+    var unit = 
UnitProvider.INSTANCE.getUnit("http://qudt.org/vocab/unit#KilometerPerHour";);
+    assertEquals("Kilometer per Hour", unit.getLabel());
+    assertEquals("km/hr", unit.getAbbreviation());
   }
 }

Reply via email to