1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.job.internal

File DefaultJobProgressStep.java

 

Coverage histogram

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

Code metrics

30
64
18
1
308
159
35
0.55
3.56
18
1.94

Classes

Class Line # Actions
DefaultJobProgressStep 33 64 0% 35 7
0.937593.8%
 

Contributing tests

This file is covered by 184 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: f1c52c076f86fbefbefaf2a51a86113834a88334 $
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    // Stored data
44   
45    protected final Message message;
46   
47    protected final int index;
48   
49    protected double offset;
50   
51    protected int maximumChildren = -1;
52   
53    protected double childSize;
54   
55    protected List<DefaultJobProgressStep> children;
56   
57    private final long startTime;
58   
59    private boolean finished;
60   
61    private boolean levelFinished;
62   
63    private long elapsedTime;
64   
65    /**
66    * @param message the message associated to the step
67    * @param source who asked to create this new step
68    * @param parent the parent step
69    */
 
70  11608 toggle public DefaultJobProgressStep(Message message, Object source, DefaultJobProgressStep parent)
71    {
72  11608 this.message = message;
73  11608 this.parent = parent;
74  11608 this.source = source;
75   
76  11608 if (this.parent != null) {
77  10940 this.index = parent.children.size();
78  10940 this.startTime = this.index == 0 ? parent.startTime : System.nanoTime();
79    } else {
80  668 this.index = 0;
81  668 this.startTime = System.nanoTime();
82    }
83   
84  11608 this.offset = 0.0D;
85    }
86   
87    /**
88    * @return true if the step is a virtual step (child of an empty level)
89    */
 
90  96282 toggle public boolean isVirtual()
91    {
92  96282 return getParent() != null && getParent().getChildren().isEmpty();
93    }
94   
 
95  0 toggle @Override
96    public Message getMessage()
97    {
98  0 return this.message;
99    }
100   
 
101  201241 toggle @Override
102    public DefaultJobProgressStep getParent()
103    {
104  201241 return this.parent;
105    }
106   
 
107  83509 toggle @Override
108    public List<DefaultJobProgressStep> getChildren()
109    {
110  83509 return this.children != null ? this.children : Collections.<DefaultJobProgressStep>emptyList();
111    }
112   
 
113  239 toggle @Override
114    public double getOffset()
115    {
116  239 return this.offset;
117    }
118   
 
119  8466 toggle @Override
120    public long getElapsedTime()
121    {
122  8466 return isFinished() ? this.elapsedTime : System.nanoTime() - this.startTime;
123    }
124   
125    /**
126    * Make sure it's allowed to modify the step.
127    */
 
128  61572 toggle private void assertModifiable()
129    {
130  61572 if (isFinished()) {
131  0 throw new UnsupportedOperationException("Step is closed");
132    }
133    }
134   
135    /**
136    * @param stepMessage the message associated to the step
137    * @param newStepSource who asked to create this new step
138    * @return the new step
139    */
 
140  7893 toggle public DefaultJobProgressStep addStep(Message stepMessage, Object newStepSource)
141    {
142  7893 assertModifiable();
143   
144  7893 if (this.children == null) {
145  0 this.children = new ArrayList<DefaultJobProgressStep>();
146    }
147   
148  7893 DefaultJobProgressStep step = new DefaultJobProgressStep(stepMessage, newStepSource, this);
149   
150  7893 this.children.add(step);
151   
152    // Update offset if needed
153  7893 if (this.maximumChildren <= 0) {
154  1692 this.childSize = 1.0D / this.children.size();
155  1692 double newOffset = this.childSize * (this.children.size() - 1);
156  1692 move(newOffset - this.offset);
157    }
158   
159  7893 return step;
160    }
161   
162    /**
163    * Add level with unknown number of steps to the step and return a virtual step as child of the level.
164    *
165    * @param newLevelSource who asked to create this new level
166    * @return the new step
167    */
 
168  5 toggle public DefaultJobProgressStep addLevel(Object newLevelSource)
169    {
170  5 return addLevel(0, newLevelSource);
171    }
172   
173    /**
174    * Add children to the step and return the first one.
175    *
176    * @param steps the number of step
177    * @param newLevelSource who asked to create this new level
178    * @return the new step
179    */
 
180  3047 toggle public DefaultJobProgressStep addLevel(int steps, Object newLevelSource)
181    {
182  3047 assertModifiable();
183   
184  3047 this.maximumChildren = steps;
185  3047 this.levelSource = newLevelSource;
186   
187  3047 if (steps > 0) {
188  2568 this.childSize = 1.0D / steps;
189    }
190   
191  3047 if (this.maximumChildren > 0) {
192  2568 this.children = new ArrayList<>(this.maximumChildren);
193    } else {
194  479 this.children = new ArrayList<>();
195    }
196   
197    // Create a virtual child
198  3047 return new DefaultJobProgressStep(null, newLevelSource, this);
199    }
200   
201    /**
202    * @param size update the offset with the provided size
203    */
 
204  42739 toggle public void move(double size)
205    {
206  42739 assertModifiable();
207   
208  42739 if (size != 0D) {
209  40187 this.offset += size;
210   
211    // Fix size
212  40187 double actualSize = size;
213  40187 if (this.offset > 1D) {
214  323 actualSize -= this.offset - 1D;
215  323 this.offset = 1D;
216    }
217   
218    // Update parent offset
219  40187 if (this.parent != null && actualSize != 0D) {
220  32573 actualSize *= this.parent.childSize;
221   
222  32573 this.parent.move(actualSize);
223    }
224    }
225    }
226   
227    /**
228    * Move to next child step.
229    *
230    * @param stepMessage the message associated to the step
231    * @param newStepSource who asked to create this new step
232    * @return the new step
233    */
 
234  7893 toggle public DefaultJobProgressStep nextStep(Message stepMessage, Object newStepSource)
235    {
236  7893 assertModifiable();
237   
238    // Close current step and move to the end
239  7893 finishStep();
240   
241    // Add new step
242  7893 return addStep(stepMessage, newStepSource);
243    }
244   
245    /**
246    * Finish current step.
247    */
 
248  16367 toggle public void finishStep()
249    {
250    // Close step
251  16367 if (this.children != null && !this.children.isEmpty()) {
252  7886 this.children.get(this.children.size() - 1).finish();
253    }
254    }
255   
256    /**
257    * @return true if the step is closed
258    */
 
259  79664 toggle public boolean isFinished()
260    {
261  79664 return this.finished || isVirtual();
262    }
263   
264    /**
265    * @return true if the step level is closed
266    */
 
267  21941 toggle public boolean isLevelFinished()
268    {
269  21941 return this.levelFinished || isVirtual();
270    }
271   
272    /**
273    * Close the step level.
274    */
 
275  11093 toggle public void finishLevel()
276    {
277  11093 if (!isLevelFinished()) {
278    // Make sure current children is closed
279  8474 finishStep();
280   
281    // Move offset to the end of the step (in case some sub-steps were not executed)
282  8474 move(1.0D - this.offset);
283   
284  8474 this.levelSource = null;
285   
286    // Mark it as finished
287  8474 this.levelFinished = true;
288    }
289    }
290   
291    /**
292    * Close the step.
293    */
 
294  9626 toggle public void finish()
295    {
296  9626 if (!isFinished()) {
297  8466 finishLevel();
298   
299    // Calculate the elapsed time
300  8466 this.elapsedTime = getElapsedTime();
301   
302  8466 this.source = null;
303   
304    // Mark it as finished
305  8466 this.finished = true;
306    }
307    }
308    }