Content of file ProgressHandler.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ProgressHandler.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">blueMarine II :: Media Scanner</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.mediascanner.impl</a> &gt; <span class="el_source">ProgressHandler.java</span></div><h1>ProgressHandler.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.mediascanner.impl;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.List;
import it.tidalwave.messagebus.MessageBus;
import it.tidalwave.bluemarine2.mediascanner.ScanCompleted;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L45">@ToString(exclude = { &quot;messageBus&quot;, &quot;all&quot; }) @Slf4j</span>
<span class="fc" id="L46">public class ProgressHandler</span>
  {
<span class="fc" id="L48">    @RequiredArgsConstructor @Getter</span>
    static class Progress
      {
        @Nonnull
<span class="nc" id="L52">        private final String name;</span>

        @Nonnegative
<span class="nc" id="L55">        private volatile int total;</span>

        @Nonnegative
<span class="nc" id="L58">        private volatile int done;</span>

        public synchronized void reset()
          {
<span class="fc" id="L62">            total = done = 0;</span>
<span class="fc" id="L63">          }</span>

        public synchronized void incrementTotal()
          {
<span class="fc" id="L67">            total++;</span>
<span class="fc" id="L68">          }</span>

        public synchronized void incrementDone()
          {
<span class="fc" id="L72">            done++;</span>
<span class="fc" id="L73">          }</span>

        public synchronized boolean completed()
          {
<span class="fc bfc" id="L77" title="All 2 branches covered.">            return done == total;</span>
          }

        @Override
        public synchronized String toString()
          {
<span class="fc bfc" id="L83" title="All 2 branches covered.">            return String.format(&quot;%d/%d (%d%%)&quot;, done, total, (total == 0) ? 0 : (100 * done) / total);</span>
          }
      }

    @Inject
    private MessageBus messageBus;

<span class="fc" id="L90">    private final Progress folders = new Progress(&quot;folders&quot;);</span>
<span class="fc" id="L91">    private final Progress fingerprints = new Progress(&quot;fingerprints&quot;);</span>
<span class="fc" id="L92">    private final Progress mediaItems = new Progress(&quot;mediaItems&quot;);</span>
<span class="fc" id="L93">    private final Progress artists = new Progress(&quot;artists&quot;);</span>
<span class="fc" id="L94">    private final Progress records = new Progress(&quot;records&quot;);</span>
<span class="fc" id="L95">    private final Progress downloads = new Progress(&quot;downloads&quot;);</span>
<span class="fc" id="L96">    private final Progress insertions = new Progress(&quot;insertions&quot;);</span>

<span class="fc" id="L98">    private final List&lt;Progress&gt; all = List.of(folders, fingerprints, mediaItems, artists, records, downloads, insertions);</span>

    // TODO: should also collect errors
should also collect errors
public synchronized void reset() { <span class="fc" id="L104"> all.forEach(Progress::reset);</span> <span class="fc" id="L105"> }</span> public void incrementTotalFolders() { <span class="fc" id="L109"> folders.incrementTotal();</span> <span class="fc" id="L110"> check();</span> <span class="fc" id="L111"> }</span> public void incrementScannedFolders() { <span class="fc" id="L115"> folders.incrementDone();</span> <span class="fc" id="L116"> check();</span> <span class="fc" id="L117"> }</span> public void incrementTotalMediaItems() { <span class="fc" id="L121"> mediaItems.incrementTotal();</span> <span class="fc" id="L122"> fingerprints.incrementTotal();</span> <span class="fc" id="L123"> check();</span> <span class="fc" id="L124"> }</span> public void incrementDoneFingerprints() { <span class="fc" id="L128"> fingerprints.incrementDone();</span> <span class="fc" id="L129"> check();</span> <span class="fc" id="L130"> }</span> public void incrementImportedMediaItems() { <span class="fc" id="L134"> mediaItems.incrementDone();</span> <span class="fc" id="L135"> check();</span> <span class="fc" id="L136"> }</span> public void incrementTotalArtists() { <span class="nc" id="L140"> artists.incrementTotal();</span> <span class="nc" id="L141"> check();</span> <span class="nc" id="L142"> }</span> public void incrementImportedArtists() { <span class="nc" id="L146"> artists.incrementDone();</span> <span class="nc" id="L147"> check();</span> <span class="nc" id="L148"> }</span> public void incrementTotalDownloads() { <span class="nc" id="L152"> downloads.incrementTotal();</span> <span class="nc" id="L153"> check();</span> <span class="nc" id="L154"> }</span> public void incrementCompletedDownloads() { <span class="nc" id="L158"> downloads.incrementDone();</span> <span class="nc" id="L159"> check();</span> <span class="nc" id="L160"> }</span> public void incrementTotalRecords() { <span class="nc" id="L164"> records.incrementTotal();</span> <span class="nc" id="L165"> check();</span> <span class="nc" id="L166"> }</span> public void incrementImportedRecords() { <span class="nc" id="L170"> records.incrementDone();</span> <span class="nc" id="L171"> check();</span> <span class="nc" id="L172"> }</span> public void incrementTotalInsertions() { <span class="fc" id="L176"> insertions.incrementTotal();</span> <span class="fc" id="L177"> check();</span> <span class="fc" id="L178"> }</span> public void incrementCompletedInsertions() { <span class="fc" id="L182"> insertions.incrementDone();</span> <span class="fc" id="L183"> check();</span> <span class="fc" id="L184"> }</span> private void check() { <span class="fc" id="L188"> log.debug(&quot;{}&quot;, this);</span> <span class="fc bfc" id="L190" title="All 2 branches covered."> if (isCompleted())</span> { <span class="fc" id="L192"> messageBus.publish(new ScanCompleted());</span> } <span class="fc" id="L194"> }</span> public synchronized boolean isCompleted() { <span class="fc" id="L198"> return all.stream().allMatch(Progress::completed);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>