/**
 * Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
 */
package e2.chapter8;

/**
 * A visitor that checks whether a card is contained in a card source 
 * structure.
 */
public class ChecksContainmentVisitor extends AbstractCardSourceVisitor
{
private final Card aCard; private boolean aResult = false;
public ChecksContainmentVisitor(Card pCard) { aCard = pCard; } public void reset() { aResult = false; } @Override
public void visitDeck(Deck pDeck) { for( Card card : pDeck) { if( card.equals(aCard)) { aResult = true; break; } } }
@Override public void visitCardSequence(CardSequence pCardSequence) { for( int i = 0; i < pCardSequence.size(); i++ ) { if( pCardSequence.get(i).equals(aCard)) { aResult = true; break; } } } @Override public void visitCompositeCardSource(CompositeCardSource pCompositeCardSource) {
if( !aResult) { super.visitCompositeCardSource(pCompositeCardSource); }
} public boolean contains() { return aResult; } }