BUG 3340 : Improve logging
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / identifiers / TransactionChainIdentifier.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.datastore.identifiers;
10
11 import com.google.common.base.Supplier;
12 import com.google.common.base.Suppliers;
13 import java.util.concurrent.atomic.AtomicLong;
14
15 public class TransactionChainIdentifier {
16
17     protected static final String CHAIN_SEPARATOR = "-chn-";
18
19     private final AtomicLong txnCounter = new AtomicLong();
20     private final Supplier<String> stringRepresentation;
21     private final String memberName;
22
23     public TransactionChainIdentifier(final String memberName, final long counter) {
24         this.memberName = memberName;
25         stringRepresentation = Suppliers.memoize(new Supplier<String>() {
26             @Override
27             public String get() {
28                 final StringBuilder sb = new StringBuilder();
29                 sb.append(memberName).append(CHAIN_SEPARATOR);
30                 sb.append(counter);
31                 return sb.toString();
32             }
33         });
34     }
35     @Override
36     public String toString() {
37         return stringRepresentation.get();
38     }
39
40     public TransactionIdentifier newTransactionIdentifier(){
41         return new ChainedTransactionIdentifier(this, txnCounter.incrementAndGet());
42     }
43
44     public String getMemberName() {
45         return memberName;
46     }
47 }