package com.dianping.cat.consumer.problem;

import java.util.ArrayList;
import java.util.List;

import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import com.dianping.cat.consumer.problem.model.transform.BaseVisitor;

public class ProblemReportFilter extends BaseVisitor {

	private int m_maxUrlSize = 100;

	public ProblemReportFilter() {

	}

	public ProblemReportFilter(int size) {
		m_maxUrlSize = size;
	}

	@Override
	public void visitDuration(Duration duration) {
		super.visitDuration(duration);
	}

	@Override
	public void visitEntry(Entry entry) {
		String status = entry.getStatus();
		int length = status.length();

		for (int i = 0; i < length; i++) {
			// invalidate char
			if (status.charAt(i) > 126 || status.charAt(i) < 32) {
				continue;
			}
		}
		super.visitEntry(entry);
	}

	@Override
	public void visitMachine(Machine machine) {
		List<Entry> entries = machine.getEntries();
		List<Entry> longUrls = new ArrayList<Entry>();
		List<Entry> errorCodes = new ArrayList<Entry>();

		for (Entry e : entries) {
			String status = e.getStatus();
			int length = status.length();

			for (int i = 0; i < length; i++) {
				// invalidate char
				if (status.charAt(i) > 126 || status.charAt(i) < 32) {
					errorCodes.add(e);
					break;
				}
			}

			if (ProblemType.LONG_URL.getName().equals(e.getType())) {
				longUrls.add(e);
			}
		}

		for (int i = 0; i < errorCodes.size(); i++) {
			entries.remove(errorCodes.get(i));
		}

		int size = longUrls.size();

		if (size > m_maxUrlSize) {
			for (int i = m_maxUrlSize; i < size; i++) {
				entries.remove(longUrls.get(i));
			}
		}
	}

	@Override
	public void visitProblemReport(ProblemReport problemReport) {
		super.visitProblemReport(problemReport);
	}

	@Override
	public void visitSegment(Segment segment) {
		super.visitSegment(segment);
	}

	@Override
	public void visitThread(JavaThread thread) {
		super.visitThread(thread);
	}

}

@@ -890,218 +890,8 @@
 ) %7B%0A
-%09%09String status = entry.getStatus();%0A%09%09int length = status.length();%0A%0A%09%09for (int i = 0; i %3C length; i++) %7B%0A%09%09%09// invalidate char%0A%09%09%09if (status.charAt(i) %3E 126 %7C%7C status.charAt(i) %3C 32) %7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09su

clean code