From 7bbec29c0d6a2e2534cb8ca2847b3ecb50d0d651 Mon Sep 17 00:00:00 2001 From: Thomas Couchoud <1688389+Rakambda@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:23:25 +0200 Subject: [PATCH] Add unit factor --- .../progressbar/api/bar/IProgressBar.java | 16 ++++ .../progressbar/impl/bar/BaseProgressBar.java | 78 ++++++++++--------- .../impl/bar/ComposedProgressBar.java | 3 +- .../impl/bar/CumulativeProgressBar.java | 3 +- .../impl/bar/SimpleProgressBar.java | 3 +- .../impl/render/DefaultRenderer.java | 4 +- 6 files changed, 65 insertions(+), 42 deletions(-) diff --git a/src/main/java/fr/rakambda/progressbar/api/bar/IProgressBar.java b/src/main/java/fr/rakambda/progressbar/api/bar/IProgressBar.java index 25db5bb..9fa73da 100644 --- a/src/main/java/fr/rakambda/progressbar/api/bar/IProgressBar.java +++ b/src/main/java/fr/rakambda/progressbar/api/bar/IProgressBar.java @@ -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. * diff --git a/src/main/java/fr/rakambda/progressbar/impl/bar/BaseProgressBar.java b/src/main/java/fr/rakambda/progressbar/impl/bar/BaseProgressBar.java index 299eb51..fbbf953 100644 --- a/src/main/java/fr/rakambda/progressbar/impl/bar/BaseProgressBar.java +++ b/src/main/java/fr/rakambda/progressbar/impl/bar/BaseProgressBar.java @@ -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; + } } diff --git a/src/main/java/fr/rakambda/progressbar/impl/bar/ComposedProgressBar.java b/src/main/java/fr/rakambda/progressbar/impl/bar/ComposedProgressBar.java index b540fb2..3f1049f 100644 --- a/src/main/java/fr/rakambda/progressbar/impl/bar/ComposedProgressBar.java +++ b/src/main/java/fr/rakambda/progressbar/impl/bar/ComposedProgressBar.java @@ -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<>(); } diff --git a/src/main/java/fr/rakambda/progressbar/impl/bar/CumulativeProgressBar.java b/src/main/java/fr/rakambda/progressbar/impl/bar/CumulativeProgressBar.java index bf5137a..ec82600 100644 --- a/src/main/java/fr/rakambda/progressbar/impl/bar/CumulativeProgressBar.java +++ b/src/main/java/fr/rakambda/progressbar/impl/bar/CumulativeProgressBar.java @@ -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<>(); } diff --git a/src/main/java/fr/rakambda/progressbar/impl/bar/SimpleProgressBar.java b/src/main/java/fr/rakambda/progressbar/impl/bar/SimpleProgressBar.java index f3f3592..08dd203 100644 --- a/src/main/java/fr/rakambda/progressbar/impl/bar/SimpleProgressBar.java +++ b/src/main/java/fr/rakambda/progressbar/impl/bar/SimpleProgressBar.java @@ -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()); diff --git a/src/main/java/fr/rakambda/progressbar/impl/render/DefaultRenderer.java b/src/main/java/fr/rakambda/progressbar/impl/render/DefaultRenderer.java index a24cbfd..2a857bd 100644 --- a/src/main/java/fr/rakambda/progressbar/impl/render/DefaultRenderer.java +++ b/src/main/java/fr/rakambda/progressbar/impl/render/DefaultRenderer.java @@ -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()); }