All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.metrics.stream.handler.Summator Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.metrics.stream.handler;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;

import com.alogic.metrics.Fragment;
import com.anysoft.stream.Handler;
import com.anysoft.stream.SlideHandler;

/**
 * 累加器
 * 
 * @author yyduan
 *
 * @since 1.6.6.13
 *
 */
public class Summator extends SlideHandler{
	protected Hashtable metrics = new Hashtable();
	
	protected void incr(Fragment f){
		String id = f.getStatsDimesion();
		
		Fragment found = metrics.get(id);
		
		if (found == null){
			synchronized(metrics){
				found = metrics.get(id);
				if (found == null){
					metrics.put(id, f);
				}else{
					found.incr(f);
				}
			}
		}else{
			found.incr(f);
		}
	}

	
	protected void onHandle(Fragment _data,long t) {
		incr(_data);
	}

	
	protected void onFlush(long t) {
		Handler handler = getSlidingHandler();
		Iterator> iterator = metrics.entrySet().iterator();
		
		while (iterator.hasNext()){
			Entry entry = iterator.next();
			if (handler != null){
				handler.handle(entry.getValue(),t);
			}
		}
		
		if (handler != null){
			handler.flush(t);
		}
		
		metrics.clear();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy