は確かhasItems()
が真でなければ意味がありませんgetNextItem()
を呼び出して、意味的に奇妙に聞こえますか?
とにかく、あなたのロジックがどのように動作するのかという考え方は、あなたの条件をチェックする新しいクラスにあなたのOutgoingItems
オブジェクトをラップしてください。私はassert
で小切手を出しましたが、好きな方法で選ぶことができます。
public class OutgoingItemsDelegate {
private final OutgoingItems items;
public OutgoingItemsDelegate(OutgoingItems items) {
this.items = items;
}
public boolean hasItems() {
return items.hasItems();
}
public Object getNextItem() {
assert !items.hasItems();
// alternatively you can do this:
// if (items.hasItems()) throw new IllegalStateException("hasItems() must not be true");
return items.getNextItems();
}
}
あなたはこのソリューションを支払う必要があるかもしれない、との理想的なソリューションがOutgoingItems
自体を修正することですが、それは現実的な選択肢だ、ということができないわずかなパフォーマンスとスペースのオーバーヘッドはもちろんあります。
* assert
ステートメントを評価するには、コマンドラインで-ea
フラグを使用する必要があります。