/*
* AFLPlayer.java
*
* Created on 9 May 2006, 13:37
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ffg.entity;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.impl.sort.ReverseComparator;
import ffg.util.BeanMatcher;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
* Represents an AFL player. Immutable.
* @author eugene
*/
public class AFLPlayer implements Comparable<AFLPlayer> {
private static final String PLAYERS_FILE_NAME = "/dat/players.txt";
private static final List<AFLPlayer> ALL_PLAYERS = new ArrayList<AFLPlayer>();
private final int ordinal;
private final String firstName;
private final String lastName;
private final String fullName;
private final int number;
private final AFLTeam team;
/**
* Creates a new instance of AFLPlayer
* @param line The line containing the player's information.
* @param ordinal The player's ordinal.
*/
private AFLPlayer(String line, int ordinal) {
this.ordinal = ordinal;
String[] fields = line.split(",");
team = AFLTeam.getTeam(fields[0]);
number = Integer.parseInt(fields[1]);
firstName = fields[2];
lastName = fields[3];
fullName = firstName + " " + lastName;
}
/**
* Loads the players information.
* @throws java.io.FileNotFoundException If players.txt is missing.
*/
public static void loadPlayers() throws FileNotFoundException {
ALL_PLAYERS.clear();
Scanner scanner = null;
try {
scanner = new Scanner(AFLPlayer.class.getResourceAsStream(PLAYERS_FILE_NAME));
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.length() > 0) {
AFLPlayer player = new AFLPlayer(line, ++counter);
ALL_PLAYERS.add(player);
}
}
}
finally {
if (scanner != null) {
scanner.close();
}
}
}
/**
* Returns all {@link AFLPlayer} instances.
* @return all {@link AFLPlayer} instances.
*/
public static List<AFLPlayer> getAllPlayers() {
return new ArrayList<AFLPlayer>(ALL_PLAYERS);
}
/**
* The number of players.
* @return as above
*/
public static int getAllPlayersSize() {
return ALL_PLAYERS.size();
}
/**
* Filters the list of players for the specified JavaBean property name with the specified property values.
* @param currentPlayers {@link AFLPlayer}s to filter against.
* @param propertyName The JavaBean property name to filter with.
* @param propertyValues The property values to filter with.
* @return The filtered list of players according to the specified inputs, separate from the original list
*/
public static List<AFLPlayer> filterForProperty(List<AFLPlayer> currentPlayers, String propertyName, Object... propertyValues) {
FilterList<AFLPlayer> filtered = new FilterList<AFLPlayer>(GlazedLists.eventList(currentPlayers), new BeanMatcher<AFLPlayer>(propertyName, propertyValues));
return new ArrayList<AFLPlayer>(filtered);
}
/**
* Sorts the list of players ascendingly with the specified {@link Comparator}
* @param currentPlayers {@link AFLPlayer}s to sort against.
* @param comparator The {@link Comparator} used to sort.
* @return The ascendingly sorted list of players, separate from the original list.
*/
public static List<AFLPlayer> sortAscendingBy(List<AFLPlayer> currentPlayers, Comparator<AFLPlayer> comparator) {
List<AFLPlayer> copy = new ArrayList<AFLPlayer>(currentPlayers);
Collections.sort(copy, comparator);
return copy;
}
/**
* Sorts the list of players descendingly with the specified {@link Comparator}
* @param currentPlayers {@link AFLPlayer}s to sort against.
* @param comparator The {@link Comparator} used to sort.
* @return The descendingly sorted list of players, separate from the original list.
*/
public static List<AFLPlayer> sortDescendingBy(List<AFLPlayer> currentPlayers, Comparator<AFLPlayer> comparator) {
List<AFLPlayer> copy = new ArrayList<AFLPlayer>(currentPlayers);
Collections.sort(copy, new ReverseComparator<AFLPlayer>(comparator));
return copy;
}
/**
* Returns the {@link AFLPlayer} with the given ordinal.
* @param ordinal The ordinal of the player
* @return The {@link AFLPlayer} with ordinal ordinal, or null if none.
*/
public static AFLPlayer getPlayerByOrdinal(int ordinal) {
return ALL_PLAYERS.get(ordinal - 1);
}
/**
* A player is equal to another player if their ordinals are equal
* @param obj The other player to compare to.
* @return If obj is an {@link AFLPlayer} and their ordinals are equal
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof AFLPlayer) {
AFLPlayer player = (AFLPlayer) obj;
return getOrdinal() == player.getOrdinal();
} else {
return false;
}
}
/**
* {@inheritDoc}
* @return
*/
@Override
public int hashCode() {
return getOrdinal();
}
/**
* The player's ordinal
* @return the ordinal
*/
public int getOrdinal() {
return ordinal;
}
/**
* The player's first name
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* The player's last name
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* The player's jumper number
* @return the jumper number
*/
public int getNumber() {
return number;
}
/**
* The player's team
* @return the team
*/
public AFLTeam getTeam() {
return team;
}
// /**
// * The player's eligible positions
// * @return the positions
// */
// public AFLPositions getPositions() {
// return positions;
// }
/**
* Players are ordered by their ordinal.
* @param player
*/
// @Override
public int compareTo(AFLPlayer player) {
return getOrdinal() - player.getOrdinal();
}
/**
* @return The full name of the player
*/
@Override
public String toString() {
return fullName;
}
// public static void main(String[] args) throws Exception {
// List<AFLPlayer> list = AFLPlayer.sortAscendingBy(AFLPlayer.getAllPlayers(), new Comparator<AFLPlayer>() {
// public int compare(AFLPlayer o1, AFLPlayer o2) {
// int diff = o1.getTeam().compareTo(o2.getTeam());
// if (diff == 0) {
// diff = o1.getNumber() - o2.getNumber();
// }
// return diff;
// }
// });
// Map<String, AFLPlayer> m = new HashMap<String, AFLPlayer>();
// for (AFLPlayer p : list) {
//// System.out.printf("%s,%s,%d\n", p.toString(), p.getTeam(), p.getNumber());
// String key = p.getTeam().toString() + p.getNumber();
// if (m.containsKey(key)) {
// System.out.println("violated = " + key);
// } else {
// m.put(key, p);
// }
// }
// }
}
|