001package arcommon.progress; 002 003import arcommon.MathUtil; 004import arcommon.StringUtil; 005 006public class ProgressState { 007 008 private String name; 009 private long maximum; 010 private long current; 011 private boolean useUnits; 012 private ProgressThreadData progressThreadData; 013 private int notifyCounter; 014 015 public ProgressState(ProgressThreadData pprogressThreadData) { 016 progressThreadData = pprogressThreadData; 017 } 018 019 public long getCurrent() { 020 return current; 021 } 022 023 public void incCurrent() { 024 if (current == Long.MAX_VALUE) { 025 throw new RuntimeException("current == Long.MAX_VALUE " + current); 026 } 027 setCurrent(current + 1); 028 } 029 030 public void setCurrent(long pcurrent) { 031 if (pcurrent < 0) { 032 throw new IllegalArgumentException("current < 0 " + pcurrent); 033 } 034 if (maximum > 0 && pcurrent > maximum) { 035 throw new IllegalArgumentException("maximum > 0 && pcurrent > maximum " + pcurrent + " > " + maximum); 036 } 037 current = pcurrent; 038 if (progressThreadData == null) { 039 return ; 040 } 041 042 progressThreadData.checkIfThereShouldBeNotified(); 043 } 044 045 /*package*/ int getNotifyCounter() { 046 return notifyCounter; 047 } 048 049 /*package*/ void incNotifyCounter() { 050 notifyCounter++; 051 } 052 053 /*package*/ void resetNotifyCounter() { 054 notifyCounter = 0; 055 } 056 057 public long getMaximum() { 058 return maximum; 059 } 060 061 public void setMaximum(long pmaximum) { 062 if (pmaximum < 0) { 063 throw new IllegalArgumentException("maximum < 0 " + pmaximum); 064 } 065 maximum = pmaximum; 066 } 067 068 public String getName() { 069 return name; 070 } 071 072 public void setName(String pname) { 073 name = pname; 074 } 075 076 public boolean getUseUnits() { 077 return useUnits; 078 } 079 080 public void setUseUnits(boolean puseUnits) { 081 useUnits = puseUnits; 082 } 083 084 public String toString() { 085 String result = ""; 086 String extra = ""; 087 088 if (current > 0 || maximum > 0) { 089 int digits = MathUtil.log10(maximum); 090 if (maximum == 0 && digits == 0) { 091 digits = 1; 092 } 093 if (useUnits) { 094 result += StringUtil.stringWithFixLength(StringUtil.longToStringWithUnit(current), 6, ' '); 095 extra = " (" + StringUtil.longToStringWithFixLengthWithSpace(current, digits); 096 } else { 097 result += StringUtil.longToStringWithFixLengthWithSpace(current, digits); 098 } 099 } 100 101 if (maximum > 0) { 102 result = result + " / "; 103 if (useUnits) { 104 result += StringUtil.longToStringWithUnit(maximum); 105 extra += " / " + maximum + ")"; 106 } else { 107 result += maximum; 108 } 109 } else { 110 if (useUnits) { 111 extra += ")"; 112 } 113 } 114 115 result += extra; 116 117 if (name != null && name.length() != 0) { 118 return name + " " + result; 119 } 120 return result; 121 } 122}