001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util;
017
018 import java.io.PrintStream;
019
020 /**
021 * Print a dot to the console each time we make at least 1% progress towards the total
022 */
023 public class PercentCompleteInformer {
024
025 protected long progress;
026
027 PrintStream printStream = System.out;
028 int percentageIncrement = 1;
029 int percentCompletePrevious;
030 String startToken = "[INFO] Progress: ";
031 String progressToken = ".";
032 String completeToken = "\n";
033 long total;
034
035 public PercentCompleteInformer() {
036 this(0);
037 }
038
039 public PercentCompleteInformer(long total) {
040 super();
041 this.total = total;
042 }
043
044 /**
045 * Thread safe method exposing the current progress
046 */
047 public synchronized long getProgress() {
048 return progress;
049 }
050
051 /**
052 * Thread safe method for incrementing progress by one
053 */
054 public synchronized void incrementProgress() {
055 incrementProgress(1);
056 }
057
058 /**
059 * Thread safe method for incrementing progress by <code>amount</code>
060 */
061 public synchronized void incrementProgress(long amount) {
062 // Increment the progress indicator
063 this.progress += amount;
064
065 // Calculate how far along we are
066 int percentComplete = (int) ((progress * 100) / total);
067
068 // Print a dot to the console if any time we make at least 1% progress
069 if (isEnoughProgress(percentComplete, percentCompletePrevious, percentageIncrement)) {
070 this.percentCompletePrevious = percentComplete;
071 printStream.print(progressToken);
072 }
073 }
074
075 protected boolean isEnoughProgress(int percentComplete, int percentCompletePrevious, int percentageIncrement) {
076 int needed = percentCompletePrevious + percentageIncrement;
077 return percentComplete >= needed;
078 }
079
080 /**
081 * Print the start token
082 */
083 public void start() {
084
085 Assert.notNull(printStream, "printStream is null");
086
087 printStream.print(startToken);
088 }
089
090 /**
091 * Print the stop token
092 */
093 public void stop() {
094 printStream.print(completeToken);
095 }
096
097 public PrintStream getPrintStream() {
098 return printStream;
099 }
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 }