2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.spi.store;
10 import static java.util.Objects.requireNonNull;
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;
19 * Abstract DOM Store Transaction.
20 * Convenience super implementation of DOM Store transaction which provides common implementation of
21 * {@link #toString()} and {@link #getIdentifier()}.
22 * It can optionally capture the context where it was allocated.
24 * @param <T> identifier type
27 public abstract class AbstractDOMStoreTransaction<T> implements DOMStoreTransaction {
28 private final @Nullable Throwable debugContext;
29 private final @NonNull T identifier;
31 protected AbstractDOMStoreTransaction(final @NonNull T identifier) {
32 this(identifier, false);
35 protected AbstractDOMStoreTransaction(final @NonNull T identifier, final boolean debug) {
36 this.identifier = requireNonNull(identifier, "Identifier must not be null.");
37 debugContext = debug ? new Throwable().fillInStackTrace() : null;
41 public final T getIdentifier() {
46 * Return the context in which this transaction was allocated.
48 * @return The context in which this transaction was allocated, or null
49 * if the context was not recorded.
51 public final @Nullable Throwable getDebugContext() {
56 public final String toString() {
57 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
61 * Add class-specific toString attributes.
63 * @param toStringHelper
64 * ToStringHelper instance
65 * @return ToStringHelper instance which was passed in
67 protected @NonNull ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
68 return toStringHelper.add("id", identifier);