Introduce message bus models
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / AbstractDOMStoreTransaction.java
1 /*
2  * Copyright (c) 2014 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.md.sal.dom.store.impl;
9
10 import com.google.common.base.Objects;
11 import com.google.common.base.Objects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
15 import org.slf4j.Logger;
16
17 /**
18  * Abstract DOM Store Transaction
19  *
20  * Convenience super implementation of DOM Store transaction which provides
21  * common implementation of {@link #toString()} and {@link #getIdentifier()}.
22  */
23 abstract class AbstractDOMStoreTransaction implements DOMStoreTransaction {
24     private final Throwable debugContext;
25     private final Object identifier;
26
27     protected AbstractDOMStoreTransaction(final Object identifier, final boolean debug) {
28         this.identifier = Preconditions.checkNotNull(identifier, "Identifier must not be null.");
29         this.debugContext = debug ? new Throwable().fillInStackTrace() : null;
30     }
31
32     @Override
33     public final Object getIdentifier() {
34         return identifier;
35     }
36
37     protected final void warnDebugContext(final Logger logger) {
38         if (debugContext != null) {
39             logger.warn("Transaction {} has been allocated in the following context", identifier, debugContext);
40         }
41     }
42
43     @Override
44     public final String toString() {
45         return addToStringAttributes(Objects.toStringHelper(this)).toString();
46     }
47
48     /**
49      * Add class-specific toString attributes.
50      *
51      * @param toStringHelper
52      *            ToStringHelper instance
53      * @return ToStringHelper instance which was passed in
54      */
55     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
56         return toStringHelper.add("id", identifier);
57     }
58 }