Skip to content

Commit 17bafef

Browse files
committed
TextFormatter now uses static factory methods instead
1 parent b7bb292 commit 17bafef

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "com.darvil"
7-
version = "1.0.0"
7+
version = "2.0.0"
88
description = "Text formatting utilities to easily format text on the terminal for Java."
99

1010
dependencies {

src/main/java/textFormatter/TextFormatter.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,49 @@ public class TextFormatter {
4040
public static boolean debug = false;
4141

4242
/** A list of all the formatting options that should be applied to the text. */
43-
private final @NotNull ArrayList<FormatOption> formatOptions = new ArrayList<>();
43+
private final @NotNull ArrayList<FormatOption> formatOptions = new ArrayList<>(5);
4444

4545
/** A list of all the formatters that should be concatenated to this formatter. */
46-
private final @NotNull List<TextFormatter> concatList = new ArrayList<>();
46+
private final @NotNull List<TextFormatter> concatList = new ArrayList<>(0);
4747

4848
/** The parent formatter. Used when being concatenated to another formatter. */
4949
private @Nullable TextFormatter parent;
5050
private @Nullable Color foregroundColor;
5151
private @Nullable Color backgroundColor;
52-
private @NotNull String contents;
5352
private @Nullable String concatGap;
53+
private @NotNull String contents;
5454

5555
/**
5656
* Creates a new {@link TextFormatter} with the specified contents.
5757
* @param contents The contents of the formatter.
5858
*/
59-
public TextFormatter(@NotNull String contents) {
59+
protected TextFormatter(@NotNull String contents) {
6060
this.contents = contents;
6161
}
6262

63+
/**
64+
* Creates a new {@link TextFormatter} with the specified contents.
65+
* @param contents The contents of the formatter.
66+
*/
67+
public static TextFormatter of(@NotNull String contents) {
68+
return new TextFormatter(contents);
69+
}
70+
6371
/**
6472
* Creates a new {@link TextFormatter} with the specified contents and foreground color.
6573
* @param contents The contents of the formatter.
6674
* @param foreground The foreground color of the formatter.
6775
*/
68-
public TextFormatter(@NotNull String contents, @Nullable Color foreground) {
69-
this(contents);
70-
this.foregroundColor = foreground;
76+
public static TextFormatter of(@NotNull String contents, @NotNull Color foreground) {
77+
return TextFormatter.of(contents).withForegroundColor(foreground);
7178
}
7279

7380
/**
74-
* Creates a new {@link TextFormatter} with no contents.
81+
* Creates a new {@link TextFormatter} with empty contents.
82+
* @return a new {@link TextFormatter} with empty contents
7583
*/
76-
public TextFormatter() {
77-
this.contents = "";
84+
public static TextFormatter create() {
85+
return new TextFormatter("");
7886
}
7987

8088
/**
@@ -165,7 +173,7 @@ public TextFormatter concat(@NotNull TextFormatter... formatters) {
165173
*/
166174
public TextFormatter concat(@NotNull String... strings) {
167175
for (var str : strings) {
168-
this.concatList.add(new TextFormatter(str));
176+
this.concatList.add(TextFormatter.of(str));
169177
}
170178
return this;
171179
}
@@ -330,7 +338,7 @@ else if (TextFormatter.startWithDefaultColorIfNotDefined && this.parent == null)
330338

331339
/** Returns a template for a {@link TextFormatter} that is used for errors */
332340
public static @NotNull TextFormatter ERROR(@NotNull String msg) {
333-
return new TextFormatter(msg, Color.BRIGHT_RED).addFormat(FormatOption.REVERSE, FormatOption.BOLD);
341+
return TextFormatter.of(msg, Color.BRIGHT_RED).addFormat(FormatOption.REVERSE, FormatOption.BOLD);
334342
}
335343

336344
/**

src/test/java/TextFormatterTest.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ private static void check(@NotNull String expected, @NotNull String actual) {
1414

1515
@Test
1616
public void testSimple() {
17-
var formatter = new TextFormatter("Hello World").concat("!");
17+
var formatter = TextFormatter.of("Hello World").concat("!");
1818
check("Hello World!", formatter.toString());
1919
}
2020

2121
@Test
2222
public void testSubFormatters() {
23-
var formatter = new TextFormatter("Hello World")
23+
var formatter = TextFormatter.of("Hello World")
2424
.concat("! ")
2525
.concat(
26-
new TextFormatter("(Here is a sub-formatter")
27-
.concat(new TextFormatter(" (with another").concat(" sub-formatter)"))
26+
TextFormatter.of("(Here is a sub-formatter")
27+
.concat(TextFormatter.of(" (with another").concat(" sub-formatter)"))
2828
.concat(")")
2929
)
3030
.concat("!");
@@ -34,10 +34,10 @@ public void testSubFormatters() {
3434

3535
@Test
3636
public void testForegroundColoring() {
37-
var formatter = new TextFormatter("red text here, ", Color.RED)
37+
var formatter = TextFormatter.of("red text here, ", Color.RED)
3838
.concat(
39-
new TextFormatter("blue text here, ", Color.BLUE)
40-
.concat(new TextFormatter("now yellow", Color.BRIGHT_YELLOW))
39+
TextFormatter.of("blue text here, ", Color.BLUE)
40+
.concat(TextFormatter.of("now yellow", Color.BRIGHT_YELLOW))
4141
.concat(" and back to blue")
4242
)
4343
.concat(". back to red");
@@ -51,13 +51,13 @@ public void testForegroundColoring() {
5151

5252
@Test
5353
public void testBackgroundColoring() {
54-
var formatter = new TextFormatter("red background here, ")
54+
var formatter = TextFormatter.of("red background here, ")
5555
.withBackgroundColor(Color.RED)
5656
.concat(
57-
new TextFormatter("blue background here, ")
57+
TextFormatter.of("blue background here, ")
5858
.withBackgroundColor(Color.BLUE)
5959
.concat(
60-
new TextFormatter("now yellow")
60+
TextFormatter.of("now yellow")
6161
.withBackgroundColor(Color.BRIGHT_YELLOW)
6262
)
6363
.concat(" and back to blue")
@@ -74,9 +74,9 @@ public void testBackgroundColoring() {
7474

7575
@Test
7676
public void testMiddleBackground() {
77-
var formatter = new TextFormatter("yellow ", Color.BRIGHT_YELLOW)
77+
var formatter = TextFormatter.of("yellow ", Color.BRIGHT_YELLOW)
7878
.concat(
79-
new TextFormatter("blue ")
79+
TextFormatter.of("blue ")
8080
.withBackgroundColor(Color.BLUE)
8181
)
8282
.concat(" and back to yellow");
@@ -90,10 +90,10 @@ public void testMiddleBackground() {
9090

9191
@Test
9292
public void testEmpty() {
93-
var formatter = new TextFormatter("parent start. ", Color.RED)
93+
var formatter = TextFormatter.of("parent start. ", Color.RED)
9494
.concat(
95-
new TextFormatter("red text here, ")
96-
.concat(new TextFormatter().addFormat(FormatOption.ITALIC).concat("subsub"))
95+
TextFormatter.of("red text here, ")
96+
.concat(TextFormatter.create().addFormat(FormatOption.ITALIC).concat("subsub"))
9797
).concat(" end str");
9898

9999
check(
@@ -105,6 +105,6 @@ public void testEmpty() {
105105

106106
@Test
107107
public void testStartWithDefault() {
108-
check(Color.BRIGHT_WHITE + "test", new TextFormatter("test", Color.BRIGHT_WHITE).toString());
108+
check(Color.BRIGHT_WHITE + "test", TextFormatter.of("test", Color.BRIGHT_WHITE).toString());
109109
}
110-
}
110+
}

0 commit comments

Comments
 (0)