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 at least 1% progress towards the total
22   */
23  public class PercentCompleteInformer {
24  
25  	protected long progress;
26  
27  	PrintStream printStream = System.out;
28  	int percentageIncrement = 1;
29  	int percentCompletePrevious;
30  	String startToken = "[INFO] Progress: ";
31  	String progressToken = ".";
32  	String completeToken = "\n";
33  	long total;
34  
35  	public PercentCompleteInformer() {
36  		this(0);
37  	}
38  
39  	public PercentCompleteInformer(long total) {
40  		super();
41  		this.total = total;
42  	}
43  
44  	/**
45  	 * Thread safe method exposing the current progress
46  	 */
47  	public synchronized long getProgress() {
48  		return progress;
49  	}
50  
51  	/**
52  	 * Thread safe method for incrementing progress by one
53  	 */
54  	public synchronized void incrementProgress() {
55  		incrementProgress(1);
56  	}
57  
58  	/**
59  	 * Thread safe method for incrementing progress by <code>amount</code>
60  	 */
61  	public synchronized void incrementProgress(long amount) {
62  		// Increment the progress indicator
63  		this.progress += amount;
64  
65  		// Calculate how far along we are
66  		int percentComplete = (int) ((progress * 100) / total);
67  
68  		// Print a dot to the console any time we make at least 1% progress
69  		if (isEnoughProgress(percentComplete, percentCompletePrevious, percentageIncrement)) {
70  			this.percentCompletePrevious = percentComplete;
71  			printStream.print(progressToken);
72  		}
73  	}
74  
75  	protected boolean isEnoughProgress(int percentComplete, int percentCompletePrevious, int percentageIncrement) {
76  		int needed = percentCompletePrevious + percentageIncrement;
77  		return percentComplete >= needed;
78  	}
79  
80  	/**
81  	 * Print the start token
82  	 */
83  	public void start() {
84  
85  		Assert.notNull(printStream, "printStream is null");
86  
87  		printStream.print(startToken);
88  	}
89  
90  	/**
91  	 * Print the stop token
92  	 */
93  	public void stop() {
94  		printStream.print(completeToken);
95  	}
96  
97  	public PrintStream getPrintStream() {
98  		return printStream;
99  	}
100 
101 	public void setPrintStream(PrintStream printStream) {
102 		this.printStream = printStream;
103 	}
104 
105 	public int getPercentageIncrement() {
106 		return percentageIncrement;
107 	}
108 
109 	public void setPercentageIncrement(int percentageIncrement) {
110 		this.percentageIncrement = percentageIncrement;
111 	}
112 
113 	public String getStartToken() {
114 		return startToken;
115 	}
116 
117 	public void setStartToken(String startToken) {
118 		this.startToken = startToken;
119 	}
120 
121 	public String getCompleteToken() {
122 		return completeToken;
123 	}
124 
125 	public void setCompleteToken(String completeToken) {
126 		this.completeToken = completeToken;
127 	}
128 
129 	public String getProgressToken() {
130 		return progressToken;
131 	}
132 
133 	public void setProgressToken(String progressToken) {
134 		this.progressToken = progressToken;
135 	}
136 
137 	public long getTotal() {
138 		return total;
139 	}
140 
141 	public void setTotal(long total) {
142 		this.total = total;
143 	}
144 
145 }