2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.cluster.datastore.messages;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
17 public class CanCommitTransactionReply extends VersionedExternalizableMessage {
18 private static final CanCommitTransactionReply YES =
19 new CanCommitTransactionReply(true, DataStoreVersions.CURRENT_VERSION);
20 private static final CanCommitTransactionReply NO =
21 new CanCommitTransactionReply(false, DataStoreVersions.CURRENT_VERSION);
24 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
25 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
28 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
29 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
31 private boolean canCommit;
33 public CanCommitTransactionReply() {
36 private CanCommitTransactionReply(final boolean canCommit, final short version) {
38 this.canCommit = canCommit;
41 public boolean getCanCommit() {
46 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
47 super.readExternal(in);
48 canCommit = in.readBoolean();
52 public void writeExternal(ObjectOutput out) throws IOException {
53 super.writeExternal(out);
54 out.writeBoolean(canCommit);
59 protected Object newLegacySerializedInstance() {
60 return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
64 public String toString() {
65 return "CanCommitTransactionReply [canCommit=" + canCommit + ", version=" + getVersion() + "]";
68 public static CanCommitTransactionReply yes(short version) {
69 return version == DataStoreVersions.CURRENT_VERSION ? YES : new CanCommitTransactionReply(true, version);
72 public static CanCommitTransactionReply no(short version) {
73 return version == DataStoreVersions.CURRENT_VERSION ? NO : new CanCommitTransactionReply(false, version);
76 public static CanCommitTransactionReply fromSerializable(final Object serializable) {
77 if(serializable instanceof CanCommitTransactionReply) {
78 return (CanCommitTransactionReply)serializable;
80 ThreePhaseCommitCohortMessages.CanCommitTransactionReply serialized =
81 (ThreePhaseCommitCohortMessages.CanCommitTransactionReply) serializable;
82 return serialized.getCanCommit() ? YES : NO;
86 public static boolean isSerializedType(Object message) {
87 return message instanceof CanCommitTransactionReply ||
88 message instanceof ThreePhaseCommitCohortMessages.CanCommitTransactionReply;