Sprinkle @NonNull annotations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / FrontendHistoryMetadata.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.primitives.UnsignedLong;
16 import java.io.DataInput;
17 import java.io.DataOutput;
18 import java.io.IOException;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
24 import org.opendaylight.yangtools.concepts.WritableObject;
25 import org.opendaylight.yangtools.concepts.WritableObjects;
26
27 public final class FrontendHistoryMetadata implements WritableObject {
28     private final @NonNull ImmutableUnsignedLongSet purgedTransactions;
29     private final @NonNull ImmutableMap<UnsignedLong, Boolean> closedTransactions;
30     private final long historyId;
31     private final long cookie;
32     private final boolean closed;
33
34     public FrontendHistoryMetadata(final long historyId, final long cookie, final boolean closed,
35             final Map<UnsignedLong, Boolean> closedTransactions, final ImmutableUnsignedLongSet purgedTransactions) {
36         this.historyId = historyId;
37         this.cookie = cookie;
38         this.closed = closed;
39         this.closedTransactions = ImmutableMap.copyOf(closedTransactions);
40         this.purgedTransactions = requireNonNull(purgedTransactions);
41     }
42
43     public long getHistoryId() {
44         return historyId;
45     }
46
47     public long getCookie() {
48         return cookie;
49     }
50
51     public boolean isClosed() {
52         return closed;
53     }
54
55     public ImmutableMap<UnsignedLong, Boolean> getClosedTransactions() {
56         return closedTransactions;
57     }
58
59     public ImmutableUnsignedLongSet getPurgedTransactions() {
60         return purgedTransactions;
61     }
62
63     @Override
64     public void writeTo(final DataOutput out) throws IOException {
65         WritableObjects.writeLongs(out, historyId, cookie);
66         out.writeBoolean(closed);
67
68         final int purgedSize = purgedTransactions.size();
69         WritableObjects.writeLongs(out, closedTransactions.size(), purgedSize);
70         for (Entry<UnsignedLong, Boolean> e : closedTransactions.entrySet()) {
71             WritableObjects.writeLong(out, e.getKey().longValue());
72             out.writeBoolean(e.getValue());
73         }
74         purgedTransactions.writeRangesTo(out, purgedSize);
75     }
76
77     public static FrontendHistoryMetadata readFrom(final DataInput in) throws IOException {
78         byte header = WritableObjects.readLongHeader(in);
79         final long historyId = WritableObjects.readFirstLong(in, header);
80         final long cookie = WritableObjects.readSecondLong(in, header);
81         final boolean closed = in.readBoolean();
82
83         header = WritableObjects.readLongHeader(in);
84         long ls = WritableObjects.readFirstLong(in, header);
85         Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
86         final int csize = (int) ls;
87
88         ls = WritableObjects.readSecondLong(in, header);
89         Verify.verify(ls >= 0 && ls <= Integer.MAX_VALUE);
90         final int psize = (int) ls;
91
92         final Map<UnsignedLong, Boolean> closedTransactions = new HashMap<>(csize);
93         for (int i = 0; i < csize; ++i) {
94             final UnsignedLong key = UnsignedLong.fromLongBits(WritableObjects.readLong(in));
95             final Boolean value = in.readBoolean();
96             closedTransactions.put(key, value);
97         }
98         final var purgedTransactions = ImmutableUnsignedLongSet.readFrom(in, psize);
99
100         return new FrontendHistoryMetadata(historyId, cookie, closed, closedTransactions, purgedTransactions);
101     }
102
103     @Override
104     public String toString() {
105         return MoreObjects.toStringHelper(FrontendHistoryMetadata.class).add("historyId", historyId)
106                 .add("cookie", cookie).add("closed", closed).add("closedTransactions", closedTransactions)
107                 .add("purgedTransactions", purgedTransactions).toString();
108     }
109 }