Class ConnectedObjectScanner.MaskElementCounter

java.lang.Object
net.algart.matrices.scanning.ConnectedObjectScanner.MaskElementCounter
All Implemented Interfaces:
ConnectedObjectScanner.ElementVisitor
Enclosing class:
ConnectedObjectScanner

public static class ConnectedObjectScanner.MaskElementCounter extends Object implements ConnectedObjectScanner.ElementVisitor

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

    Fields
    Modifier and Type
    Field
    Description
    protected long
    The internal counter, stored in this object and returned by counter() method.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates new instance of this class with the specified mask matrix.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the internal counter: the value of counter field.
    Matrix<? extends BitArray>
    Returns the reference to mask matrix.
    void
    Resets the internal counter to 0.
    void
    visit(long[] coordinatesInMatrix, long indexInArray)
    This implementation increases the internal counter by 1, if the bit of the mask with the specified coordinates is 1.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • counter

      protected long counter
      The internal counter, stored in this object and returned by counter() method. This counter is increased by 1 for unit elements of the mask by visit(long[], long) method and is cleared to zero by reset() method. The default value after creating a new instance is zero.
  • Constructor Details

    • MaskElementCounter

      public MaskElementCounter(Matrix<? extends BitArray> mask)
      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 internal counter 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 interface ConnectedObjectScanner.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

      public Matrix<? extends BitArray> 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 internal counter 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 of counter field.
      Returns:
      the current value of the counter.