Fix warnings/javadocs in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / CompositeDataTreeCohort.java
index d833962277d9a1c3ee1785c2147737740649b6f5..05b9981113a039a75b3679698d9ddd61e8d97318 100644 (file)
@@ -13,7 +13,6 @@ import akka.actor.Status.Failure;
 import akka.dispatch.ExecutionContexts;
 import akka.dispatch.Futures;
 import akka.dispatch.Recover;
-import akka.japi.Function;
 import akka.pattern.Patterns;
 import akka.util.Timeout;
 import com.google.common.base.Preconditions;
@@ -21,20 +20,20 @@ import com.google.common.base.Throwables;
 import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.CanCommit;
 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.Success;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import scala.concurrent.Await;
 import scala.concurrent.Future;
 
 /**
- *
  * Composite cohort, which coordinates multiple user-provided cohorts as if it was only one cohort.
- *
+ * <p/>
  * It tracks current operation and list of cohorts which successfuly finished previous phase in
  * case, if abort is necessary to invoke it only on cohort steps which are still active.
  *
@@ -72,20 +71,17 @@ class CompositeDataTreeCohort {
         COMMITED,
         /**
          * Some of cohorts responsed back with unsuccessful message.
-         *
          */
         FAILED,
         /**
-         *
          * Abort message was send to all cohorts which responded with success previously.
-         *
          */
         ABORTED
     }
 
     protected static final Recover<Object> EXCEPTION_TO_MESSAGE = new Recover<Object>() {
         @Override
-        public Failure recover(Throwable error) throws Throwable {
+        public Failure recover(final Throwable error) throws Throwable {
             return new Failure(error);
         }
     };
@@ -98,25 +94,20 @@ class CompositeDataTreeCohort {
     private Iterable<Success> successfulFromPrevious;
     private State state = State.IDLE;
 
-    CompositeDataTreeCohort(DataTreeCohortActorRegistry registry, TransactionIdentifier transactionID,
-        SchemaContext schema, Timeout timeout) {
+    CompositeDataTreeCohort(final DataTreeCohortActorRegistry registry, final TransactionIdentifier transactionID,
+        final SchemaContext schema, final Timeout timeout) {
         this.registry = Preconditions.checkNotNull(registry);
         this.txId = Preconditions.checkNotNull(transactionID);
         this.schema = Preconditions.checkNotNull(schema);
         this.timeout = Preconditions.checkNotNull(timeout);
     }
 
-    void canCommit(DataTreeCandidateTip tip) throws ExecutionException, TimeoutException {
+    void canCommit(final DataTreeCandidate tip) throws ExecutionException, TimeoutException {
         Collection<CanCommit> messages = registry.createCanCommitMessages(txId, tip, schema);
         // FIXME: Optimize empty collection list with pre-created futures, containing success.
-        Future<Iterable<Object>> canCommitsFuture =
-                Futures.traverse(messages, new Function<CanCommit, Future<Object>>() {
-                    @Override
-                    public Future<Object> apply(CanCommit input) {
-                        return Patterns.ask(input.getCohort(), input, timeout).recover(EXCEPTION_TO_MESSAGE,
-                                ExecutionContexts.global());
-                    }
-                }, ExecutionContexts.global());
+        Future<Iterable<Object>> canCommitsFuture = Futures.traverse(messages,
+            input -> Patterns.ask(input.getCohort(), input, timeout).recover(EXCEPTION_TO_MESSAGE,
+                    ExecutionContexts.global()), ExecutionContexts.global());
         changeStateFrom(State.IDLE, State.CAN_COMMIT_SENT);
         processResponses(canCommitsFuture, State.CAN_COMMIT_SENT, State.CAN_COMMIT_SUCCESSFUL);
     }
@@ -135,25 +126,22 @@ class CompositeDataTreeCohort {
         processResponses(commitsFuture, State.COMMIT_SENT, State.COMMITED);
     }
 
-    void abort() throws TimeoutException {
+    Optional<Future<Iterable<Object>>> abort() {
         if (successfulFromPrevious != null) {
-            sendMesageToSuccessful(new DataTreeCohortActor.Abort(txId));
+            return Optional.of(sendMesageToSuccessful(new DataTreeCohortActor.Abort(txId)));
         }
+
+        return Optional.empty();
     }
 
     private Future<Iterable<Object>> sendMesageToSuccessful(final Object message) {
-        return Futures.traverse(successfulFromPrevious, new Function<DataTreeCohortActor.Success, Future<Object>>() {
-
-            @Override
-            public Future<Object> apply(DataTreeCohortActor.Success cohortResponse) throws Exception {
-                return Patterns.ask(cohortResponse.getCohort(), message, timeout);
-            }
-
-        }, ExecutionContexts.global());
+        return Futures.traverse(successfulFromPrevious, cohortResponse -> Patterns.ask(
+                cohortResponse.getCohort(), message, timeout), ExecutionContexts.global());
     }
 
-    private void processResponses(Future<Iterable<Object>> resultsFuture, State currentState, State afterState)
-            throws TimeoutException, ExecutionException {
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void processResponses(final Future<Iterable<Object>> resultsFuture, final State currentState,
+            final State afterState) throws TimeoutException, ExecutionException {
         final Iterable<Object> results;
         try {
             results = Await.result(resultsFuture, timeout.duration());
@@ -179,7 +167,7 @@ class CompositeDataTreeCohort {
         changeStateFrom(currentState, afterState);
     }
 
-    void changeStateFrom(State expected, State followup) {
+    void changeStateFrom(final State expected, final State followup) {
         Preconditions.checkState(state == expected);
         state = followup;
     }