Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.eternalcode.commons.progressbar;

public class ProgressBar {

private final String filledToken;

private final String emptyToken;

private final boolean showBrackets;
private final String leftBracket;
private final String rightBracket;
private final String bracketColor;

private final int length;

private ProgressBar(Builder builder) {
this.leftBracket = builder.leftBracket;
this.rightBracket = builder.rightBracket;
this.bracketColor = builder.bracketColor;
this.length = builder.length;
this.showBrackets = builder.showBrackets;

this.filledToken = builder.filledColor + builder.filledChar;
this.emptyToken = builder.emptyColor + builder.emptyChar;
}

public String render(double progress) {
double clampedProgress = Math.max(0, Math.min(1, progress));
int filled = (int) (this.length * clampedProgress);

StringBuilder bar = new StringBuilder();

if (this.showBrackets) {
bar.append(this.bracketColor).append(this.leftBracket);
}

for (int i = 0; i < this.length; i++) {
if (i < filled) {
bar.append(this.filledToken);
}
else {
bar.append(this.emptyToken);
}
}

if (this.showBrackets) {
bar.append(this.bracketColor).append(this.rightBracket);
}

return bar.toString();
}

public String render(int current, int max) {
if (max <= 0) {
return this.render(1.0);
}
double progress = (double) current / max;
return this.render(progress);
}

public String render(long current, long max) {
if (max <= 0) {
return this.render(1.0);
}
double progress = (double) current / max;
return this.render(progress);
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
private String filledChar = "█";
private String emptyChar = "░";
private String filledColor = "";
private String emptyColor = "";
private String leftBracket = "[";
private String rightBracket = "]";
private String bracketColor = "";
private int length = 10;
private boolean showBrackets = true;

public Builder filledChar(String filledChar) {
this.filledChar = filledChar;
return this;
}

public Builder emptyChar(String emptyChar) {
this.emptyChar = emptyChar;
return this;
}

public Builder filledColor(String filledColor) {
this.filledColor = filledColor;
return this;
}

public Builder emptyColor(String emptyColor) {
this.emptyColor = emptyColor;
return this;
}

public Builder leftBracket(String leftBracket) {
this.leftBracket = leftBracket;
return this;
}

public Builder rightBracket(String rightBracket) {
this.rightBracket = rightBracket;
return this;
}

public Builder brackets(String left, String right) {
this.leftBracket = left;
this.rightBracket = right;
return this;
}

public Builder bracketColor(String bracketColor) {
this.bracketColor = bracketColor;
return this;
}

public Builder length(int length) {
if (length <= 0) {
throw new IllegalArgumentException("Length must be positive");
}
this.length = length;
return this;
}

public Builder showBrackets(boolean showBrackets) {
this.showBrackets = showBrackets;
return this;
}

public Builder hideBrackets() {
this.showBrackets = false;
return this;
}

public ProgressBar build() {
return new ProgressBar(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eternalcode.commons.time;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class SimpleDurationUtil {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this is a utility class with only static members, it's a good practice to add a private constructor to prevent it from being instantiated.

Suggested change
private SimpleDurationUtil() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

private static final Duration ONE_SECOND = Duration.ofSeconds(1);
private static final String ZERO_SECONDS = "0s";

private static final TemporalAmountParser<Duration> WITHOUT_MILLIS_FORMAT = new DurationParser()
.withUnit("s", ChronoUnit.SECONDS)
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS)
.withUnit("d", ChronoUnit.DAYS)
.withUnit("w", ChronoUnit.WEEKS)
.withUnit("mo", ChronoUnit.MONTHS)
.withUnit("y", ChronoUnit.YEARS)
.roundOff(ChronoUnit.MILLIS);

private static final TemporalAmountParser<Duration> STANDARD_FORMAT = new DurationParser()
.withUnit("ms", ChronoUnit.MILLIS)
.withUnit("s", ChronoUnit.SECONDS)
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS)
.withUnit("d", ChronoUnit.DAYS)
.withUnit("w", ChronoUnit.WEEKS)
.withUnit("mo", ChronoUnit.MONTHS)
.withUnit("y", ChronoUnit.YEARS);

public static String format(Duration duration, boolean removeMillis) {
if (removeMillis) {
if (duration.toMillis() < ONE_SECOND.toMillis()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For comparing Duration objects, it's more idiomatic and readable to use the compareTo method instead of comparing their millisecond values.

Suggested change
if (duration.toMillis() < ONE_SECOND.toMillis()) {
if (duration.compareTo(ONE_SECOND) < 0) {

return ZERO_SECONDS;
}

return WITHOUT_MILLIS_FORMAT.format(duration);
}

return STANDARD_FORMAT.format(duration);
}

public static String format(Duration duration) {
return format(duration, false);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.eternalcode.commons.progressbar;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ProgressBarTest {

@Test
void testRenderFullProgress() {
ProgressBar bar = ProgressBar.builder()
.length(5)
.build();
String rendered = bar.render(1.0);

assertTrue(rendered.contains("█"));
assertFalse(rendered.contains("░"));
assertEquals(7, rendered.length());
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The assertions in this test can be more specific. Instead of checking for character containment and length separately, you can assert the exact expected string. This makes the test more robust and clearly states the expected outcome.

Suggested change
assertTrue(rendered.contains("█"));
assertFalse(rendered.contains("░"));
assertEquals(7, rendered.length());
assertEquals("[█████]", rendered);

}

@Test
void testRenderEmptyProgress() {
ProgressBar bar = ProgressBar.builder()
.length(5)
.build();
String rendered = bar.render(0.0);

assertTrue(rendered.contains("░"));
assertFalse(rendered.contains("█"));
assertEquals(7, rendered.length());
Comment on lines +28 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make this test more robust and clear, consider asserting against the exact expected string instead of checking for character containment and length.

Suggested change
assertTrue(rendered.contains("░"));
assertFalse(rendered.contains("█"));
assertEquals(7, rendered.length());
assertEquals("[░░░░░]", rendered);

}

@Test
void testRenderHalfProgress() {
ProgressBar bar = ProgressBar.builder()
.length(4)
.build();
String rendered = bar.render(0.5);

assertTrue(rendered.contains("█"));
assertTrue(rendered.contains("░"));
assertEquals(6, rendered.length());
Comment on lines +40 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The assertions here can be made more specific by checking for the exact expected string. This improves the test's clarity and robustness.

Suggested change
assertTrue(rendered.contains("█"));
assertTrue(rendered.contains("░"));
assertEquals(6, rendered.length());
assertEquals("[██░░]", rendered);

}

@Test
void testRenderIntOverMax() {
ProgressBar bar = ProgressBar.builder()
.length(3)
.build();
String rendered = bar.render(5, 3);

assertEquals("[███]", rendered);
}

@Test
void testRenderIntWithZeroMax() {
ProgressBar bar = ProgressBar.builder()
.length(3)
.build();
String rendered = bar.render(0, 0);

assertEquals("[███]", rendered);
}

@Test
void testHideBrackets() {
ProgressBar bar = ProgressBar.builder()
.length(3)
.hideBrackets()
.build();
String rendered = bar.render(1.0);

assertEquals("███", rendered);
}

@Test
void testCustomCharacters() {
ProgressBar bar = ProgressBar.builder()
.length(4)
.filledChar("#")
.emptyChar("-")
.brackets("{", "}")
.build();

String rendered = bar.render(0.5);
assertEquals("{##--}", rendered);
}

@Test
void testNegativeProgressClampedToZero() {
ProgressBar bar = ProgressBar.builder()
.length(3)
.build();
String rendered = bar.render(-1.0);

assertEquals("[░░░]", rendered);
}

@Test
void testProgressGreaterThanOneClampedToOne() {
ProgressBar bar = ProgressBar.builder()
.length(3)
.build();
String rendered = bar.render(2.0);

assertEquals("[███]", rendered);
}

@Test
void testInvalidLengthThrowsException() {
assertThrows(IllegalArgumentException.class, () -> ProgressBar.builder().length(0));
assertThrows(IllegalArgumentException.class, () -> ProgressBar.builder().length(-5));
}
}
Loading
Loading