`
woming66
  • 浏览: 57889 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

记录工作每阶段的代码质量——2011年2月20日

    博客分类:
  • Java
阅读更多
package com.zzq.pattern.decorator;

public interface Executor {

	public void execute(String msg);
}


package com.zzq.pattern.decorator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class BufferedLogger {

	private static final int SIZE = 100;

	private Logger logger = null;

	private Map<String, List<String>> map = null;

	public BufferedLogger(Logger logger) {
		this.logger = logger;
		this.map = new HashMap<String, List<String>>();
	}

	public void fine(String msg) {
		process(msg, "fine", new Executor() {
			public void execute(String msg) {
				logger.fine(msg);
			}
		});
	}

	public void info(String msg) {
		process(msg, "info", new Executor() {
			public void execute(String msg) {
				logger.info(msg);
			}
		});
	}

	public void severe(String msg) {
		process(msg, "severe", new Executor() {
			public void execute(String msg) {
				logger.severe(msg);
			}
		});
	}

	public void warning(String msg) {
		process(msg, "warning", new Executor() {
			public void execute(String msg) {
				logger.warning(msg);
			}
		});
	}
	
	private void process(String msg, String level, Executor executor) {
		List<String> buffer = map.get(level);

		boolean overflow = checkSize(level);
		if (overflow == true) {
			for (String content : buffer) {
				executor.execute(content);
			}
			clearBuffer(level);
		}

		buffer.add(msg);
	}
	
	private boolean checkSize(String level) {
		List<String> buffer = map.get(level);
		if (buffer != null && buffer.size() < SIZE) {
			return false;
		}

		if (buffer == null) {
			map.put(level, new ArrayList<String>());
			return false;
		}

		return true;
	}
	
	private void clearBuffer(String level) {
		List<String> buffer = map.get(level);
		if (buffer != null) {
			buffer.clear();
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics