Class ConnectedObjectScanner.MaskElementCounter
- All Implemented Interfaces:
ConnectedObjectScanner.ElementVisitor
- Enclosing class:
ConnectedObjectScanner
The simplest implementation of ConnectedObjectScanner.ElementVisitor
interface.
It is also a good example for possible cloning in your code.
This class works with some bit matrix, named mask and specified in the constructor's argument.
It should have the same dimensions, as the matrix, scanned by the connected objects scanner:
ConnectedObjectScanner.matrix()
.
(If this condition is not fulfilled, visit
method can throw unexpected
IndexOutOfBoundException.)
The only thing, performed by visit
method of this class, is increasing
some internal long counter by 1. You can read this counter by counter()
method.
This counter is zero after instantiating this object and can be cleared by reset()
method.
So, this class allows to count the number of unit elements of the mask, lying at the same positions as the elements of some connected object of the scanned matrix.
This class is not thread-safe, but is thread-compatible and can be synchronized manually, if multithread access is necessary.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionMaskElementCounter
(Matrix<? extends BitArray> mask) Creates new instance of this class with the specified mask matrix. -
Method Summary
Modifier and TypeMethodDescriptionlong
counter()
Returns the internal counter: the value ofcounter
field.mask()
Returns the reference to mask matrix.void
reset()
Resets the internalcounter
to 0.void
visit
(long[] coordinatesInMatrix, long indexInArray) This implementation increases the internalcounter
by 1, if the bit of the mask with the specified coordinates is 1.
-
Field Details
-
counter
protected long counterThe internal counter, stored in this object and returned bycounter()
method. This counter is increased by 1 for unit elements of the mask byvisit(long[], long)
method and is cleared to zero byreset()
method. The default value after creating a new instance is zero.
-
-
Constructor Details
-
MaskElementCounter
Creates new instance of this class with the specified mask matrix.- Parameters:
mask
- the mask matrix, unit elements of which (corresponding the some unit elements of the scanned matrix) should be counted.- Throws:
NullPointerException
- if the argument is null.
-
-
Method Details
-
visit
public void visit(long[] coordinatesInMatrix, long indexInArray) This implementation increases the internalcounter
by 1, if the bit of the mask with the specified coordinates is 1.More precisely, this method does the following:
if (
mask()
.array().getBit(indexInArray)) {counter
++; }- Specified by:
visit
in interfaceConnectedObjectScanner.ElementVisitor
- Parameters:
coordinatesInMatrix
- the coordinates of the element in the bit matrix or, maybe, null (not used by this implementation).indexInArray
- the index of this element in the underlying array.
-
mask
Returns the reference to mask matrix. The result is identical to the argument of the constructor.- Returns:
- the reference to mask matrix, passed via constructor.
-
reset
public void reset()Resets the internalcounter
to 0.Usually this method should be called before scanning new connected object via
ConnectedObjectScanner.clear(ArrayContext, ConnectedObjectScanner.ElementVisitor, long[], boolean)
method. -
counter
public long counter()Returns the internal counter: the value ofcounter
field.- Returns:
- the current value of the counter.
-