View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  /**
19   * Print a dot to the console each time we make at least 1% progress towards the total
20   */
21  public class PercentCompleteInformer extends AbstractProgressInformer {
22  
23  	protected long progress;
24  
25  	int percentageIncrement = 1;
26  	int percentCompletePrevious;
27  	long total;
28  
29  	public PercentCompleteInformer() {
30  		this(0);
31  	}
32  
33  	public PercentCompleteInformer(long total) {
34  		this(total, null);
35  	}
36  
37  	public PercentCompleteInformer(long total, LogMsg startMessage) {
38  		super();
39  		this.total = total;
40  		this.startMessage = startMessage;
41  	}
42  
43  	/**
44  	 * Thread safe method for incrementing progress by one
45  	 */
46  	public synchronized void incrementProgress() {
47  		incrementProgress(1);
48  	}
49  
50  	/**
51  	 * Thread safe method for incrementing progress by <code>amount</code>
52  	 */
53  	public synchronized void incrementProgress(long amount) {
54  		// Increment the progress indicator
55  		this.progress += amount;
56  
57  		// Calculate how far along we are
58  		int percentComplete = (int) ((progress * 100) / total);
59  
60  		// Print a dot to the console any time we make at least 1% progress
61  		if (isEnoughProgress(percentComplete, percentCompletePrevious, percentageIncrement)) {
62  			this.percentCompletePrevious = percentComplete;
63  			printStream.print(progressToken);
64  		}
65  	}
66  
67  	protected boolean isEnoughProgress(int percentComplete, int percentCompletePrevious, int percentageIncrement) {
68  		int needed = percentCompletePrevious + percentageIncrement;
69  		return percentComplete >= needed;
70  	}
71  
72  	public int getPercentageIncrement() {
73  		return percentageIncrement;
74  	}
75  
76  	public void setPercentageIncrement(int percentageIncrement) {
77  		this.percentageIncrement = percentageIncrement;
78  	}
79  
80  	public int getPercentCompletePrevious() {
81  		return percentCompletePrevious;
82  	}
83  
84  	public void setPercentCompletePrevious(int percentCompletePrevious) {
85  		this.percentCompletePrevious = percentCompletePrevious;
86  	}
87  
88  	public long getTotal() {
89  		return total;
90  	}
91  
92  	public void setTotal(long total) {
93  		this.total = total;
94  	}
95  }