Skip to content

Commit

Permalink
Add unit factor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Sep 21, 2024
1 parent 006505a commit 7bbec29
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 42 deletions.
16 changes: 16 additions & 0 deletions src/main/java/fr/rakambda/progressbar/api/bar/IProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ default boolean isFinished(){
*/
void setUnit(String value);

/**
* Gets the unit factor associated to this bar.
* For example, if the bar has a value of 100 and a unit factor of 20, then 5 will be displayed.
*
* @return The unit factor.
*/
long getUnitFactor();

/**
* Sets the unit factor associated to this bar.
* For example, if the bar has a value of 100 and a unit factor of 20, then 5 will be displayed.
*
* @param value The unit factor.
*/
void setUnitFactor(long value);

/**
* Gets the description associated to this bar.
*
Expand Down
78 changes: 41 additions & 37 deletions src/main/java/fr/rakambda/progressbar/impl/bar/BaseProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
* Ease of use class containing all the "metadata" fields of a {@link fr.rakambda.progressbar.api.bar.IProgressBar}.
*/
@Getter
abstract class BaseProgressBar {
@NotNull
private IRenderer renderer;
@Nullable
@Setter
private String name;
@Setter
private boolean hideWhenComplete;
@Setter
private boolean removeWhenComplete;
@Setter
private boolean showPercentage;
@Nullable
@Setter
private String unit;
@Nullable
@Setter
private String description;

public BaseProgressBar(
@Nullable IRenderer renderer,
@Nullable String name,
boolean hideWhenComplete,
boolean removeWhenComplete,
boolean showPercentage,
@Nullable String unit,
@Nullable String description
) {
this.renderer = Optional.ofNullable(renderer).orElse(new DefaultRenderer());
this.name = name;
this.hideWhenComplete = hideWhenComplete;
this.removeWhenComplete = removeWhenComplete;
this.showPercentage = showPercentage;
this.unit = unit;
this.description = description;
}
abstract class BaseProgressBar{
@NotNull
private IRenderer renderer;
@Nullable
@Setter
private String name;
@Setter
private boolean hideWhenComplete;
@Setter
private boolean removeWhenComplete;
@Setter
private boolean showPercentage;
@Nullable
@Setter
private String unit;
@Setter
@Getter
private long unitFactor;
@Nullable
@Setter
private String description;

public BaseProgressBar(
@Nullable IRenderer renderer,
@Nullable String name,
boolean hideWhenComplete,
boolean removeWhenComplete,
boolean showPercentage,
@Nullable String unit,
@Nullable Long unitFactor,
@Nullable String description
){
this.renderer = Optional.ofNullable(renderer).orElse(new DefaultRenderer());
this.name = name;
this.hideWhenComplete = hideWhenComplete;
this.removeWhenComplete = removeWhenComplete;
this.showPercentage = showPercentage;
this.unit = unit;
this.unitFactor = Optional.ofNullable(unitFactor).filter(v -> v != 0L).orElse(1L);
this.description = description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ private ComposedProgressBar(
boolean removeWhenComplete,
boolean showPercentage,
@Nullable String unit,
@Nullable Long unitFactor,
@Nullable String description
){
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, description);
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, unitFactor, description);

this.children = new ConcurrentLinkedQueue<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ private CumulativeProgressBar(
boolean removeWhenComplete,
boolean showPercentage,
@Nullable String unit,
@Nullable Long unitFactor,
@Nullable String description
){
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, description);
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, unitFactor, description);

this.children = new ConcurrentLinkedQueue<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ private SimpleProgressBar(
boolean removeWhenComplete,
boolean showPercentage,
@Nullable String unit,
@Nullable Long unitFactor,
@Nullable String description
){
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, description);
super(renderer, name, hideWhenComplete, removeWhenComplete, showPercentage, unit, unitFactor, description);

start = Optional.ofNullable(start).orElse(new AtomicLong());
current = Optional.ofNullable(current).orElse(new AtomicLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public String render(int maxLength, @NotNull IProgressBar bar){
}

suffix.append(' ');
suffix.append(elapsed);
suffix.append(elapsed / bar.getUnitFactor());
suffix.append('/');
suffix.append(totalSteps);
suffix.append(totalSteps / bar.getUnitFactor());
if(Objects.nonNull(bar.getUnit())){
suffix.append(bar.getUnit());
}
Expand Down

0 comments on commit 7bbec29

Please sign in to comment.