class NewsMakerList
extends java.lang.Object
implements java.io.Serializable
A NewsMakerList
is a list of NewsMaker
objects.
Each NewsMaker
in the list must have a unique name.
Note that this class now keeps track of whether its list has been sorted, to ensure that a binary search will work, if used.
Modifier and Type | Field and Description |
---|---|
private java.util.List<NewsMaker> |
newsMakers
The list of news makers.
|
private static long |
serialVersionUID
This is the first serializable version of NewsMakerList, so we select a
serialVersionUID of 1L.
|
private boolean |
sorted
A flag to indicate whether the list is sorted.
|
Constructor and Description |
---|
NewsMakerList()
The no-argument constructor initializes the list to be an empty
ArrayList of NewsMaker objects. |
Modifier and Type | Method and Description |
---|---|
void |
add(NewsMaker newsMaker)
The mutator for adding news makers to the list.
|
boolean |
contains(NewsMaker newsMaker)
An accessor method to test whether the list already contains a news
maker.
|
NewsMaker |
get(NewsMaker newsMaker)
An accessor method to get a news maker from the list.
|
NewsMaker |
getExactMatch(java.lang.String newsMakerName)
This method should be able to use a binary search to find the news maker
but relies on the list being sorted first.
|
NewsMaker |
getPartialMatch(java.lang.String newsMakerName)
This method searches for partial matches in the list, and returns the
first news maker that contains the search string specified.
|
void |
sort()
This method sorts the list using a stable sort.
|
private static final long serialVersionUID
private java.util.List<NewsMaker> newsMakers
private boolean sorted
NewsMakerList()
ArrayList
of NewsMaker
objects.public void add(NewsMaker newsMaker)
By using our own class with its own add
method, rather than
directly using the add
method of ArrayList
, we
can ensure that we don't add multiple NewsMaker
objects with
the same name to our list (thereby keeping the names unique).
newsMaker
- The news maker to add.java.lang.IllegalArgumentException
- If the news maker to add is already in the list.public boolean contains(NewsMaker newsMaker)
Simply makes use of the contains
method of
ArrayList
.
newsMaker
- The news maker to check for in the list.public NewsMaker get(NewsMaker newsMaker)
Note that NewsMaker
objects are mutable, so this really
should return a copy of the news maker instead. However, we haven't
studied that yet, so returning the news maker itself is acceptable for
now.
newsMaker
- The news maker to get from the list.public NewsMaker getExactMatch(java.lang.String newsMakerName)
sorted
flag and prints an error to the standard error if it
was called with an unsorted list. It will conduct a linear search if a
binary search is not possible.newsMakerName
- The exact name for which to search.public NewsMaker getPartialMatch(java.lang.String newsMakerName)
newsMakerName
- The string on which to search.public void sort()
sorted
flag to true
.