package org.kne.debug;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import java.util.Set;
final class Int implements Comparable<Int>,Cloneable {
	public int value;

	public Int(int value) {
		super();
		this.value = value;
	}

	@Override
	public int hashCode() {
		return value;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO �Զ����ɵķ������
		return super.clone();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Int other = (Int) obj;
		if (value != other.value)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return new Integer(value).toString();
	}

	@Override
	public int compareTo(Int o) {
		
		return o.value-value;
	}


	
}

public class Debuger extends Thread {
	Map <List<StackTraceElement>,Object[]>md=new HashMap<List<StackTraceElement>,Object[]>();
	@Override
	public void run() {
		while(true){
			for(int iz=0;iz<100;iz++){
			Map<Thread, StackTraceElement[]> m=Thread.getAllStackTraces();
			Set<Entry<Thread, StackTraceElement[]>> s=m.entrySet();
			Iterator<Entry<Thread, StackTraceElement[]>> i=s.iterator();
			while(i.hasNext()){
				Entry<Thread, StackTraceElement[]>elements =i.next();
				 StackTraceElement[]value=elements.getValue();
				List<StackTraceElement> l=new ArrayList<StackTraceElement>(value.length);
				for (int j = 0; j < value.length; j++) {
					l.add(value[j]);
				}
				if(!l.isEmpty())
				if(md.containsKey(l)){
					((Int)md.get(l)[0]).value++;
				}else{
				md.put(l,new Object[]{new Int(1),elements.getKey()} );
				}
			}
			
			}
			ArrayList<Entry<List<StackTraceElement>, Object[]>> tmp=new ArrayList();
			Set<Entry<List<StackTraceElement>, Object[]>> sn=md.entrySet();
			Iterator<Entry<List<StackTraceElement>, Object[]>> in=sn.iterator();
			while(in.hasNext()){
				Entry<List<StackTraceElement>, Object[]>elements =in.next();
				
				tmp.add(elements);
			}
			tmp.sort(new Comparator<Entry<List<StackTraceElement>, Object[]>>() {

				@Override
				public int compare(Entry<List<StackTraceElement>, Object[]> o1,
						Entry<List<StackTraceElement>, Object[]> o2) {
					
					return ((Int)o2.getValue()[0]).value-((Int)o1.getValue()[0]).value;
				}
			});
			for(int d=0;d<tmp.size();d++){
				
				Entry<List<StackTraceElement>, Object[]>elements=tmp.get(d);
				if(((Thread)(elements.getValue()[1])).getName().startsWith("Decode")){
					
				}else{
					continue;
				}
				List<StackTraceElement> k=elements.getKey();
				StringBuilder sb=new StringBuilder();
				for(int d2=0;d2<k.size();d2++){
					sb.append(' ');sb.append(k.get(d2));
				}
			System.err.println(elements.getValue()[0]+":"+elements.getValue()[1]+"="+sb);
			}
				System.err.println("--------------------------------------------------------------------------------------------------------------------");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO �Զ����ɵ� catch ��
				e.printStackTrace();
			}
		}
	}

}
