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 /**
019 * Print a dot to the console each time we make at least 1% progress towards the total
020 */
021 public class PercentCompleteInformer extends AbstractProgressInformer {
022
023 protected long progress;
024
025 int percentageIncrement = 1;
026 int percentCompletePrevious;
027 long total;
028
029 public PercentCompleteInformer() {
030 this(0);
031 }
032
033 public PercentCompleteInformer(long total) {
034 this(total, null);
035 }
036
037 public PercentCompleteInformer(long total, LogMsg startMessage) {
038 super();
039 this.total = total;
040 this.startMessage = startMessage;
041 }
042
043 /**
044 * Thread safe method for incrementing progress by one
045 */
046 public synchronized void incrementProgress() {
047 incrementProgress(1);
048 }
049
050 /**
051 * Thread safe method for incrementing progress by <code>amount</code>
052 */
053 public synchronized void incrementProgress(long amount) {
054 // Increment the progress indicator
055 this.progress += amount;
056
057 // Calculate how far along we are
058 int percentComplete = (int) ((progress * 100) / total);
059
060 // Print a dot to the console any time we make at least 1% progress
061 if (isEnoughProgress(percentComplete, percentCompletePrevious, percentageIncrement)) {
062 this.percentCompletePrevious = percentComplete;
063 printStream.print(progressToken);
064 }
065 }
066
067 protected boolean isEnoughProgress(int percentComplete, int percentCompletePrevious, int percentageIncrement) {
068 int needed = percentCompletePrevious + percentageIncrement;
069 return percentComplete >= needed;
070 }
071
072 public int getPercentageIncrement() {
073 return percentageIncrement;
074 }
075
076 public void setPercentageIncrement(int percentageIncrement) {
077 this.percentageIncrement = percentageIncrement;
078 }
079
080 public int getPercentCompletePrevious() {
081 return percentCompletePrevious;
082 }
083
084 public void setPercentCompletePrevious(int percentCompletePrevious) {
085 this.percentCompletePrevious = percentCompletePrevious;
086 }
087
088 public long getTotal() {
089 return total;
090 }
091
092 public void setTotal(long total) {
093 this.total = total;
094 }
095 }