DurationWithTime is a simple record 55/103155/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Nov 2022 17:01:16 +0000 (18:01 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Nov 2022 17:01:16 +0000 (18:01 +0100)
This is a pure value holder, use a record for it.

Change-Id: I3f6743cfbbe74bd7b83acb0b9d8bfb0265f03540
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ConcurrentDurationStatisticsTracker.java
common/util/src/main/java/org/opendaylight/yangtools/util/DurationStatisticsTracker.java
common/util/src/main/java/org/opendaylight/yangtools/util/DurationWithTime.java
common/util/src/test/java/org/opendaylight/yangtools/util/SynchronizedDurationStatsTrackerTest.java

index d58e8df35c9f2bde43a69b701b15d5e4241767fb..7e7283842bf326e8d1ff80f48ed077be81fc220c 100644 (file)
@@ -58,11 +58,11 @@ class ConcurrentDurationStatisticsTracker extends DurationStatisticsTracker {
          * the number of volatile operations.
          */
         final var currentShortest = (DurationWithTime) SHORTEST.getAcquire(this);
-        if (currentShortest == null || duration < currentShortest.getDuration()) {
+        if (currentShortest == null || duration < currentShortest.duration()) {
             updateShortest(currentShortest, duration);
         }
         final var currentLongest = (DurationWithTime) LONGEST.getAcquire(this);
-        if (currentLongest == null || duration > currentLongest.getDuration()) {
+        if (currentLongest == null || duration > currentLongest.duration()) {
             updateLongest(currentLongest, duration);
         }
     }
@@ -73,7 +73,7 @@ class ConcurrentDurationStatisticsTracker extends DurationStatisticsTracker {
         var expected = prev;
         while (true) {
             final var witness = (DurationWithTime) SHORTEST.compareAndExchangeRelease(this, expected, newObj);
-            if (witness == expected || duration >= witness.getDuration()) {
+            if (witness == expected || duration >= witness.duration()) {
                 break;
             }
             expected = witness;
@@ -86,7 +86,7 @@ class ConcurrentDurationStatisticsTracker extends DurationStatisticsTracker {
         var expected = prev;
         while (true) {
             final var witness = (DurationWithTime) LONGEST.compareAndExchangeRelease(this, expected, newObj);
-            if (witness == expected || duration <= witness.getDuration()) {
+            if (witness == expected || duration <= witness.duration()) {
                 break;
             }
             expected = witness;
index 0d5555f7ed16c69d6e62e716cab98c51143fdb6b..d85164149e43a5748fdfd5f0be6015b42ff87fa9 100644 (file)
@@ -165,11 +165,11 @@ public abstract class DurationStatisticsTracker {
     }
 
     private static long getDuration(final DurationWithTime current) {
-        return current == null ? 0L : current.getDuration();
+        return current == null ? 0L : current.duration();
     }
 
     private static long getTimeMillis(final DurationWithTime current) {
-        return current == null ? 0L : current.getTimeMillis();
+        return current == null ? 0L : current.timeMillis();
     }
 
     private static String formatDuration(final double duration, final Long timeStamp) {
@@ -187,10 +187,7 @@ public abstract class DurationStatisticsTracker {
     }
 
     private static String formatDuration(final DurationWithTime current) {
-        if (current == null) {
-            return formatDuration(0, null);
-        }
-        return formatDuration(current.getDuration(), current.getTimeMillis());
+        return current == null ? formatDuration(0, null) : formatDuration(current.duration(), current.timeMillis());
     }
 
     private static TimeUnit chooseUnit(final long nanos) {
index aa77678206a004cd5766be92699d784b2440f5c6..acf9263249fff0a35958aa2839c638ff67fe7856 100644 (file)
@@ -8,22 +8,8 @@
 package org.opendaylight.yangtools.util;
 
 /**
- * Utility holder for a duration/time of occurance.
+ * Utility holder for a duration/time of occurrence.
  */
-final class DurationWithTime {
-    private final long duration;
-    private final long timeMillis;
-
-    DurationWithTime(final long duration, final long timeMillis) {
-        this.duration = duration;
-        this.timeMillis = timeMillis;
-    }
-
-    long getDuration() {
-        return duration;
-    }
-
-    long getTimeMillis() {
-        return timeMillis;
-    }
-}
\ No newline at end of file
+record DurationWithTime(long duration, long timeMillis) {
+    // Nothing else
+}
index 050c5737c2e6078636333a46419ea9f9bb05f026..f23a73215c89bcc74be56b0a65d884147b550fee 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertNull;
 import org.junit.Test;
 
 public class SynchronizedDurationStatsTrackerTest {
-
     @Test
     public void testAllMethodsOfSynchronizedDurationStatsTracker() {
         final SynchronizedDurationStatsTracker statsTracker = new SynchronizedDurationStatsTracker();
@@ -21,9 +20,9 @@ public class SynchronizedDurationStatsTrackerTest {
         statsTracker.addDuration(2000);
         statsTracker.addDuration(3000);
 
-        assertEquals("Shortest recorded duration should be '1000'.", 1000, statsTracker.getShortest().getDuration());
+        assertEquals("Shortest recorded duration should be '1000'.", 1000, statsTracker.getShortest().duration());
         assertEquals("Average recorded duration should be '2000'.", 2000, statsTracker.getAverageDuration(), 0.0001);
-        assertEquals("Longest recorded duration should be '3000'.", 3000, statsTracker.getLongest().getDuration());
+        assertEquals("Longest recorded duration should be '3000'.", 3000, statsTracker.getLongest().duration());
         assertEquals("Total recorded duration count should be '3'.", 3, statsTracker.getTotalDurations());
 
         statsTracker.reset();