From bcf7010fbbf0308020819376ea48c635ac208b4f Mon Sep 17 00:00:00 2001 From: George Tay Date: Sun, 12 May 2024 19:53:34 +0800 Subject: [PATCH] Refactor BlurbMap --- src/main/java/reposense/model/BlurbMap.java | 45 +++++-------------- .../reposense/parser/BlurbMarkdownParser.java | 8 ++-- .../java/reposense/model/BlurbMapTest.java | 16 +++---- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/src/main/java/reposense/model/BlurbMap.java b/src/main/java/reposense/model/BlurbMap.java index e61d64eaee..478a3d0c1f 100644 --- a/src/main/java/reposense/model/BlurbMap.java +++ b/src/main/java/reposense/model/BlurbMap.java @@ -20,6 +20,16 @@ public Map getAllMappings() { return new HashMap<>(this.urlBlurbMap); } + /** + * Adds a key-value record into the {@code BlurbMap}. + * + * @param key Key value. + * @param value Blurb value. + */ + public void withRecord(String key, String value) { + this.urlBlurbMap.put(key, value); + } + @Override public boolean equals(Object obj) { if (obj == this) { @@ -33,39 +43,4 @@ public boolean equals(Object obj) { return false; } - - /** - * Builder class to help in the building of a {@code BlurbMap}. - */ - public static final class Builder { - private BlurbMap blurbMap; - - public Builder() { - blurbMap = new BlurbMap(); - } - - - /** - * Adds a key-value record into the {@code BlurbMap}. - * - * @param key Key value. - * @param value Blurb value. - * @return This {@code Builder}. - */ - public Builder withRecord(String key, String value) { - blurbMap.urlBlurbMap.put(key, value); - return this; - } - - /** - * Returns a built instance of {@code BlurbMap}. - * - * @return Built {@code BlurbMap}. - */ - public BlurbMap build() { - BlurbMap built = this.blurbMap; - this.blurbMap = new BlurbMap(); - return built; - } - } } diff --git a/src/main/java/reposense/parser/BlurbMarkdownParser.java b/src/main/java/reposense/parser/BlurbMarkdownParser.java index 9f86bfd66c..8c3e5a0f67 100644 --- a/src/main/java/reposense/parser/BlurbMarkdownParser.java +++ b/src/main/java/reposense/parser/BlurbMarkdownParser.java @@ -81,8 +81,8 @@ public BlurbMap parse() throws IOException, InvalidMarkdownException { throw new InvalidMarkdownException("Empty blurbs.md file"); } - // prepare the blurb builder - BlurbMap.Builder builder = new BlurbMap.Builder(); + // prepare the blurb map + BlurbMap blurbMap = new BlurbMap(); // define temporary local variables to track blurbs String url = ""; @@ -107,13 +107,13 @@ public BlurbMap parse() throws IOException, InvalidMarkdownException { // add the recorded entry into the BlurbMap // strip the trailing /n - builder = builder.withRecord(url, blurb.toString().stripTrailing()); + blurbMap.withRecord(url, blurb.toString().stripTrailing()); blurb.setLength(0); } // return the built BlurbMap instance logger.log(Level.INFO, "Blurbs parsed successfully!"); - return builder.build(); + return blurbMap; } private UrlRecord getUrlRecord(List lines, int position) throws InvalidMarkdownException { diff --git a/src/test/java/reposense/model/BlurbMapTest.java b/src/test/java/reposense/model/BlurbMapTest.java index 9f49cc1726..463d0611aa 100644 --- a/src/test/java/reposense/model/BlurbMapTest.java +++ b/src/test/java/reposense/model/BlurbMapTest.java @@ -6,28 +6,28 @@ public class BlurbMapTest { @Test public void blurbBuilder_testIfBuildsSuccessfully_success() { - BlurbMap.Builder builder = new BlurbMap.Builder(); + BlurbMap builder = new BlurbMap(); builder.withRecord("hello", "world"); - BlurbMap.Builder newBuilder = new BlurbMap.Builder(); + BlurbMap newBuilder = new BlurbMap(); newBuilder.withRecord("hello", "world"); - Assertions.assertEquals(builder.build(), newBuilder.build()); + Assertions.assertEquals(builder, newBuilder); } @Test public void blurbBuilder_testIfBuildsEmpty_success() { - BlurbMap map1 = new BlurbMap.Builder().build(); - BlurbMap map2 = new BlurbMap.Builder().build(); + BlurbMap map1 = new BlurbMap(); + BlurbMap map2 = new BlurbMap(); Assertions.assertEquals(map1, map2); } @Test public void blurbBuilder_testIfUnequal_success() { - BlurbMap.Builder builder1 = new BlurbMap.Builder(); - BlurbMap.Builder builder2 = new BlurbMap.Builder(); + BlurbMap builder1 = new BlurbMap(); + BlurbMap builder2 = new BlurbMap(); builder1.withRecord("this", "builder"); builder2.withRecord("other", "builder"); - Assertions.assertNotEquals(builder1.build(), builder2.build()); + Assertions.assertNotEquals(builder1, builder2); } }