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  import java.io.PrintStream;
19  
20  /**
21   * Print a dot to the console each time we make 1% progress towards the total count
22   */
23  public class PercentCompleteInformer {
24  
25  	PrintStream out = System.out;
26  	long count = 0;
27  	long total = 0;
28  	int percentageIncrement = 1;
29  	int percentCompletePrevious;
30  	String startToken = "[INFO] Progress: ";
31  	String progressToken = ".";
32  	String completeToken = "\n";
33  
34  	public void setTotal(long total) {
35  		this.total = total;
36  	}
37  
38  	public synchronized void progressOccurred() {
39  		// The first SQL statement was just executed
40  		if (count == 0) {
41  			out.print(startToken);
42  		}
43  
44  		// Increment the counter
45  		this.count++;
46  
47  		// Print a dot anytime we make 1% progress
48  		int percentComplete = (int) ((count * 100) / total);
49  		if (enoughProgress(percentComplete)) {
50  			this.percentCompletePrevious = percentComplete;
51  			out.print(progressToken);
52  		}
53  
54  		// The last SQL statement was just executed
55  		if (count == total) {
56  			out.print(completeToken);
57  		}
58  	}
59  
60  	protected boolean enoughProgress(int percentComplete) {
61  		int needed = percentCompletePrevious + percentageIncrement;
62  		return percentComplete >= needed;
63  	}
64  
65  	public PrintStream getOut() {
66  		return out;
67  	}
68  
69  	public void setOut(PrintStream out) {
70  		this.out = out;
71  	}
72  
73  	public long getCount() {
74  		return count;
75  	}
76  
77  	public void setCount(long count) {
78  		this.count = count;
79  	}
80  
81  	public int getPercentageIncrement() {
82  		return percentageIncrement;
83  	}
84  
85  	public void setPercentageIncrement(int percentageIncrement) {
86  		this.percentageIncrement = percentageIncrement;
87  	}
88  
89  	public String getStartToken() {
90  		return startToken;
91  	}
92  
93  	public void setStartToken(String startToken) {
94  		this.startToken = startToken;
95  	}
96  
97  	public String getCompleteToken() {
98  		return completeToken;
99  	}
100 
101 	public void setCompleteToken(String completeToken) {
102 		this.completeToken = completeToken;
103 	}
104 
105 	public String getProgressToken() {
106 		return progressToken;
107 	}
108 
109 	public void setProgressToken(String progressToken) {
110 		this.progressToken = progressToken;
111 	}
112 
113 	public long getTotal() {
114 		return total;
115 	}
116 
117 }