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