CommitInfo forward compatibility 62/109762/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 13 Jan 2024 19:29:25 +0000 (20:29 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 13 Jan 2024 19:57:38 +0000 (20:57 +0100)
This patch refactors EmptyCommitInfo to implement the future CommitInfo
interaface -- carrying uuid/time information and being Serializable.

JIRA: YANGTOOLS-1554
Change-Id: Ibe5070b17fe50fc8aa7f75a7483aa9f0e9ef5139
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CI.java [new file with mode: 0644]
common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CommitInfo.java
common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/EmptyCommitInfo.java [deleted file]
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java
common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java

diff --git a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CI.java b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CI.java
new file mode 100644 (file)
index 0000000..7736b9e
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.common.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.Futures;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.time.Instant;
+import java.util.UUID;
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Default {@link CommitInfo} implementation.
+ */
+record CI(UUID uuid, Instant instant) implements CommitInfo, Serializable {
+    @java.io.Serial
+    private static final long serialVersionUID = 0L;
+
+    static final @NonNull CI EMPTY = new CI(null, null);
+    static final @NonNull FluentFuture<@NonNull CommitInfo> EMPTY_FUTURE =
+        FluentFuture.from(Futures.immediateFuture(EMPTY));
+
+    static @NonNull CI of(final UUID uuid, final Instant time) {
+        return time != null || uuid != null ? new CI(uuid, time) : EMPTY;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(CommitInfo.class).omitNullValues()
+            .add("uuid", uuid)
+            .add("instant", instant)
+            .toString();
+    }
+
+    @java.io.Serial
+    private Object readResolve() throws ObjectStreamException {
+        return instant != null || uuid != null ? this : EMPTY;
+    }
+}
index e4965fdacd289547add12f5c8a97b0a95e9551f9..08a409c4063c0c03b169f70fe838ce23c0acb74d 100644 (file)
@@ -15,8 +15,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
  * Base interface for tagging information about a successful commit. This can include various ways of identifying
  * the resulting changeset, timing information or any other piece of data about the commit itself the implementation
  * deems interesting to the client.
- *
- * @author Robert Varga
  */
 @Beta
 @NonNullByDefault
@@ -27,7 +25,7 @@ public interface CommitInfo {
      * @return An empty {@link CommitInfo} instance.
      */
     static CommitInfo empty() {
-        return EmptyCommitInfo.INSTANCE;
+        return CI.EMPTY;
     }
 
     /**
@@ -36,6 +34,6 @@ public interface CommitInfo {
      * @return An empty {@link CommitInfo} instance enclosed in a completed future.
      */
     static FluentFuture<CommitInfo> emptyFluentFuture() {
-        return EmptyCommitInfo.FLUENT_INSTANCE;
+        return CI.EMPTY_FUTURE;
     }
 }
diff --git a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/EmptyCommitInfo.java b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/EmptyCommitInfo.java
deleted file mode 100644 (file)
index 267a7d3..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.common.api;
-
-import com.google.common.util.concurrent.FluentFuture;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.util.concurrent.FluentFutures;
-
-/**
- * Empty commit info singleton. Useful when {@link AsyncWriteTransaction#commit()} has nothing more to say.
- *
- * @author Robert Varga
- */
-@NonNullByDefault
-final class EmptyCommitInfo implements CommitInfo {
-    static final CommitInfo INSTANCE = new EmptyCommitInfo();
-    static final FluentFuture<CommitInfo> FLUENT_INSTANCE = FluentFutures.immediateFluentFuture(INSTANCE);
-
-    private EmptyCommitInfo() {
-        // Hidden
-    }
-}
index 33f1c9a16c1f45c4641cbf1a19f20acedf0d49f9..ee39a65c96d1260450567f3eb2fc0d2c09d38ae0 100644 (file)
@@ -14,11 +14,11 @@ import org.junit.jupiter.api.Test;
 class CommitInfoTest {
     @Test
     void testEmpty() {
-        assertSame(EmptyCommitInfo.INSTANCE, CommitInfo.empty());
+        assertSame(CI.EMPTY, CommitInfo.empty());
     }
 
     @Test
     void testEmptyFluentFuture() {
-        assertSame(EmptyCommitInfo.FLUENT_INSTANCE, CommitInfo.emptyFluentFuture());
+        assertSame(CI.EMPTY_FUTURE, CommitInfo.emptyFluentFuture());
     }
 }
index 43546e80ebd290da7afa63406cd283f9291371e1..881ab334ccc5b80ac76d43f299f2611453d53dab 100644 (file)
@@ -16,8 +16,8 @@ import org.junit.jupiter.api.Test;
 class EmptyCommitInfoTest {
     @Test
     void testFuture() throws Exception {
-        final var future = EmptyCommitInfo.FLUENT_INSTANCE;
+        final var future = CI.EMPTY_FUTURE;
         assertTrue(future.isDone());
-        assertSame(EmptyCommitInfo.INSTANCE, Futures.getDone(future));
+        assertSame(CI.EMPTY, Futures.getDone(future));
     }
 }