Fix modernization issues
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequestBuilder.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.commands;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import com.google.common.annotations.Beta;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.concepts.Builder;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20
21 /**
22  * A reusable {@link Builder} for creating {@link ModifyTransactionRequest} message instances. Its internal state is
23  * reset when {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT
24  * thread-safe.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class ModifyTransactionRequestBuilder implements Builder<ModifyTransactionRequest>,
30         Identifiable<TransactionIdentifier> {
31     private final List<TransactionModification> modifications = new ArrayList<>(1);
32     private final TransactionIdentifier identifier;
33     private final ActorRef replyTo;
34
35     private PersistenceProtocol protocol;
36     private boolean haveSequence;
37     private long sequence;
38
39     public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
40         this.identifier = requireNonNull(identifier);
41         this.replyTo = requireNonNull(replyTo);
42     }
43
44     @Override
45     public TransactionIdentifier getIdentifier() {
46         return identifier;
47     }
48
49     private void checkNotFinished() {
50         checkState(protocol == null, "Batch has already been finished");
51     }
52
53     public void addModification(final TransactionModification modification) {
54         checkNotFinished();
55         modifications.add(requireNonNull(modification));
56     }
57
58     public void setSequence(final long sequence) {
59         checkState(!haveSequence, "Sequence has already been set");
60         this.sequence = sequence;
61         haveSequence = true;
62     }
63
64     public void setAbort() {
65         checkNotFinished();
66         // Transaction is being aborted, no need to transmit operations
67         modifications.clear();
68         protocol = PersistenceProtocol.ABORT;
69     }
70
71     public void setCommit(final boolean coordinated) {
72         checkNotFinished();
73         protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
74     }
75
76     public void setReady() {
77         checkNotFinished();
78         protocol = PersistenceProtocol.READY;
79     }
80
81     public int size() {
82         return modifications.size();
83     }
84
85     @Override
86     public ModifyTransactionRequest build() {
87         checkState(haveSequence, "Request sequence has not been set");
88
89         final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
90             protocol);
91         modifications.clear();
92         protocol = null;
93         haveSequence = false;
94         return ret;
95     }
96 }