1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.job.internal

File DefaultJobProgressStep.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

30
65
18
1
313
161
35
0.54
3.61
18
1.94

Classes

Class Line # Actions
DefaultJobProgressStep 33 65 0% 35 7
0.938053193.8%
 

Contributing tests

This file is covered by 231 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25   
26    import org.xwiki.job.event.status.JobProgressStep;
27    import org.xwiki.logging.Message;
28   
29    /**
30    * @version $Id: c8fee742263afe674ddbd6e64c77594d81e41b34 $
31    * @since 7.1M2
32    */
 
33    public class DefaultJobProgressStep implements JobProgressStep
34    {
35    // Not stored data (used only during progress)
36   
37    protected final transient DefaultJobProgressStep parent;
38   
39    protected transient Object source;
40   
41    protected transient Object levelSource;
42   
43    protected transient boolean levelStep;
44   
45    // Stored data
46   
47    protected final Message message;
48   
49    protected final int index;
50   
51    protected double offset;
52   
53    protected int maximumChildren = -1;
54   
55    protected double childSize;
56   
57    protected List<DefaultJobProgressStep> children;
58   
59    private final long startTime;
60   
61    private boolean finished;
62   
63    private boolean levelFinished;
64   
65    private long elapsedTime;
66   
67    /**
68    * @param message the message associated to the step
69    * @param source who asked to create this new step
70    * @param parent the parent step
71    */
 
72  478254 toggle public DefaultJobProgressStep(Message message, Object source, DefaultJobProgressStep parent)
73    {
74  478943 this.message = message;
75  479356 this.parent = parent;
76  479519 this.source = source;
77   
78  479580 if (this.parent != null) {
79  258662 this.index = parent.children.size();
80  259641 this.startTime = this.index == 0 ? parent.startTime : System.nanoTime();
81    } else {
82  220681 this.index = 0;
83  220681 this.startTime = System.nanoTime();
84    }
85   
86  480511 this.offset = 0.0D;
87    }
88   
89    /**
90    * @return true if the step is a virtual step (child of an empty level)
91    */
 
92  1773075 toggle public boolean isVirtual()
93    {
94  1774377 return getParent() != null && getParent().getChildren().isEmpty();
95    }
96   
 
97  0 toggle @Override
98    public Message getMessage()
99    {
100  0 return this.message;
101    }
102   
 
103  3716746 toggle @Override
104    public DefaultJobProgressStep getParent()
105    {
106  3714152 return this.parent;
107    }
108   
 
109  1454765 toggle @Override
110    public List<DefaultJobProgressStep> getChildren()
111    {
112  1454439 return this.children != null ? this.children : Collections.<DefaultJobProgressStep>emptyList();
113    }
114   
 
115  489 toggle @Override
116    public double getOffset()
117    {
118  489 return this.offset;
119    }
120   
 
121  181594 toggle @Override
122    public long getElapsedTime()
123    {
124  181557 return isFinished() ? this.elapsedTime : System.nanoTime() - this.startTime;
125    }
126   
127    /**
128    * Make sure it's allowed to modify the step.
129    */
 
130  1069444 toggle private void assertModifiable()
131    {
132  1066996 if (isFinished()) {
133  0 throw new UnsupportedOperationException("Step is closed");
134    }
135    }
136   
137    /**
138    * @param stepMessage the message associated to the step
139    * @param newStepSource who asked to create this new step
140    * @return the new step
141    */
 
142  158669 toggle public DefaultJobProgressStep addStep(Message stepMessage, Object newStepSource)
143    {
144  159066 assertModifiable();
145   
146  158684 if (this.children == null) {
147  0 this.children = new ArrayList<DefaultJobProgressStep>();
148    }
149   
150  158585 DefaultJobProgressStep step = new DefaultJobProgressStep(stepMessage, newStepSource, this);
151   
152  158770 this.children.add(step);
153   
154    // Update offset if needed
155  159205 if (this.maximumChildren <= 0) {
156  134166 this.childSize = 1.0D / this.children.size();
157  134002 double newOffset = this.childSize * (this.children.size() - 1);
158  134346 move(newOffset - this.offset);
159    }
160   
161  159156 return step;
162    }
163   
164    /**
165    * Add level with unknown number of steps to the step and return a virtual step as child of the level.
166    *
167    * @param newLevelSource who asked to create this new level
168    * @return the new step
169    */
 
170  20444 toggle public DefaultJobProgressStep addLevel(Object newLevelSource)
171    {
172  20778 return addLevel(0, newLevelSource, false);
173    }
174   
175    /**
176    * Add children to the step and return the first one.
177    *
178    * @param steps the number of step
179    * @param newLevelSource who asked to create this new level
180    * @param levelStep the new level can contains only one step
181    * @return the new step
182    */
 
183  99207 toggle public DefaultJobProgressStep addLevel(int steps, Object newLevelSource, boolean levelStep)
184    {
185  100512 assertModifiable();
186   
187  100052 this.maximumChildren = steps;
188  100016 this.levelSource = newLevelSource;
189   
190  100307 if (steps > 0) {
191  5186 this.childSize = 1.0D / steps;
192    }
193   
194  100106 if (this.maximumChildren > 0) {
195  5186 this.children = new ArrayList<>(this.maximumChildren);
196    } else {
197  94887 this.children = new ArrayList<>();
198    }
199   
200  99813 this.levelStep = levelStep;
201   
202    // Create a virtual child
203  100132 return new DefaultJobProgressStep(null, newLevelSource, this);
204    }
205   
206    /**
207    * @param size update the offset with the provided size
208    */
 
209  650815 toggle public void move(double size)
210    {
211  651304 assertModifiable();
212   
213  651144 if (size != 0D) {
214  456510 this.offset += size;
215   
216    // Fix size
217  456506 double actualSize = size;
218  456506 if (this.offset > 1D) {
219  704 actualSize -= this.offset - 1D;
220  704 this.offset = 1D;
221    }
222   
223    // Update parent offset
224  456554 if (this.parent != null && actualSize != 0D) {
225  336051 actualSize *= this.parent.childSize;
226   
227  336059 this.parent.move(actualSize);
228    }
229    }
230    }
231   
232    /**
233    * Move to next child step.
234    *
235    * @param stepMessage the message associated to the step
236    * @param newStepSource who asked to create this new step
237    * @return the new step
238    */
 
239  158366 toggle public DefaultJobProgressStep nextStep(Message stepMessage, Object newStepSource)
240    {
241  158868 assertModifiable();
242   
243    // Close current step and move to the end
244  157907 finishStep();
245   
246    // Add new step
247  158445 return addStep(stepMessage, newStepSource);
248    }
249   
250    /**
251    * Finish current step.
252    */
 
253  340207 toggle public void finishStep()
254    {
255    // Close step
256  340116 if (this.children != null && !this.children.isEmpty()) {
257  159610 this.children.get(this.children.size() - 1).finish();
258    }
259    }
260   
261    /**
262    * @return true if the step is closed
263    */
 
264  1725533 toggle public boolean isFinished()
265    {
266  1727025 return this.finished || isVirtual();
267    }
268   
269    /**
270    * @return true if the step level is closed
271    */
 
272  266342 toggle public boolean isLevelFinished()
273    {
274  266937 return this.levelFinished || isVirtual();
275    }
276   
277    /**
278    * Close the step level.
279    */
 
280  186868 toggle public void finishLevel()
281    {
282  186815 if (!isLevelFinished()) {
283    // Make sure current children is closed
284  181502 finishStep();
285   
286    // Move offset to the end of the step (in case some sub-steps were not executed)
287  181584 move(1.0D - this.offset);
288   
289  181630 this.levelSource = null;
290   
291    // Mark it as finished
292  181610 this.levelFinished = true;
293    }
294    }
295   
296    /**
297    * Close the step.
298    */
 
299  337769 toggle public void finish()
300    {
301  337900 if (!isFinished()) {
302  181576 finishLevel();
303   
304    // Calculate the elapsed time
305  181616 this.elapsedTime = getElapsedTime();
306   
307  181642 this.source = null;
308   
309    // Mark it as finished
310  181626 this.finished = true;
311    }
312    }
313    }