Webiyo

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

org/webiyo/examples/sourceforge/ChangeLog.java

package org.webiyo.examples.sourceforge;

import org.webiyo.util.MultiMap;

import java.util.*;

class ChangeLog {

    private final List<LogEntry> entries = new ArrayList<LogEntry>();

    public ChangeLog() {
    }

    public void add(LogEntry entry) {
        entries.add(entry);
    }

    public List<LogEntry> entries() {
        List<LogEntry> result = new ArrayList<LogEntry>(entries);
        sortByDateMostRecentFirst(result);
        removeDuplicates(result);
        return result;
    }

    public MultiMap<Date, LogEntry> entriesGroupedByDay(TimeZone zone) {

        MultiMap<Date, LogEntry> result = new MultiMap<Date, LogEntry>();

        for (LogEntry entry : entries()) {
            result.put(entry.getDay(zone), entry);
        }

        return result;
    }
    
    // end of public methods

    private void sortByDateMostRecentFirst(List<LogEntry> result) {
        Collections.sort(result, new Comparator<LogEntry>() {
            public int compare(LogEntry first, LogEntry second) {
                return second.getDate().compareTo(first.getDate());
            }
        });
    }

    private void removeDuplicates(List<LogEntry> result) {
        LogEntry prev = null;
        for (Iterator<LogEntry> it = result.iterator(); it.hasNext();) {
            LogEntry entry = it.next();
            if (prev != null && prev.getAuthor().equals(entry.getAuthor()) && prev.getComment().equals(entry.getComment())) {
                it.remove();
            }
            prev = entry;
        }
    }


}

SourceForge