Add AbstractDOMBrokerTransaction.toString()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBrokerTransaction.java
index 98fea88f632285b3e7d5d6f1f900d6b716d3bb89..180623edf884b921fe2f03a168009654c3645f39 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.controller.cluster.databroker;
 
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Preconditions;
 import java.util.Collection;
 import java.util.EnumMap;
@@ -19,41 +21,41 @@ import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransaction> implements
+public abstract class AbstractDOMBrokerTransaction<T extends DOMStoreTransaction> implements
         AsyncTransaction<YangInstanceIdentifier, NormalizedNode<?, ?>> {
 
-    private Map<K, T> backingTxs;
+    private final EnumMap<LogicalDatastoreType, T> backingTxs;
     private final Object identifier;
     private final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories;
 
     /**
-     *
      * Creates new composite Transactions.
      *
-     * @param identifier
-     *            Identifier of transaction.
+     * @param identifier Identifier of transaction.
      */
-    protected AbstractDOMBrokerTransaction(final Object identifier, Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
+    protected AbstractDOMBrokerTransaction(final Object identifier,
+            Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories) {
         this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
-        this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories, "Store Transaction Factories should not be null");
-        this.backingTxs = new EnumMap(LogicalDatastoreType.class);
+        this.storeTxFactories = Preconditions.checkNotNull(storeTxFactories,
+                "Store Transaction Factories should not be null");
+        this.backingTxs = new EnumMap<>(LogicalDatastoreType.class);
     }
 
     /**
      * Returns subtransaction associated with supplied key.
      *
-     * @param key
-     * @return
+     * @param key the data store type key
+     * @return the subtransaction
      * @throws NullPointerException
      *             if key is null
      * @throws IllegalArgumentException
      *             if no subtransaction is associated with key.
      */
-    protected final T getSubtransaction(final K key) {
+    protected final T getSubtransaction(final LogicalDatastoreType key) {
         Preconditions.checkNotNull(key, "key must not be null.");
 
         T ret = backingTxs.get(key);
-        if(ret == null){
+        if (ret == null) {
             ret = createTransaction(key);
             backingTxs.put(key, ret);
         }
@@ -61,7 +63,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
         return ret;
     }
 
-    protected abstract T createTransaction(final K key);
+    protected abstract T createTransaction(LogicalDatastoreType key);
 
     /**
      * Returns immutable Iterable of all subtransactions.
@@ -76,6 +78,7 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
         return identifier;
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     protected void closeSubtransactions() {
         /*
          * We share one exception for all failures, which are added
@@ -101,7 +104,16 @@ public abstract class AbstractDOMBrokerTransaction<K, T extends DOMStoreTransact
         }
     }
 
-    protected DOMStoreTransactionFactory getTxFactory(K type){
+    protected DOMStoreTransactionFactory getTxFactory(LogicalDatastoreType type) {
         return storeTxFactories.get(type);
     }
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
+    }
+
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return toStringHelper.add("identifier", identifier);
+    }
 }