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.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Set;
import org.oreilly.javaGoodParts.examples.statistics.BoxScore;
import org.oreilly.javaGoodParts.examples.statistics.Player;
import org.oreilly.javaGoodParts.examples.statistics.StatRecorder;
/**
* A component to a client that can be used to report
* the results of games to a {@link StatRecorder}. This
* class will get the roster for a team from a given server,
* and send a {@link BoxScore} to that server.
*/
public class StatReporterImpl {
/**
* Get a team roster from a {@link StatRecorder} running
* on the indicated host. If no server is running on
* the host, or if the host cannot be contacted, the
* method will return null.
* @param fromHost name of the host on which the
* server is running
* @param Team the {@link Team} whose roster
* is desired
* @return a set of {@link Player} objects that
* make up the roster of the team, or {@code null}
* if there is a problem contacting the server
*/
public Set<Player> getPlayers(String fromHost, String Team) {
try {
StatRecorder recorder = getRecorder(fromHost);
if (recorder != null) {
return recorder.getRoster(Team);
} else {
return null;
}
} catch (RemoteException e) {
System.out.println("Unable to find roster for team " + Team);
e.printStackTrace();
return null;
}
}
/**
* Send the results of a game to a {@link StatRecorder}
* running on the indicated host. If there is a problem
* contacting the host, print an error message on the
* console
* @param toHost name of the host on which the server
* is running
* @param score the {@link BoxScore} for the game
* being reported
*/
public void reportGame(String toHost, BoxScore score) {
try {
StatRecorder recorder = getRecorder(toHost);
if (recorder != null) {
recorder.recordGame(score);
}
} catch (Exception e) {
System.out.println("Unable to report game");
e.printStackTrace();
}
}
/**
* A private method that does the work of getting
* a {@link Remote} reference to the {@link StatRecorder}.
* If there is a problem with the communication, or if
* no server is running, a {@code null} will be returned.
* @param fromHost name of the host on which the
* {@link StatRecorder} is running
* @return a remote reference to the {@link StatRecorder},
* or {@code null} if there is no such server or if it cannot
* be contacted.
*/
private StatRecorder getRecorder(String fromHost) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
Registry useRegistry = LocateRegistry.getRegistry(fromHost);
return ((StatRecorder) useRegistry.lookup("Recorder"));
} catch (Exception e) {
System.out.println("Unable to find StatRecorder");
e.printStackTrace();
return null;
}
}
}
package org.oreilly.javaGoodParts.examples.impl;
import java.util.HashMap;
import java.util.HashSet;
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, Set<Player>> byName = new HashMap<String, Set<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 set of the players that are on this team
*/
public Set<Player> getRoster() {
return new HashSet<Player>(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 {
HashSet<Player> newSet = new HashSet<Player>();
newSet.add(toAdd);
byName.put(toAdd.getName(), newSet);
}
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 Set<Player> getPlayer(String name) {
return byName.get(name);
}
@Override
public Player getPlayer(UUID playerId) {
return byIds.get(playerId);
}
}
package org.oreilly.javaGoodParts.examples.statistics;
/**
* An interface that defines the notion of a batter in a baseball statistics
* package. Each at-bat will be recorded for the hitter, along with the result
* of that at-bat. Running totals of the important statistics will be available.
*
*/
public interface Batter {
/**
* The possible results of an at-bat for the hitter
*
*/
enum AtBatResult {
strikeOut, fieldOut, base1, base2, base3, base4, walk, reachOnError, sacrifice
}
/**
* Get the {@list Player} for whom these batting statistics apply
*
* @return a {@list Player} object for whom the batting statistics are being
* kept
*/
Player getPlayer();
/**
* Used to record an at-bat. The result of the at-bat is passed in and
* recorded.
*
* @param what
* The result of the at-bat
*/
void atBat(AtBatResult what);
/**
* Returns the batting average for the hitter, defined as number of hits
* divided by the number of at-bats.
*
* @return The batting average, as a float
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getAverage() throws NotEnoughAtBatsException;
/**
* Returns the on-base percentage for this hitter, defined as (hits +
* walks)/at-bats.
*
* @return the on-base percentage
* @throws {@link NotEnoughAtBatsException} if the number of at-bats, walks,
* and sacrifices is insufficient to establish a meaningful on-base
* percentage
*/
float getOBP() throws NotEnoughAtBatsException;
/**
* Return the slugging percentage for the batter, defined as total number of
* bases / at bats.
*
* @return The slugging percentage, as a float
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getSlugging() throws NotEnoughAtBatsException;
/**
* Return the total number of bases for the hitter
*
* @return Total bases, as an integer.
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
int getTotalBases() throws NotEnoughAtBatsException;
/**
* return the total number of at-bats for this hitter. Note that walks and
* sacrifices do not count as at-bats.
*
* @return the total number of official at-bats for this batter.
*/
int getAtBats();
}
package org.oreilly.javaGoodParts.examples.statistics;
import java.util.List;
import java.util.UUID;
/**
* An interface that will allow passing all of the
* statistics from a single game to a {@link StatRecorder},
* which will record the statistics for that game.
*/
public interface BoxScore {
/**
* Get a {@link List} of the names of the teams that played
* the game. Unless something is very strange,
* there should be exactly two teams in the List
* @return A {@link List} of {@link String} that are the names
* containing the two teams that played the game
*/
List<String> getTeams();
/**
* Get a {@link List} of the ids of the players who were
* in the game for the indicated team
* @param forTeam The {@link String} name of
* the team for which
* the list of players returned plays
* @return a {@link List} of {@link UUID} identifying
* who played for the team in the game
* being reported
*/
List <UUID> getPlayers(String forTeam);
/**
* Get the position played by the particular
* player today.
* @param forPlayer The {@link UUID} which
* is the player id of the player whose
* position for the game is being reported
* @return the {@link Player.Position} played
* by the player in this game
* @throws DidNotPlayException if the player
* did not play in the game being reported
*/
Player.Position getPosition(UUID forPlayer)
throws DidNotPlayException;
/**
* Gets a list of the at-bat results for a player
* @param playerID the {@link UUID} for
* the player whose batting is being reported
* @return a list of {@link Batter.AtBatResults} for
* the player in the particular game. If the player
* did not bat in the game, or if the player did not
* play in the game, the list will be empty
*/
List<Batter.AtBatResult> getBatting(UUID playerID);
/**
* Gets a list of the fielding results for the indicated
* player in this game.
* @param forPlayer the {@link UUID} player id
* for the player whose fielding results are being reported
* @return a list of {@link Fielder.AttemptResult} for
* the player in the particular game. If the player made
* no plays in the field, or did not play, the list will be
* empty
*/
List<Fielder.AttemptResult> getFielding(UUID forPlayer);
/**
* Gets the number of passed balls being reported
* for a particlar player in this game. Note that this
* should only be called if the player had the {@link Player.Position}
* of catcher sometime during the game
* @param forPlayer The {@link UUID} player id
* for the player whose passed ball count is being reported
* @return the number of passed balls recorded
* for the player during the game. If there were no
* passed balls for the player, or if the player was
* not catching during the game, or if the player did
* not play, the returned value will be 0
*/
int getPassedBalls(UUID forPlayer);
}
D
/Team.java/1.3/Fri Nov 13 15:34:58 2009//
/Catcher.java/1.2/Tue Oct 20 11:22:27 2009//
/Fielder.java/1.2/Tue Oct 20 11:22:27 2009//
/Player.java/1.2/Tue Oct 20 11:20:19 2009//
/NotEnoughAtBatsException.java/1.2/Tue Oct 20 10:59:47 2009//
/Batter.java/1.2/Tue Oct 20 11:22:12 2009//
GoodPartsExamples_collections/src/org/oreilly/javaGoodParts/examples/statistics
package org.oreilly.javaGoodParts.examples.statistics;
/**
* An interface that defines the notion of a batter
* in a baseball statistics package. Each at-bat will
* be recorded for the hitter, along with the result of
* that at-bat. Running totals of the important statistics
* will be available.
*
*/
public interface Batter {
/**
* The possible results of an at-bat for the hitter
*
*/
public enum AtBatResult {
strikeOut, fieldOut, base1, base2, base3, base4, walk,
reachOnError, sacrifice
}
/**
* Method used to record an at-bat. The result
* of the at-bat is passed in and recorded
* @param what The result of the at-bat
*/
public void atBat(AtBatResult what);
/**
* Returns the batting average for the hitter, defined as
* number of hits divided by the number of at-bats
* @return The batting average, as a float
* @throws NotEnoughAtBatsException if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getAverage() throws NotEnoughAtBatsException;
/**
* Returns the on-base percentage for this hitter, defined as
* (hits + walks)/at-bats
* @return the on-base percentage
* @throws {@link NotEnoughAtBatsException} if the number of at-bats, walks,
* and sacrifices is insufficient to establish a meaningful on-base
* percentage
*/
float getOBP() throws NotEnoughAtBatsException;
/**
* Return the slugging percentage for the batter, defined as
* total number of bases / at bats
* @return The slugging percentage, as a float
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getSlugging() throws NotEnoughAtBatsException;
/**
* Return the total number of bases for the hitter
* @return Total bases, as an integer
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
int getTotalBases() throws NotEnoughAtBatsException;
/**
* return the total number of at-bats for this hitter. Note that
* walks and sacrifices do not count as at-bats
* @return the total number of official at-bats for
* this batter.
*/
int getAtBats();
}
package org.oreilly.javaGoodParts.examples.statistics;
/**
* An interface that defines the notion of a batter
* in a baseball statistics package. Each at-bat will
* be recorded for the hitter, along with the result of
* that at-bat. Running totals of the important statistics
* will be available.
*
*/
public interface Batter {
/**
* The possible results of an at-bat for the hitter
*
*/
public enum AtBatResult {
strikeOut, fieldOut, base1, base2, base3, base4, walk,
reachOnError, sacrifice
}
/**
* Used to record an at-bat. The result
* of the at-bat is passed in and recorded.
* @param what The result of the at-bat
*/
public void atBat(AtBatResult what);
/**
* Returns the batting average for the hitter, defined as
* number of hits divided by the number of at-bats.
* @return The batting average, as a float
* @throws {@link NotEnoughAtBatsException}
* if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getAverage() throws NotEnoughAtBatsException;
/**
* Returns the on-base percentage for this hitter, defined as
* (hits + walks)/at-bats.
* @return the on-base percentage
* @throws {@link NotEnoughAtBatsException} if the number of at-bats, walks,
* and sacrifices is insufficient to establish a meaningful on-base
* percentage
*/
float getOBP() throws NotEnoughAtBatsException;
/**
* Return the slugging percentage for the batter, defined as
* total number of bases / at bats.
* @return The slugging percentage, as a float
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getSlugging() throws NotEnoughAtBatsException;
/**
* Return the total number of bases for the hitter
* @return Total bases, as an integer.
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
int getTotalBases() throws NotEnoughAtBatsException;
/**
* return the total number of at-bats for this hitter. Note that
* walks and sacrifices do not count as at-bats.
* @return the total number of official at-bats for
* this batter.
*/
int getAtBats();
}
package org.oreilly.javaGoodParts.examples.statistics;
/**
* An interface that defines the notion of a batter
* in a baseball statistics package. Each at-bat will
* be recorded for the hitter, along with the result of
* that at-bat. Running totals of the important statistics
* will be available.
*
*/
public interface Batter {
/**
* The possible results of an at-bat for the hitter
*
*/
public enum AtBatResult {
strikeOut, fieldOut, base1, base2, base3, base4, walk,
reachOnError, sacrifice
}
/**
* Used to record an at-bat. The result
* of the at-bat is passed in and recorded.
* @param what The result of the at-bat
*/
public void atBat(AtBatResult what);
/**
* Returns the batting average for the hitter, defined as
* number of hits divided by the number of at-bats.
* @return The batting average, as a float
* @throws {@link NotEnoughAtBatsException}
* if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getAverage() throws NotEnoughAtBatsException;
/**
* Returns the on-base percentage for this hitter, defined as
* (hits + walks)/at-bats.
* @return the on-base percentage
* @throws {@link NotEnoughAtBatsException} if the number of at-bats, walks,
* and sacrifices is insufficient to establish a meaningful on-base
* percentage
*/
float getOBP() throws NotEnoughAtBatsException;
/**
* Return the slugging percentage for the batter, defined as
* total number of bases / at bats.
* @return The slugging percentage, as a float
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
float getSlugging() throws NotEnoughAtBatsException;
/**
* Return the total number of bases for the hitter
* @return Total bases, as an integer.
* @throws {@link NotEnoughAtBatsException} if the number of at-bats is
* insufficient to establish a meaningful average
*/
int getTotalBases() throws NotEnoughAtBatsException;
/**
* return the total number of at-bats for this hitter. Note that
* walks and sacrifices do not count as at-bats.
* @return the total number of official at-bats for
* this batter.
*/
int getAtBats();
}