CacheCount.java 950 Bytes
package com.yoho.search.cache.model;

import java.util.concurrent.atomic.AtomicLong;

public class CacheCount {

	private AtomicLong totalCount = new AtomicLong(0);
	private AtomicLong matchCount = new AtomicLong(0);

	public void clear() {
		totalCount = new AtomicLong(0);
		matchCount = new AtomicLong(0);
	}

	public void incTotalCount() {
		totalCount.incrementAndGet();
	}

	public void incMatchCount() {
		matchCount.incrementAndGet();
	}
	
	public AtomicLong getTotalCount() {
		return totalCount;
	}

	public AtomicLong getMatchCount() {
		return matchCount;
	}

	public int getMatchPercent() {
		long matchCnt = matchCount.longValue();
		long totalCnt = totalCount.longValue();
		if (totalCnt == 0) {
			return 0;
		}
		return (int) (matchCnt * 100L / totalCnt);
	}
	
	@Override
	public String toString() {
		return "CacheCount [totalCount=" + totalCount + ", matchCount=" + matchCount + ", matchPercent=" + getMatchPercent() + "]";
	}
	
}