BUG-5280: add cds-access-api identifiers
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / LocalHistoryIdentifier.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.access.concepts;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.opendaylight.yangtools.concepts.Identifier;
17
18 /**
19  * Globally-unique identifier of a local history.
20  *
21  * @param <T> Frontend type
22  *
23  * @author Robert Varga
24  */
25 public final class LocalHistoryIdentifier<T extends FrontendType> implements Identifier {
26     private static final class Proxy<T extends FrontendType> implements Externalizable {
27         private static final long serialVersionUID = 1L;
28         private ClientIdentifier<T> clientId;
29         private long historyId;
30
31         public Proxy() {
32             // For Externalizable
33         }
34
35         Proxy(final ClientIdentifier<T> frontendId, final long historyId) {
36             this.clientId = Preconditions.checkNotNull(frontendId);
37             this.historyId = historyId;
38         }
39
40         @Override
41         public void writeExternal(final ObjectOutput out) throws IOException {
42             out.writeObject(clientId);
43             out.writeLong(historyId);
44         }
45
46         @SuppressWarnings("unchecked")
47         @Override
48         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
49             clientId = (ClientIdentifier<T>) in.readObject();
50             historyId = in.readLong();
51         }
52
53         private Object readResolve() {
54             return new LocalHistoryIdentifier<>(clientId, historyId);
55         }
56     }
57
58     private static final long serialVersionUID = 1L;
59     private final ClientIdentifier<T> clientId;
60     private final long historyId;
61
62     public LocalHistoryIdentifier(final ClientIdentifier<T> frontendId, final long historyId) {
63         this.clientId = Preconditions.checkNotNull(frontendId);
64         this.historyId = historyId;
65     }
66
67     public ClientIdentifier<T> getClienId() {
68         return clientId;
69     }
70
71     public long getHistoryId() {
72         return historyId;
73     }
74
75     @Override
76     public int hashCode() {
77         return clientId.hashCode() * 31 + Long.hashCode(historyId);
78     }
79
80     @Override
81     public boolean equals(final Object o) {
82         if (this == o) {
83             return true;
84         }
85         if (!(o instanceof LocalHistoryIdentifier)) {
86             return false;
87         }
88
89         final LocalHistoryIdentifier<?> other = (LocalHistoryIdentifier<?>) o;
90         return historyId == other.historyId && clientId.equals(other.clientId);
91     }
92
93     @Override
94     public String toString() {
95         return MoreObjects.toStringHelper(LocalHistoryIdentifier.class).add("client", clientId)
96                 .add("history", Long.toUnsignedString(historyId)).toString();
97     }
98
99     private Object writeReplace() {
100         return new Proxy<>(clientId, historyId);
101     }
102 }