Expose uuid/instant from CommitInfo
[mdsal.git] / common / mdsal-common-api / src / main / java / org / opendaylight / mdsal / common / api / CI.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  * Copyright (c) 2024 PANTHEON.tech, s.r.o.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.mdsal.common.api;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.io.ObjectStreamException;
15 import java.time.Instant;
16 import java.util.UUID;
17 import org.eclipse.jdt.annotation.NonNull;
18
19 /**
20  * Default {@link CommitInfo} implementation.
21  */
22 record CI(UUID uuid, Instant instant) implements CommitInfo {
23     @java.io.Serial
24     private static final long serialVersionUID = 0L;
25
26     static final @NonNull CI EMPTY = new CI(null, null);
27     static final @NonNull FluentFuture<@NonNull CommitInfo> EMPTY_FUTURE =
28         FluentFuture.from(Futures.immediateFuture(EMPTY));
29
30     static @NonNull CI of(final UUID uuid, final Instant time) {
31         return time != null || uuid != null ? new CI(uuid, time) : EMPTY;
32     }
33
34     @Override
35     public String toString() {
36         return MoreObjects.toStringHelper(CommitInfo.class).omitNullValues()
37             .add("uuid", uuid)
38             .add("instant", instant)
39             .toString();
40     }
41
42     @java.io.Serial
43     private Object readResolve() throws ObjectStreamException {
44         return instant != null || uuid != null ? this : EMPTY;
45     }
46 }