Webiyo

This page was generated using Webiyo. See its source code and unit tests.

org/webiyo/examples/sourceforge/LogEntry.java

package org.webiyo.examples.sourceforge;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class LogEntry {

    private static final DateFormat DATE_FORMAT =
            makeFormat("yyyy/MM/dd HH:mm:ss", TimeZone.getTimeZone("UTC"));

    private static final Pattern FIELD_PATTERN =
            Pattern.compile("date: ([^;]+);  author: ([^;]+);  state: ([^;]+);(  lines: (.+))?");

    private final Date date;
    private final String author;
    private final String comment;

    public LogEntry(String fields, String comment) throws IOException {
        Matcher fieldMatcher = FIELD_PATTERN.matcher(fields);
        if (!fieldMatcher.matches()) throw new IOException("can't parse fields: " + fields);
        this.date = parseUtcDate(fieldMatcher.group(1));
        this.author = fieldMatcher.group(2);
        this.comment = comment;
    }

    public LogEntry(Date date, String author, String comment) {
        this.date = date;
        this.author = author;
        this.comment = comment;
    }

    public Date getDay(TimeZone zone) {
        GregorianCalendar cal = new GregorianCalendar(zone);
        cal.setTime(date);
        cal.set(GregorianCalendar.HOUR, 0);
        cal.set(GregorianCalendar.MINUTE, 0);
        cal.set(GregorianCalendar.SECOND, 0);
        cal.set(GregorianCalendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public Date getDate() {
        return date;
    }

    public String getAuthor() {
        return author;
    }

    public String getComment() {
        return comment;
    }

    public String toString() {
        return "LogEntry(" + date + "," + author + "," + comment + ")";
    }
    
    // end of public methods

    static SimpleDateFormat makeFormat(String pattern, TimeZone timeZone) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        format.setTimeZone(timeZone);
        return format;
    }

    static Date parseUtcDate(String date) throws IOException {
        try {
            return DATE_FORMAT.parse(date);
        } catch (ParseException e) {
            IOException ioE = new IOException("can't parse date in changelog: " + date);
            ioE.initCause(e);
            throw ioE;
        }
    }

}

SourceForge