Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
examples
Java The Good Parts
Commits
ac1f02a8
Commit
ac1f02a8
authored
Jun 27, 2017
by
O'Reilly Media, Inc.
Browse files
Initial commit
parents
Changes
215
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
682 additions
and
0 deletions
+682
-0
src/org/oreilly/javaGoodParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.2
...odParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.2
+96
-0
src/org/oreilly/javaGoodParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.3
...odParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.3
+93
-0
src/org/oreilly/javaGoodParts/examples/impl/CVS/Root
src/org/oreilly/javaGoodParts/examples/impl/CVS/Root
+1
-0
src/org/oreilly/javaGoodParts/examples/impl/CVS/Template
src/org/oreilly/javaGoodParts/examples/impl/CVS/Template
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/Catcher.class
src/org/oreilly/javaGoodParts/examples/impl/Catcher.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/CatcherImpl.class
...org/oreilly/javaGoodParts/examples/impl/CatcherImpl.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/CatcherImpl.java
src/org/oreilly/javaGoodParts/examples/impl/CatcherImpl.java
+37
-0
src/org/oreilly/javaGoodParts/examples/impl/CheckBatter.class
...org/oreilly/javaGoodParts/examples/impl/CheckBatter.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/CheckBatter.java
src/org/oreilly/javaGoodParts/examples/impl/CheckBatter.java
+38
-0
src/org/oreilly/javaGoodParts/examples/impl/Fielder$AttemptResult.class
...y/javaGoodParts/examples/impl/Fielder$AttemptResult.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/Fielder.class
src/org/oreilly/javaGoodParts/examples/impl/Fielder.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/FielderImpl.class
...org/oreilly/javaGoodParts/examples/impl/FielderImpl.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/FielderImpl.java
src/org/oreilly/javaGoodParts/examples/impl/FielderImpl.java
+66
-0
src/org/oreilly/javaGoodParts/examples/impl/Formatter.class
src/org/oreilly/javaGoodParts/examples/impl/Formatter.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/Formatter.java
src/org/oreilly/javaGoodParts/examples/impl/Formatter.java
+91
-0
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.class
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.java
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.java
+161
-0
src/org/oreilly/javaGoodParts/examples/impl/StatRecorderImpl.class
...reilly/javaGoodParts/examples/impl/StatRecorderImpl.class
+0
-0
src/org/oreilly/javaGoodParts/examples/impl/StatRecorderImpl.java
...oreilly/javaGoodParts/examples/impl/StatRecorderImpl.java
+99
-0
src/org/oreilly/javaGoodParts/examples/impl/StatReporterImpl$RosterRetriever.class
...arts/examples/impl/StatReporterImpl$RosterRetriever.class
+0
-0
No files found.
src/org/oreilly/javaGoodParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.2
0 → 100644
View file @
ac1f02a8
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
);
}
}
src/org/oreilly/javaGoodParts/examples/impl/CVS/RevisionCache/TeamImpl.java#1.3
0 → 100644
View file @
ac1f02a8
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
);
}
}
src/org/oreilly/javaGoodParts/examples/impl/CVS/Root
0 → 100644
View file @
ac1f02a8
:local:/usr/local/cvsrep
src/org/oreilly/javaGoodParts/examples/impl/CVS/Template
0 → 100644
View file @
ac1f02a8
src/org/oreilly/javaGoodParts/examples/impl/Catcher.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/CatcherImpl.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/CatcherImpl.java
0 → 100644
View file @
ac1f02a8
/*
*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
;
}
}
src/org/oreilly/javaGoodParts/examples/impl/CheckBatter.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/CheckBatter.java
0 → 100644
View file @
ac1f02a8
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
;
}
}
src/org/oreilly/javaGoodParts/examples/impl/Fielder$AttemptResult.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/Fielder.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/FielderImpl.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/FielderImpl.java
0 → 100644
View file @
ac1f02a8
/*
* 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."
);
}
}
src/org/oreilly/javaGoodParts/examples/impl/Formatter.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/Formatter.java
0 → 100644
View file @
ac1f02a8
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
);
}
}
}
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.class
0 → 100644
View file @
ac1f02a8
File added
src/org/oreilly/javaGoodParts/examples/impl/PlayerImpl.java
0 → 100644
View file @
ac1f02a8
/*
* 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
();