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.controller.sal.core.spi.data;
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.
22 * Convenience super implementation of DOM Store transaction which provides
23 * common implementation of {@link #toString()} and {@link #getIdentifier()}.
26 * It can optionally capture the context where it was allocated.
28 * @param <T> identifier type
31 public abstract class AbstractDOMStoreTransaction<T> implements DOMStoreTransaction {
32 private final Throwable debugContext;
33 private final @NonNull T identifier;
35 protected AbstractDOMStoreTransaction(final @NonNull T identifier) {
36 this(identifier, false);
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;
45 public final T getIdentifier() {
50 * Return the context in which this transaction was allocated.
52 * @return The context in which this transaction was allocated, or null if the context was not recorded.
54 public final @Nullable Throwable getDebugContext() {
59 public final String toString() {
60 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
64 * Add class-specific toString attributes.
66 * @param toStringHelper
67 * ToStringHelper instance
68 * @return ToStringHelper instance which was passed in
70 protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
71 return toStringHelper.add("id", identifier);