1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.PrintStream;
19
20
21
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
46
47 public synchronized long getProgress() {
48 return progress;
49 }
50
51
52
53
54 public synchronized void incrementProgress() {
55 incrementProgress(1);
56 }
57
58
59
60
61 public synchronized void incrementProgress(long amount) {
62
63 this.progress += amount;
64
65
66 int percentComplete = (int) ((progress * 100) / total);
67
68
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
82
83 public void start() {
84
85 Assert.notNull(printStream, "printStream is null");
86
87 printStream.print(startToken);
88 }
89
90
91
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 }