Skip to content

Commit

Permalink
Refactor BlurbMap
Browse files Browse the repository at this point in the history
  • Loading branch information
georgetayqy committed May 12, 2024
1 parent 3f979da commit bcf7010
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
45 changes: 10 additions & 35 deletions src/main/java/reposense/model/BlurbMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public Map<String, String> 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) {
Expand All @@ -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;
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/reposense/parser/BlurbMarkdownParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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<String> lines, int position) throws InvalidMarkdownException {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/reposense/model/BlurbMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit bcf7010

Please sign in to comment.