checkStyleViolationSeverity=error implemented for mdsal-dom-spi module
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / store / 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.mdsal.dom.spi.store;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16
17 /**
18  * Abstract DOM Store Transaction.
19  * Convenience super implementation of DOM Store transaction which provides common implementation of
20  * {@link #toString()} and {@link #getIdentifier()}.
21  * It can optionally capture the context where it was allocated.
22  *
23  * @param <T> identifier type
24  */
25 @Beta
26 public abstract class AbstractDOMStoreTransaction<T> implements DOMStoreTransaction {
27     private final Throwable debugContext;
28     private final T identifier;
29
30     protected AbstractDOMStoreTransaction(@Nonnull final T identifier) {
31         this(identifier, false);
32     }
33
34     protected AbstractDOMStoreTransaction(@Nonnull final T identifier, final boolean debug) {
35         this.identifier = Preconditions.checkNotNull(identifier, "Identifier must not be null.");
36         this.debugContext = debug ? new Throwable().fillInStackTrace() : null;
37     }
38
39     @Override
40     public final T getIdentifier() {
41         return identifier;
42     }
43
44     /**
45      * Return the context in which this transaction was allocated.
46      *
47      * @return The context in which this transaction was allocated, or null
48      *         if the context was not recorded.
49      */
50     @Nullable
51     public final Throwable getDebugContext() {
52         return debugContext;
53     }
54
55     @Override
56     public final String toString() {
57         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
58     }
59
60     /**
61      * Add class-specific toString attributes.
62      *
63      * @param toStringHelper
64      *            ToStringHelper instance
65      * @return ToStringHelper instance which was passed in
66      */
67     protected ToStringHelper addToStringAttributes(@Nonnull final ToStringHelper toStringHelper) {
68         return toStringHelper.add("id", identifier);
69     }
70 }