Commit ac1f02a8 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
package org.oreilly.javaGoodParts.examples.impl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.Team;
/**
* A fourth implementation of the Team interface, using
* a HashSet as the backing store for the Players on
* the team, after the interface has been changed to
* return a Set from the getPlayers() method. This
* iteration adds methods that allow getting player records
* in a random access fashion. One method allows getting
* a {@link List} of {@link Player} objects
* for a player from the name, and the other
* allows getting a {@link Player} object from the player
* id. To do this, we add two {@link HashMap} objects
* to the private fields.
*/
public class TeamImpl implements Team {
private String name;
private HashSet<Player> players =
new HashSet<Player>();
private HashMap<String, List<Player>> byName
= new HashMap<String, List<Player>>();
private HashMap<UUID, Player> byIds
= new HashMap<UUID, Player>();
/**
* Create a TeamImpl object, with the name
* supplied
*/
public TeamImpl(String teamName) {
name = teamName;
}
/**
* Return a <code>String</code> that is the name of
* this team
*/
public String getName() {
return name;
}
/**
* Return a list of the players that are on this
* team
*/
public Set<Player> getRoster() {
return players;
}
/**
* Add a player to the team
*/
public void addPlayer(Player toAdd) {
players.add(toAdd);
if (byName.containsKey(toAdd.getName())){
byName.get(toAdd.getName()).add(toAdd);
} else {
LinkedList newList = new LinkedList<Player>();
newList.add(toAdd);
byName.put(toAdd.getName(), newList);
}
byIds.put(toAdd.getId(), toAdd);
}
/**
* Remove a player from the team
*/
public void removePlayer(Player toRemove) {
players.remove(toRemove);
byName.get(toRemove.getName()).remove(toRemove);
if (byName.get(toRemove.getName()).isEmpty()){
byName.remove(toRemove.getName());
}
byIds.remove(toRemove.getId());
}
@Override
public List<Player> getPlayer(String name) {
return byName.get(name);
}
@Override
public Player getPlayer(UUID playerId) {
return byIds.get(playerId);
}
}
package org.oreilly.javaGoodParts.examples.impl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.Team;
/**
* A fourth implementation of the Team interface, using
* a HashSet as the backing store for the Players on
* the team, after the interface has been changed to
* return a Set from the getPlayers() method. This
* iteration adds methods that allow getting player records
* in a random access fashion. One method allows getting
* a {@link List} of {@link Player} objects
* for a player from the name, and the other
* allows getting a {@link Player} object from the player
* id. To do this, we add two {@link HashMap} objects
* to the private fields.
*/
public class TeamImpl implements Team {
private String name;
private HashSet<Player> players =
new HashSet<Player>();
private HashMap<String, List<Player>> byName = new HashMap<String, List<Player>>();
private HashMap<UUID, Player> byIds = new HashMap<UUID, Player>();
/**
* Create a TeamImpl object, with the name
* supplied
*/
public TeamImpl(String teamName) {
name = teamName;
}
/**
* Return a <code>String</code> that is the name of
* this team
*/
public String getName() {
return name;
}
/**
* Return a list of the players that are on this
* team
*/
public Set<Player> getRoster() {
return players;
}
/**
* Add a player to the team
*/
public void addPlayer(Player toAdd) {
players.add(toAdd);
if (byName.containsKey(toAdd.getName())) {
byName.get(toAdd.getName()).add(toAdd);
} else {
LinkedList newList = new LinkedList<Player>();
newList.add(toAdd);
byName.put(toAdd.getName(), newList);
}
byIds.put(toAdd.getId(), toAdd);
}
/**
* Remove a player from the team
*/
public void removePlayer(Player toRemove) {
players.remove(toRemove);
byName.get(toRemove.getName()).remove(toRemove);
if (byName.get(toRemove.getName()).isEmpty()) {
byName.remove(toRemove.getName());
}
byIds.remove(toRemove.getId());
}
@Override
public List<Player> getPlayer(String name) {
return byName.get(name);
}
@Override
public Player getPlayer(UUID playerId) {
return byIds.get(playerId);
}
}
/*
*A basic implementation of the Catcher interface
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Catcher;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
* A basic implementation of the Catcher
*/
public class CatcherImpl extends FielderImpl
implements Catcher{
private int passedBalls;
public CatcherImpl(){
super();
passedBalls = 0;
}
public CatcherImpl(Fielder oldStats){
attempts = oldStats.getAttempts();
putOuts = oldStats.getPutOuts();
assists = oldStats.getAssists();
errors = oldStats.getErrors();
passedBalls = 0;
}
public void PassedBall(){
passedBalls++;
}
public int getPassedBalls(){
return passedBalls;
}
}
package org.oreilly.javaGoodParts.examples.impl;
/**
* A class that checks implementations of the Batter
* interface. The class will be initialized with an array
* of AtBatResults, will take a Batter object and feed in
* those results, and then will check the statistics that
* were generated. For simplicity sake, the current implementation
* only checks the Slugging Average
* @author waldo
*
*/
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
public class CheckBatter {
private Batter.AtBatResult[] testData;
public CheckBatter(Batter.AtBatResult[] data){
testData = data;
}
public boolean SluggingTest(Batter toTest){
for (Batter.AtBatResult r : testData){
toTest.atBat(r);
}
try {
if ((toTest.getTotalBases()/toTest.getAtBats())
!= toTest.getSlugging()){
return false;
}
} catch (NotEnoughAtBatsException e){
if ((e.getNeeded() + toTest.getAtBats()) != 10)
return false;
}
return true;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.oreilly.javaGoodParts.examples.impl;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
/**
*
* @author waldo
*/
public class FielderImpl implements Fielder{
protected int attempts;
protected int putOuts;
protected int assists;
protected int errors;
public FielderImpl(){
attempts = putOuts = assists = errors = 0;
}
@Override
public void fieldTry(AttemptResult what) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void playInning() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public float getFieldAverage() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public float getRange() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getAssists() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getAttempts() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getErrors() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getPutOuts() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
package org.oreilly.javaGoodParts.examples.impl;
/**
* A set of examples using the classes to print out
* information kept in the statistics package
*
*/
import java.util.Iterator;
import java.util.TreeSet;
import org.oreilly.javaGoodParts.examples.statistics.NotEnoughAtBatsException;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.Player.Roles;
import org.oreilly.javaGoodParts.examples.statistics.Team;
public class Formatter {
public static void Format(Batter toFormat) {
System.out.print("\t");
try {
System.out.print(toFormat.getAverage());
System.out.print("\t" + toFormat.getOBP());
System.out.print("\t" + toFormat.getSlugging());
System.out.print("\t" + toFormat.getAtBats());
System.out.println("\t" + toFormat.getTotalBases());
} catch (NotEnoughAtBatsException e) {
System.out.println("\t" + "Not enough at-bats to be significant");
}
}
public static void FormatRoster(Team toFormat){
System.out.println(toFormat.getName());
Iterator<Player> e = toFormat.getRoster().iterator();
while (e.hasNext()){
System.out.println("\t" + e.next().getName());
}
}
public static void FormatRoster2(Team toFormat){
System.out.println(toFormat.getName());
for (Player i : toFormat.getRoster()){
System.out.println("\t" + i.getName());
}
}
public static void FormatBattingAvg(Team toFormat){
TreeSet<Player> battingSort =
new TreeSet<Player>(new BattingComparitor());
battingSort.addAll(toFormat.getRoster());
float avg = 0.0f;
for (Player p : battingSort){
System.out.print(p.getName() + "\t");
if (p.hasRole(Roles.Batter)){
try {
avg = p.asBatter().getAverage();
} catch (NotEnoughAtBatsException e) {
avg = 0.0f;
}
}
else
avg = 0.0f;
System.out.println(avg);
}
}
public static void FormatBADescending(Team toFormat){
TreeSet<Player> battingSort =
new TreeSet<Player>(new BattingComparitor());
battingSort.addAll(toFormat.getRoster());
float avg = 0.0f;
for (Player p = battingSort.last();
null != p;
battingSort.remove(p)){
if (!p.hasRole(Roles.Batter))
return;
try {
avg = p.asBatter().getAverage();
if (avg == 0.0)
return;
} catch (NotEnoughAtBatsException e){
return;
}
System.out.println(p.getName() + "\t"
+ avg);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.oreilly.javaGoodParts.examples.impl;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.EnumSet;
import java.util.UUID;
import org.oreilly.javaGoodParts.examples.statistics.Batter;
import org.oreilly.javaGoodParts.examples.statistics.Catcher;
import org.oreilly.javaGoodParts.examples.statistics.Fielder;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.Team;
/**
*
*/
public class PlayerImpl implements Player, Serializable {
private static final long serialVersionUID = 1;
private UUID id;
private String name;
private Team team;
private Position pos = Position.Utility;
private EnumSet<Roles> roles;
private boolean changed = false;
private BatterImpl batterStats;
private FielderImpl fielderStats;
private CatcherImpl catcherStats;
public PlayerImpl(String playerName) {
name = playerName;
id = UUID.randomUUID();
batterStats = null;
fielderStats = null;
catcherStats = null;
}
private String getFileRoot(){
String fileRoot = System.getProperty("Statistics.fileRoot");
if (fileRoot == null){
fileRoot = System.getProperty("user.dir");
}
fileRoot = fileRoot + System.getProperty("file.separator");
return fileRoot;
}
public PlayerImpl(UUID playerId, String teamName) {
id = playerId;
try {
ObjectInputStream readIn =
new ObjectInputStream(new FileInputStream(
getFileRoot() + teamName +
System.getProperty("file.separator") + id.toString()));
PlayerImpl copy = (PlayerImpl) readIn.readObject();
readIn.close();
id = playerId;
name = copy.name;
team = copy.team;
pos = copy.pos;
} catch (IOException e) {
System.out.println("unable to open file for player, creating new player object");
name = "unknown";
} catch (ClassNotFoundException e) {
System.out.println("unable to read file for player");
}
}
protected void writeState() {
try {
ObjectOutputStream writeOut =
new ObjectOutputStream(new FileOutputStream(
getFileRoot() + team.getName() +
System.getProperty("file.separator") + id.toString()));
writeOut.writeObject(this);
writeOut.close();
changed = false;
} catch (Exception e) {
System.out.println("unable to write object");
}
}
protected void finalize(){
if (changed){
writeState();
}
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public Team getTeam() {
return team;
}
public void setTeam(Team newTeam){
team = newTeam;
}
public Position getPosition() {
return pos;
}
public void setPosition(Position playPosition) {
if (playPosition != pos) {
pos = playPosition;
changed = true;
}
}
public boolean hasRole(Roles role){
return roles.contains(role);
}
public void addRole(Roles role){
if (roles.contains(role))
return;
switch (role){
case Batter:
batterStats = new BatterImpl(this);
break;
case Fielder:
fielderStats = new FielderImpl();