Conditionally returning objects in Java (without iterators)

Say I have a method getBook()in the Bookshelf class. The method should only return a book object if the bookshelf is not empty. However, Java requires the method to return a Book object under all circumstances. I may have a method isShelfEmpty()that is called first, but it is inconvenient to detect the implementation of the bookshelf to the caller. What is the cleanest way to do this?

An Iteratorwill make this simpler, but it is for a class project, and we have not yet considered iterators in the class.

Edit:

I was thinking of returning null, but for some reason this seems ugly to me for some reason. Is this the best option, or is there another?

+3
source share
8 answers

null, . null , , .

isBookAvailable() , getBook(), .

+10

, null . , , Bookshelf .

Bookshelf Book, , . .

+4

.

  • null, , Book getBook(...) , .

:

public class BookShelf {

  Book getBook(...) {
    if (empty) {
      return null;
    }
  }

}
  • , a Book, . , .

Sentinels NIL-, null, . - :

public class BookShelf {
  public static final NIL = new Book();

  ...

  public Book getBook(...) {
    if (empty) {
      return NIL;
    }
  }

}

; "NIL". Java , .

if (shelf.getBook(...) == null) {
   System.out.println("We ran out of books!");
}

if (shelf.getBook(...) == BookShelf.NIL) {
   System.out.println("We ran out of books!");
}

, Java,

if (BookShelf.NIL.equals(shelf.getBook(...))) {
   System.out.println("We ran out of books!");
}

, , , . , ( , ), , " ".

+1

java.lang.IllegalStateException, . javadocs IllegalStateException

, . , Java Java .

, :

public Book getBook() {
    if(books.isEmpty()) {
        throw new IllegalStateException();
    }
    // return the book, knowing that the books list has at least one item in it.
}
+1

Null, -, . , - "BookOrEmptySpace" ( ), "" " ".

, , BookOrEmptySpace, , EmptySpace.

, EmptySpace , null - , null - , , , ,

, - , @Nullable ( , null, )

0

, , , !!?! , -, , - , , , .

, ?!?!

0

.

  • :
    • null
  • :
    • , .

C/++ Python, , , , (, getBookAtIndex(14)), null, NULL, None .. , , .

0

, , / .

-, , :

public Book getBook() {
    public static Book NO_BOOK = new Book();
    if(books.empty()) {
        return NO_BOOK;
    }
    ...
}
book = shelf.getBook();
if(Book.NO_BOOK.equals(book)) {
    // there were no books
}

null, , ,

public Book getBook() {
    if(books.empty()) {
        return null;
    }
    ...
}
book = shelf.getBook();
if(book == null) {
    // there were no books
}

, , hasMoreBooks.

public boolean hasMoreBooks() {
    return (! books.empty());
}
public Book getBook() {
    if(books.empty()) {
        throw new IllegalStateException("no more books");
    }
    ...
}
if(shelf.hasMoreBooks()) {
    book = shelf.getBook();
    // you know it a valid book here
}

Maybe Haskell . , " ". , . , ( ..) (null is teh devil;), . Java , .

" ", , , " " , .

: , , . ( hasMoreBooks) , , .

, , , FAR , , - :

try {
    while(true) {
        book = shelf.getBook();
        ...
    }
} catch(IllegalStateException ex) {
    // no more books
}

, , ... hasMoreBooks, , , .

0

Source: https://habr.com/ru/post/1767581/


All Articles