    001package com.hammurapi.store.local;
    002
    003import java.util.concurrent.atomic.AtomicInteger;
    004import java.util.concurrent.locks.Lock;
    005
    006import com.hammurapi.store.AbstractTrackingLock;
    007
    008/**
    009 * This class tracks number of locks acquired by current thread.
    010 * @author Pavel Vlasov
    011 *
    012 */
    013public class LocalTrackingLock extends AbstractTrackingLock {
    014
    015        public LocalTrackingLock(Lock master) {
    016                super(master);
    017        }
    018
    019        private ThreadLocal<AtomicInteger> counter = new ThreadLocal<AtomicInteger>() {
    020                protected AtomicInteger initialValue() {
    021                        return new AtomicInteger();
    022                }
    023        };
    024
    025        @Override
    026        protected void incCounter() {
    027                counter.get().incrementAndGet();
    028        }
    029
    030        @Override
    031        protected void decCounter() {
    032                counter.get().decrementAndGet();
    033        }
    034
    035        @Override
    036        protected int getCounter() {
    037                return counter.get().get();
    038        }
    039
    040}