📄 eventcount.java
字号:
/**
*
*/
package logfileAnalyse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Jack
*
*/
public class EventCount {
public static final String DELIMITER = "#";
public static final Object NEWLINE = System.getProperty("line.separator");
private static int PRINT_MAX_LENGTH = 30;
public String pattern1 = "([1-9])([0-9])([0-9])";
public String pattern2 = "([1-9])([0-9])([0-9])([0-9])";
public File fileIn;
public int fileNum;
private int totalEvents;
private long begin;
private long end;
private Map<String, Event> events;
/**
* no argument constructor initial for internal storage.
*/
public EventCount() {
fileNum = 0;
totalEvents = 0;
begin = 0;
end = 0;
events = new TreeMap<String, Event>();
}
/**
* Accept filename to be analysed
*
* @param fileIn
* filename to be analysed
*/
public EventCount(String filename) {
this();
fileIn = new File(filename);
if (!fileIn.exists()) {
System.err.println(filename + " doses not exits.");
System.exit(1);
}
else
fileNum++;
}
/**
* @return the begin
*/
public long getBegin() {
return begin;
}
/**
* @param begin the begin to set
*/
public void setBegin(long begin) {
this.begin = begin;
}
/**
* @return the end
*/
public long getEnd() {
return end;
}
/**
* @param end the end to set
*/
public void setEnd(long end) {
this.end = end;
}
/**
* @return the totalEvents
*/
public int getTotalEvents() {
return totalEvents;
}
/*
* Processes the data(from the log file ) one line at a time, tokenizes the
* events and stores the event in a TreeMap with the event as the key and a
* event number as the value.
*/
public void calculate() {
BufferedReader reader = null;
String line = null;
StringTokenizer st = null;
int size = 0;
try{
reader = new BufferedReader(new FileReader(fileIn));
long start = System.currentTimeMillis();
this.setBegin(start);
line = reader.readLine();
while(line != null) {
st = new StringTokenizer(line, DELIMITER);
while(st.hasMoreTokens()) {
String token = st.nextToken();
size = token.length();
if( (size == 3)||(size == 4) ) {
Event value = events.get(token);
if(value == null) {
if(isEvent(token))
{
totalEvents++;
events.put(token, new Event(token));
}
}
else {
totalEvents++;
value.increment();
events.put(token, value);
}
}
}
line = reader.readLine();
}
long finish = System.currentTimeMillis();
this.setEnd(finish);
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
/*
* input string is event or not
*/
public boolean isEvent(String input) {
int size = 0;
size = input.length();
if(size == 3) {
if(patternMatch(input, pattern1))
return true;
else
return false;
}
else if(size == 4) {
if(patternMatch(input, pattern2))
return true;
else
return false;
}
else
return false;
}
/*
* match the pattern
*/
public boolean patternMatch(String source, String pattern) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(source);
if(m.find())
return true;
else
return false;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer strbf = new StringBuffer();
String sEvt = "Event Id";
String sFrq = "Frequency";
String s1 = " ";
String s2 = "-";
Event tEvt = new Event();
tEvt.PRINT_MAX_LENGTH = PRINT_MAX_LENGTH;
List<Event> entries = new ArrayList<Event>();
entries.addAll(events.values());
strbf.append(sEvt);
for(int i = 0; i < (PRINT_MAX_LENGTH - sEvt.length() - sFrq.length()); i++)
strbf.append(s1);
strbf.append(sFrq).append(NEWLINE);
for(int i = 0; i < PRINT_MAX_LENGTH; i++)
strbf.append(s2);
strbf.append(NEWLINE);
for(Event evt : entries) {
evt.PRINT_MAX_LENGTH = PRINT_MAX_LENGTH;
strbf.append(evt).append(NEWLINE);
}
for(int i = 0; i < PRINT_MAX_LENGTH; i++)
strbf.append(s2);
strbf.append(NEWLINE);
return new String(strbf);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -