7736b9e356729acfc3a1e36f7a1055cfd218c711
[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.io.Serializable;
16 import java.time.Instant;
17 import java.util.UUID;
18 import org.eclipse.jdt.annotation.NonNull;
19
20 /**
21  * Default {@link CommitInfo} implementation.
22  */
23 record CI(UUID uuid, Instant instant) implements CommitInfo, Serializable {
24     @java.io.Serial
25     private static final long serialVersionUID = 0L;
26
27     static final @NonNull CI EMPTY = new CI(null, null);
28     static final @NonNull FluentFuture<@NonNull CommitInfo> EMPTY_FUTURE =
29         FluentFuture.from(Futures.immediateFuture(EMPTY));
30
31     static @NonNull CI of(final UUID uuid, final Instant time) {
32         return time != null || uuid != null ? new CI(uuid, time) : EMPTY;
33     }
34
35     @Override
36     public String toString() {
37         return MoreObjects.toStringHelper(CommitInfo.class).omitNullValues()
38             .add("uuid", uuid)
39             .add("instant", instant)
40             .toString();
41     }
42
43     @java.io.Serial
44     private Object readResolve() throws ObjectStreamException {
45         return instant != null || uuid != null ? this : EMPTY;
46     }
47 }