Skip to content

Commit

Permalink
Refactor article Title as Class
Browse files Browse the repository at this point in the history
  • Loading branch information
H4mes committed Mar 30, 2024
1 parent c3e43e2 commit cb20293
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.article.Title;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -91,7 +92,7 @@ public CommandResult execute(Model model) throws CommandException {
private static Article createEditedArticle(Article articleToEdit, EditArticleDescriptor editArticleDescriptor) {
assert articleToEdit != null;

String title = editArticleDescriptor.getTitle().orElse(articleToEdit.getTitle());
Title title = editArticleDescriptor.getTitle().orElse(articleToEdit.getTitle());
Set<Author> authors = editArticleDescriptor.getAuthors().orElse(articleToEdit.getAuthors());
Set<Source> sources = editArticleDescriptor.getSources().orElse(articleToEdit.getSources());
Set<Tag> tags = editArticleDescriptor.getTags().orElse(articleToEdit.getTags());
Expand Down Expand Up @@ -134,7 +135,7 @@ public String toString() {
*/
public static class EditArticleDescriptor {

private String title;
private Title title;
private Set<Author> authors;
private Set<Source> sources;
private Set<Tag> tags;
Expand Down Expand Up @@ -164,11 +165,11 @@ public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(title, authors, sources, outlets, publicationDate, tags, status);
}

public void setTitle(String title) {
public void setTitle(Title title) {
this.title = title;
}

public Optional<String> getTitle() {
public Optional<Title> getTitle() {
return Optional.ofNullable(title);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.article.Title;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -41,7 +42,7 @@ public AddArticleCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddArticleCommand.MESSAGE_USAGE));
}

String title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
Set<Author> authorList = ParserUtil.parseAuthors(argMultimap.getAllValues(PREFIX_AUTHOR));
Set<Source> sourceList = ParserUtil.parseSources(argMultimap.getAllValues(PREFIX_SOURCE));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_ARTICLETAG));
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.article.Title;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -136,11 +137,13 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
/**
* Parses a {@code String title} into a {@code Title}.
*/
public static String parseTitle(String title) throws ParseException {
public static Title parseTitle(String title) throws ParseException {
requireNonNull(title);
String trimmedTitle = title.trim();
//removed the check for title validity
return trimmedTitle;
if (!Title.isValidTitle(trimmedTitle)) {
throw new ParseException(Title.MESSAGE_CONSTRAINTS);
}
return new Title(trimmedTitle);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/model/article/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Represents an article in the address book.
*/
public class Article {
private final String title;
private final Title title;
private final Set<Outlet> outlets = new HashSet<>();
private final Set<Author> authors = new HashSet<>();
private final Set<Source> sources = new HashSet<>();
Expand All @@ -42,7 +42,7 @@ public enum Status {
* @param tags the subject of the article.
* @param status the current status of the article.
*/
public Article(String title, Set<Author> authors, Set<Source> sources, Set<Tag> tags,
public Article(Title title, Set<Author> authors, Set<Source> sources, Set<Tag> tags,
Set<Outlet> outlets, LocalDateTime publicationDate, Status status) {
requireAllNonNull(title, authors, sources, tags, outlets, publicationDate, status);
this.title = title;
Expand All @@ -54,8 +54,8 @@ public Article(String title, Set<Author> authors, Set<Source> sources, Set<Tag>
this.status = status;
}

public String getTitle() {
return this.title;
public Title getTitle() {
return title;
}

public Set<Author> getAuthors() {
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/seedu/address/model/article/Title.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package seedu.address.model.article;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Article's title in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidTitle(String)}
*/
public class Title implements Comparable<Title> {
public static final String MESSAGE_CONSTRAINTS =
"Titles should should not be blank or start with a whitespace character.";

public static final String VALIDATION_REGEX = "^(?!\\s).*";

public final String fullTitle;

/**
* Constructs a {@code Title}.
*
* @param title A valid title.
*/
public Title(String title) {
requireNonNull(title);
checkArgument(isValidTitle(title), MESSAGE_CONSTRAINTS);
fullTitle = title;
}

/**
* Returns true if a given string is a valid title.
*/
public static boolean isValidTitle(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return fullTitle;
}

@Override
public int compareTo(Title other) {
return this.fullTitle.compareTo(other.fullTitle);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof Title)) {
return false;
}

Title otherTitle = (Title) other;
return otherTitle.fullTitle.equals(this.fullTitle);
}

@Override
public int hashCode() {
return fullTitle.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public TitleContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Article article) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(article.getTitle(), keyword));
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(article.getTitle().fullTitle, keyword));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.article.Title;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -21,7 +22,8 @@ public class SampleArticleDataUtil {

public static Article[] getSampleArticles() {
return new Article[]{
new Article("The epitome of pain and suffering by NUS CS students.", getAuthorSet("Alice", "Bob"),
new Article(new Title("The epitome of pain and suffering by NUS CS students."),
getAuthorSet("Alice", "Bob"),
getSourceSet("NUS Computing Club"), getTagSet("Student Life"), getOutletSet("SOC News Bulletin"),
LocalDateTime.now(), Status.PUBLISHED)
};
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedArticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.model.article.Author;
import seedu.address.model.article.Outlet;
import seedu.address.model.article.Source;
import seedu.address.model.article.Title;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -70,7 +71,7 @@ public JsonAdaptedArticle(@JsonProperty("title") String title,
* @param sourceArticle
*/
public JsonAdaptedArticle(Article sourceArticle) {
title = sourceArticle.getTitle();
title = sourceArticle.getTitle().fullTitle;
authors.addAll(sourceArticle.getAuthors().stream()
.map(JsonAdaptedAuthor::new)
.collect(Collectors.toList()));
Expand All @@ -96,6 +97,7 @@ public Article toModelType() throws IllegalValueException {
if (title == null) {
throw new IllegalValueException("The title is missing");
}
final Title modelTitle = new Title(title);
if (status == null) {
throw new IllegalValueException("The status is missing");
}
Expand Down Expand Up @@ -125,6 +127,6 @@ public Article toModelType() throws IllegalValueException {

final Set<Outlet> modelOutlets = new HashSet<>(articleOutlets);

return new Article(title, modelAuthors, modelSources, modelTags, modelOutlets, publicationDate, status);
return new Article(modelTitle, modelAuthors, modelSources, modelTags, modelOutlets, publicationDate, status);
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/ArticleCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ArticleCard(Article article, int displayedIndex) {
super(FXML);
this.article = article;
id.setText(displayedIndex + ". ");
title.setText(article.getTitle());
title.setText(article.getTitle().fullTitle);

article.getAuthors().stream()
.sorted(Comparator.comparing(author -> author.authorName))
Expand Down

0 comments on commit cb20293

Please sign in to comment.