package com.hammurapi.store.local;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;

import com.hammurapi.store.AbstractTrackingLock;

/**
 * This class tracks number of locks acquired by current thread.
 * @author Pavel Vlasov
 *
 */
public class LocalTrackingLock extends AbstractTrackingLock {
	
	public LocalTrackingLock(Lock master) {
		super(master);
	}

	private ThreadLocal<AtomicInteger> counter = new ThreadLocal<AtomicInteger>() {
		protected AtomicInteger initialValue() {
			return new AtomicInteger();
		}
	};

	@Override
	protected void incCounter() {
		counter.get().incrementAndGet();		
	}

	@Override
	protected void decCounter() {
		counter.get().decrementAndGet();
	}

	@Override
	protected int getCounter() {
		return counter.get().get();
	}

}
