
package org.tio.sitexxx.im.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.Command;
import org.tio.utils.lock.LockUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @author tanyaowu
 *
 */
public class CommandStat {
	private static Logger							log					= LoggerFactory.getLogger(CommandStat.class);
	public final static Map<Command, CommandStat>	COMMAND_STAT_MAP	= new HashMap<>();

	public static CommandStat getCommandStat(Command command) {
		if (command == null) {
			return null;
		}
		CommandStat ret = COMMAND_STAT_MAP.get(command);
		if (ret != null) {
			return ret;
		}

		try {
			LockUtils.runWriteOrWaitRead(CommandStat.class.getName(), CommandStat.class, () -> {
				CommandStat ret1 = COMMAND_STAT_MAP.get(command);
				if (ret1 == null) {
					ret1 = new CommandStat();
					COMMAND_STAT_MAP.put(command, ret1);
				}
			});
		} catch (Exception e) {
			log.error("", e);
		}
		ret = COMMAND_STAT_MAP.get(command);
		return ret;
	}

	public final AtomicLong received = new AtomicLong();

	public final AtomicLong handled = new AtomicLong();

	public final AtomicLong sent = new AtomicLong();

	/**
	 * @return the sentCount
	 */
	public AtomicLong getSent() {
		return sent;
	}

}
