package com.odell.glazedlists.example;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GroupingList;
import ca.odell.glazedlists.FunctionList;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class GroupingAndFunctionListExample {

    public static class Song {
        private String artist;
        private String name;
        private String genre;

        public Song(String artist, String name, String genre) {
            this.artist = artist;
            this.name = name;
            this.genre = genre;
        }

        public String getArtist() {
            return artist;
        }

        public void setArtist(String artist) {
            this.artist = artist;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGenre() {
            return genre;
        }

        public void setGenre(String genre) {
            this.genre = genre;
        }

        public String toString() {
            return this.artist + " - " + this.name;
        }
    }

    public static void main(String[] args) {
        // create an EventList of Songs
        final EventList<Song> songs = new BasicEventList<Song>();
        songs.add(new Song("Metallica", "Of Wolf And Man", "Metal"));
        songs.add(new Song("Eminem", "Without Me", "Rap"));
        songs.add(new Song("Britney Spears", "Oops I did it Again", "Pop"));
        songs.add(new Song("Michael Jackson", "Thriller", "Pop"));
        songs.add(new Song("Megadeth", "Rust In Peace", "Metal"));
        songs.add(new Song("MC Hammer", "U Can't Touch This", "Rap"));

        final GroupingList<Song> songsByGenre = new GroupingList<Song>(songs, new GenreComparator());

        for (Iterator<List<Song>> iter = songsByGenre.iterator(); iter.hasNext();) {
            List<Song> songList = iter.next();
            System.out.println(songList.get(0).getGenre());

            for (Iterator<Song> songIter = songList.iterator(); songIter.hasNext();) {
                Song song = songIter.next();
                System.out.println("\t" + song.getArtist() + " - " + song.getName());
            }
        }

        System.out.println();
        final FunctionList<List<Song>, String> reportList = new FunctionList<List<Song>, String>(songsByGenre, new GenreReportFunction());

        for (Iterator<String> reportIter = reportList.iterator(); reportIter.hasNext();) {
            System.out.println(reportIter.next());
        }
    }

    private static class GenreComparator implements Comparator<Song> {
        public int compare(Song o1, Song o2) {
            return o1.getGenre().compareTo(o2.getGenre());
        }
    }

    private static class GenreReportFunction implements FunctionList.Function<List<Song>, String> {
        public String evaluate(List<Song> value) {
            return value.get(0).getGenre() + ": " + value;
        }
    }
}