2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.messaging;
10 import com.google.common.base.Preconditions;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.yangtools.concepts.Identifier;
19 * Identifier for a message slice that is composed of a client-supplied Identifier and an internal counter value.
21 * @author Thomas Pantelis
23 final class MessageSliceIdentifier implements Identifier {
24 private static final long serialVersionUID = 1L;
25 private static final AtomicLong ID_COUNTER = new AtomicLong(1);
27 private final Identifier clientIdentifier;
28 private final long messageId;
30 MessageSliceIdentifier(final Identifier clientIdentifier) {
31 this(clientIdentifier, ID_COUNTER.getAndIncrement());
34 private MessageSliceIdentifier(final Identifier clientIdentifier, final long messageId) {
35 this.clientIdentifier = Preconditions.checkNotNull(clientIdentifier);
36 this.messageId = messageId;
39 Identifier getClientIdentifier() {
40 return clientIdentifier;
44 public int hashCode() {
47 result = prime * result + clientIdentifier.hashCode();
48 result = prime * result + (int) (messageId ^ messageId >>> 32);
53 public boolean equals(Object obj) {
58 if (!(obj instanceof MessageSliceIdentifier)) {
62 MessageSliceIdentifier other = (MessageSliceIdentifier) obj;
63 return other.clientIdentifier.equals(clientIdentifier) && other.messageId == messageId;
67 public String toString() {
68 return "MessageSliceIdentifier [clientIdentifier=" + clientIdentifier + ", messageId=" + messageId + "]";
71 private Object writeReplace() {
72 return new Proxy(this);
75 private static class Proxy implements Externalizable {
76 private static final long serialVersionUID = 1L;
78 private MessageSliceIdentifier messageSliceId;
80 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
81 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
82 @SuppressWarnings("checkstyle:RedundantModifier")
86 Proxy(MessageSliceIdentifier messageSliceId) {
87 this.messageSliceId = messageSliceId;
91 public void writeExternal(ObjectOutput out) throws IOException {
92 out.writeObject(messageSliceId.clientIdentifier);
93 out.writeLong(messageSliceId.messageId);
97 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
98 messageSliceId = new MessageSliceIdentifier((Identifier) in.readObject(), in.readLong());
101 private Object readResolve() {
102 return messageSliceId;