/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java",
* 2nd edition by Martin P. Robillard.
*
* Copyright (C) 2022 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*
*******************************************************************************/
package e2.chapter8;
/**
* A visitor to compute the total number of cards in a card
* source.
*/
public class CountingVisitor extends AbstractCardSourceVisitor
{
private int aCount = 0;
@Override
public void visitDeck(Deck pDeck)
{
for( @SuppressWarnings("unused") Card card : pDeck)
{
aCount++;
}
}
@Override
public void visitCardSequence(CardSequence pCardSequence)
{
aCount += pCardSequence.size();
}
public int getCount()
{
return aCount;
}
public void reset()
{
aCount = 0;
}
}
The SuppressWarnings
annotation interface is applicable
in all declaration contexts, so an @SuppressWarnings
annotation can be used on any element. As a matter of style,
programmers should always use this annotation on the most deeply
nested element where it is effective. For example, if you want to
suppress a warning in a particular method, you should annotate that
method rather than its class.
The set of warnings suppressed in a given element is a union of
the warnings suppressed in all containing elements. For example,
if you annotate a class to suppress one warning and annotate a
method in the class to suppress another, both warnings will be
suppressed in the method. However, note that if a warning is
suppressed in a module-info
file, the suppression applies
to elements within the file and not to types contained
within the module. Likewise, if a warning is suppressed in a
package-info
file, the suppression applies to elements
within the file and not to types contained within the
package.
Java compilers must recognize all the kinds of warnings defined in the Java Language Specification (JLS section 9.6.4.5) which include:
"unchecked"
.
"deprecation"
.
"removal"
.
"preview"
.
javac
reference implementation recognizes compilation-related warning
names documented in its --help-lint
output.