View Javadoc
1   /*
2    * License : The MIT License
3    * Copyright(c) 2022 Olyutorskii
4    */
5   
6   package io.github.olyutorskii.aletojio.idling;
7   
8   /**
9    * Monitor pop-count of random number sequence.
10   *
11   * <ul>
12   * <li>pop-count of 0x00000000 is 0
13   * <li>pop-count of 0xa5a5a5a5 is 16
14   * <li>pop-count of 0xffffffff is 32
15   * </ul>
16   *
17   * <p>Pop-count is another name for Hamming-weight.
18   *
19   * @see <a href="https://en.wikipedia.org/wiki/Hamming_weight">Hamming weight(Wikipedia)</a>
20   */
21  public class PopCntMonitor extends AbstractRndMonitor {
22  
23      private final int popCt;
24  
25  
26      /**
27       * Constructor.
28       *
29       * @param pop pop counts (must be 0&lt;= and &lt;= 32)
30       * @throws IllegalArgumentException pop counts too small or large
31       */
32      public PopCntMonitor(int pop) throws IllegalArgumentException {
33          super();
34  
35          if (pop < 0 || Integer.SIZE < pop) {
36              throw new IllegalArgumentException();
37          }
38  
39          this.popCt = pop;
40  
41          return;
42      }
43  
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      protected void resetImpl() {
50          assert true;
51      }
52  
53      /**
54       * {@inheritDoc}
55       *
56       * @param iVal {@inheritDoc}
57       * @return {@inheritDoc}
58       */
59      @Override
60      protected boolean probeImpl(int iVal) {
61          boolean result =
62                  Integer.bitCount(iVal) == this.popCt;
63          return result;
64      }
65  
66  }