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 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;
18 * Abstract DOM Store Transaction.
21 * Convenience super implementation of DOM Store transaction which provides
22 * common implementation of {@link #toString()} and {@link #getIdentifier()}.
25 * It can optionally capture the context where it was allocated.
27 * @param <T> identifier type
30 public abstract class AbstractDOMStoreTransaction<T> implements DOMStoreTransaction {
31 private final Throwable debugContext;
32 private final T identifier;
34 protected AbstractDOMStoreTransaction(@Nonnull final T identifier) {
35 this(identifier, false);
38 protected AbstractDOMStoreTransaction(@Nonnull final T identifier, final boolean debug) {
39 this.identifier = Preconditions.checkNotNull(identifier, "Identifier must not be null.");
40 this.debugContext = debug ? new Throwable().fillInStackTrace() : null;
44 public final T getIdentifier() {
49 * Return the context in which this transaction was allocated.
51 * @return The context in which this transaction was allocated, or null
52 * if the context was not recorded.
55 public final Throwable getDebugContext() {
60 public final String toString() {
61 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
65 * Add class-specific toString attributes.
67 * @param toStringHelper
68 * ToStringHelper instance
69 * @return ToStringHelper instance which was passed in
71 protected ToStringHelper addToStringAttributes(@Nonnull final ToStringHelper toStringHelper) {
72 return toStringHelper.add("id", identifier);