From: Robert Varga Date: Sat, 13 Jan 2024 19:29:25 +0000 (+0100) Subject: CommitInfo forward compatibility X-Git-Tag: v13.0.0~32 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=730b24538dfa113ea18ab874d932d5726aef96ce CommitInfo forward compatibility 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 --- 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 index 0000000000..7736b9e356 --- /dev/null +++ b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CI.java @@ -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; + } +} diff --git a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CommitInfo.java b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CommitInfo.java index e4965fdacd..08a409c406 100644 --- a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CommitInfo.java +++ b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/CommitInfo.java @@ -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 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 index 267a7d37f4..0000000000 --- a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/EmptyCommitInfo.java +++ /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 FLUENT_INSTANCE = FluentFutures.immediateFluentFuture(INSTANCE); - - private EmptyCommitInfo() { - // Hidden - } -} diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java index 33f1c9a16c..ee39a65c96 100644 --- a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/CommitInfoTest.java @@ -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()); } } diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java index 43546e80eb..881ab334cc 100644 --- a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java +++ b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/EmptyCommitInfoTest.java @@ -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)); } }