CacheCount.java 1.23 KB
package com.yoho.search.cache.model;

import java.util.concurrent.atomic.AtomicLong;

public class CacheCount {

	private AtomicLong totalCount = null;
	private AtomicLong matchCount = null;
	
	public CacheCount(){
		this.totalCount = new AtomicLong(0);
		this.matchCount = new AtomicLong(0);
	}
	
	public CacheCount(long totalCountVal,long matchCountVal){
		this.totalCount = new AtomicLong(totalCountVal);
		this.matchCount = new AtomicLong(matchCountVal);
	}

	public void clear() {
		this.totalCount = new AtomicLong(0);
		this.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() + "]";
	}
	
}