I have the following Book
class (I've omitted the irrelevant code).
public final class Book {
public Book(String bookTitle, List<Author> listofAuthors, int numberofchapters, String ISBN)
{
//Ommitted other variable initializations.
this.checkAuthors(listofAuthors);
this.listofAuthors = Collections.unmodifiableList(new ArrayList<Author>(listofAuthors));
}
private void checkAuthors(List<Author> checkifEmpty)
{
if(checkifEmpty == null){
throw new IllegalArgumentException( "List of authors is null");
}
if (checkifEmpty.size() == 0){
throw new IllegalArgumentException( "The authors list contains no authors");
}
for (Author checkEmpty : checkifEmpty){
if(checkEmpty == null){
throw new IllegalArgumentException("A book must have at least one author");
}
}
}
I included a private method to check the List
of authors to make sure that the List
collection isn't 0, and to prevent
Author bookAuthor = null;
List<Author> listofAuthors = new ArrayList<Author>();
listofAuthors.add(bookAuthor);
or
List<Author> listofAuthors = null;
Book tlac = new Book("book title goes here", listofAuthors, 30, "isbn goes here");
from happening.
Questions:
Is calling a
private
method from theconstructor
and doing the work there considered a bad practice? If so, how should I correct it?In the
private
methodcheckAuthors
I use the==
instead ofequals()
. My intention is to check that no object stored in theList<Author>
has a value ofnull
. Is this case, I found using==
works, if I were to useequals()
it give menull
as the error message. Is using==
correct in this case, if not, what should I be doing?