/** * Encapsulate and otherwise handle the parsing of a line from * the web server log * @author gtowell * Created: April 29, 2020 */ public class LogLine { /** The IP address extracted from the log line */ private final String ipAddress; /** The line itself, stored here in case further processing is needed */ private final String line; /** A counter, not properly a part of the line, but is data associaed with the line */ private int count; /** * Constructor for the LogLine. It takes a line from a log and * does some parsing * @param lin the line from the log * @throws Exception if for some reason, the line cannot be parsed. */ public LogLine(String lin) throws Exception { if (lin==null || lin.length()==0) throw new Exception("Log lines should not be null or empty"); line = lin; count = 1; String[] spl = lin.trim().split("\\s+"); if (spl.length==0) throw new Exception("The line could not be split"); ipAddress = spl[0]; // should check that this really looks like an IP address } /** Get the count */ public int getCount() { return count; } /** Gte the IP */ public String getIP() { return ipAddress; } /** Increment the ount by 1. * @return the new value of count. */ public int incCount() { return ++count; } @Override public String toString() { return ipAddress; } /** A longer string version of the contents */ public String toStringLong() { return ipAddress + " " + count; } }