Package: ProgressHandler$Progress
ProgressHandler$Progress
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ProgressHandler.Progress(String) |
|
|
|
|
|
||||||||||||||||||||
completed() |
|
|
|
|
|
||||||||||||||||||||
getDone() |
|
|
|
|
|
||||||||||||||||||||
getName() |
|
|
|
|
|
||||||||||||||||||||
getTotal() |
|
|
|
|
|
||||||||||||||||||||
incrementDone() |
|
|
|
|
|
||||||||||||||||||||
incrementTotal() |
|
|
|
|
|
||||||||||||||||||||
reset() |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.mediascanner.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.inject.Inject;
32: import java.util.List;
33: import it.tidalwave.messagebus.MessageBus;
34: import it.tidalwave.bluemarine2.mediascanner.ScanCompleted;
35: import lombok.Getter;
36: import lombok.RequiredArgsConstructor;
37: import lombok.ToString;
38: import lombok.extern.slf4j.Slf4j;
39:
40: /***********************************************************************************************************************
41: *
42: * @author Fabrizio Giudici
43: *
44: **********************************************************************************************************************/
45: @ToString(exclude = { "messageBus", "all" }) @Slf4j
46: public class ProgressHandler
47: {
48: @RequiredArgsConstructor @Getter
49: static class Progress
50: {
51: @Nonnull
52: private final String name;
53:
54: @Nonnegative
55: private volatile int total;
56:
57: @Nonnegative
58: private volatile int done;
59:
60: public synchronized void reset()
61: {
62: total = done = 0;
63: }
64:
65: public synchronized void incrementTotal()
66: {
67: total++;
68: }
69:
70: public synchronized void incrementDone()
71: {
72: done++;
73: }
74:
75: public synchronized boolean completed()
76: {
77:• return done == total;
78: }
79:
80: @Override
81: public synchronized String toString()
82: {
83:• return String.format("%d/%d (%d%%)", done, total, (total == 0) ? 0 : (100 * done) / total);
84: }
85: }
86:
87: @Inject
88: private MessageBus messageBus;
89:
90: private final Progress folders = new Progress("folders");
91: private final Progress fingerprints = new Progress("fingerprints");
92: private final Progress mediaItems = new Progress("mediaItems");
93: private final Progress artists = new Progress("artists");
94: private final Progress records = new Progress("records");
95: private final Progress downloads = new Progress("downloads");
96: private final Progress insertions = new Progress("insertions");
97:
98: private final List<Progress> all = List.of(folders, fingerprints, mediaItems, artists, records, downloads, insertions);
99:
100: // TODO: should also collect errors
101:
102: public synchronized void reset()
103: {
104: all.forEach(Progress::reset);
105: }
106:
107: public void incrementTotalFolders()
108: {
109: folders.incrementTotal();
110: check();
111: }
112:
113: public void incrementScannedFolders()
114: {
115: folders.incrementDone();
116: check();
117: }
118:
119: public void incrementTotalMediaItems()
120: {
121: mediaItems.incrementTotal();
122: fingerprints.incrementTotal();
123: check();
124: }
125:
126: public void incrementDoneFingerprints()
127: {
128: fingerprints.incrementDone();
129: check();
130: }
131:
132: public void incrementImportedMediaItems()
133: {
134: mediaItems.incrementDone();
135: check();
136: }
137:
138: public void incrementTotalArtists()
139: {
140: artists.incrementTotal();
141: check();
142: }
143:
144: public void incrementImportedArtists()
145: {
146: artists.incrementDone();
147: check();
148: }
149:
150: public void incrementTotalDownloads()
151: {
152: downloads.incrementTotal();
153: check();
154: }
155:
156: public void incrementCompletedDownloads()
157: {
158: downloads.incrementDone();
159: check();
160: }
161:
162: public void incrementTotalRecords()
163: {
164: records.incrementTotal();
165: check();
166: }
167:
168: public void incrementImportedRecords()
169: {
170: records.incrementDone();
171: check();
172: }
173:
174: public void incrementTotalInsertions()
175: {
176: insertions.incrementTotal();
177: check();
178: }
179:
180: public void incrementCompletedInsertions()
181: {
182: insertions.incrementDone();
183: check();
184: }
185:
186: private void check()
187: {
188: log.debug("{}", this);
189:
190: if (isCompleted())
191: {
192: messageBus.publish(new ScanCompleted());
193: }
194: }
195:
196: public synchronized boolean isCompleted()
197: {
198: return all.stream().allMatch(Progress::completed);
199: }
200: }