/*
	cursus - Race series management program
	Copyright 2012  Simon Arlott

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.lp0.cursus.scoring.scores.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import com.google.common.base.Predicates;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

import eu.lp0.cursus.db.data.Pilot;
import eu.lp0.cursus.db.data.Race;
import eu.lp0.cursus.scoring.data.RacePointsData;
import eu.lp0.cursus.scoring.data.ScoredData;
import eu.lp0.cursus.scoring.scores.base.AbstractRaceDiscardsData;

public class GenericRaceDiscardsData<T extends ScoredData & RacePointsData> extends AbstractRaceDiscardsData<T> {
	public GenericRaceDiscardsData(T scores) {
		super(scores, 0);
	}

	public GenericRaceDiscardsData(T scores, DiscardCalculator discardCalculator) {
		super(scores, discardCalculator.getDiscardsFor(scores.getRaces()));
	}

	@Override
	protected List<Race> calculateDiscardedRaces(Pilot pilot) {
		List<Race> pilotDiscards = new ArrayList<Race>(discards);

		if (discards > 0) {
			final Map<Race, Integer> racePoints = scores.getRacePoints(pilot);
			SortedSet<Race> pilotRaces = new TreeSet<Race>(new Comparator<Race>() {
				@Override
				public int compare(Race o1, Race o2) {
					return ComparisonChain.start().compare(racePoints.get(o2), racePoints.get(o1)).compare(o1, o2).result();
				}
			});

			// Use all races where the score is not null
			pilotRaces.addAll(Maps.filterValues(racePoints, Predicates.notNull()).keySet());

			// Discard the highest scoring races
			Iterator<Race> it = Iterables.limit(pilotRaces, 2).iterator();
			for (int i = 1; i <= discards; i++) {
				pilotDiscards.add(it.hasNext() ? it.next() : null);
			}
		}

		return pilotDiscards;
	}
}
@@ -755,25 +755,27 @@
 va.util.
-ArrayList
+Collections
 ;%0Aimport
@@ -801,35 +801,8 @@
 or;%0A
-import java.util.Iterator;%0A
 impo
@@ -845,62 +845,8 @@
 Map;
-%0Aimport java.util.SortedSet;%0Aimport java.util.TreeSet;
 %0A%0Aim
@@ -1012,20 +1012,103 @@
 collect.
-Maps
+Lists;%0Aimport com.google.common.collect.Maps;%0Aimport com.google.common.collect.Ordering
 ;%0A%0Aimpor
@@ -1750,69 +1750,8 @@
 ) %7B%0A
-%09%09List%3CRace%3E pilotDiscards = new ArrayList%3CRace%3E(discards);%0A%0A
 %09%09if
@@ -1842,57 +1842,107 @@
 t);%0A
+%0A
 %09%09%09
-SortedSet%3CRace%3E pilotRaces = new TreeSet%3CRace%3E
+// Discard the highest scoring races%0A%09%09%09return Lists.newArrayList(Iterables.limit(Ordering.from
 (new
@@ -2141,15 +2141,8 @@
 %09%7D%0A%09
-%09%09%7D);%0A%0A
 %09%09%09/
@@ -2192,25 +2192,21 @@
 %0A%09%09%09
-pilotRaces.addAll
+%7D).sortedCopy
 (Map
@@ -2267,246 +2267,59 @@
 t())
-;%0A%0A%09%09%09// Discard the highest scoring races%0A%09%09%09Iterator%3CRace%3E it = Iterables.limit(pilotRaces, 2).iterator();%0A%09%09%09for (int i = 1; i %3C= discards; i++) %7B%0A%09%09%09%09pilotDiscards.add(it.hasNext() ? it.next() : null);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return pilotDiscards
+, discards));%0A%09%09%7D%0A%0A%09%09return Collections.emptyList()
 ;%0A%09%7D

handle more than two discards