Skip to content

Commit

Permalink
Fix code quality and follow design principles
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyyyming committed Nov 14, 2023
1 parent f13f0a1 commit 619ee52
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static Instrument parseInstrument(String instrument) throws ParseExceptio
}

/**
* Parses {@code Collection<String> instruments} into a {@code Set<Instrument>}.
* Parses {@code Collection<String> instruments} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseInstruments(Collection<String> instruments) throws ParseException {
requireNonNull(instruments);
Expand All @@ -168,7 +168,7 @@ public static Genre parseGenre(String genre) throws ParseException {
}

/**
* Parses {@code Collection<String> genres} into a {@code Set<Genre>}.
* Parses {@code Collection<String> genres} into a {@code Set<Tag>}.
*/
public static Set<Tag> parseGenres(Collection<String> genres) throws ParseException {
requireNonNull(genres);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public FindCommand parse(String args) throws ParseException {
throw new ParseException(FindCommand.MESSAGE_MORE_THAN_ONE_WORD);
}

HashSet<Predicate<Musician>> predicates = this.buildPredicates(names, tags, instruments, genres);

return new FindCommand(predicates);
}

/** Builds a set of predicates based on the given lists of keywords. If a list is empty, skip over it. */
private HashSet<Predicate<Musician>> buildPredicates(List<String> names, List<String> tags,
List<String> instruments, List<String> genres) {
HashSet<Predicate<Musician>> predicates = new HashSet<>();
if (!names.isEmpty()) {
predicates.add(new NameContainsKeywordsPredicate(names));
Expand All @@ -70,8 +78,6 @@ public FindCommand parse(String args) throws ParseException {
if (!genres.isEmpty()) {
predicates.add(new GenreMatchesPredicate(genres));
}

return new FindCommand(predicates);
return predicates;
}

}
17 changes: 10 additions & 7 deletions src/main/java/seedu/address/model/tag/GenreMatchesPredicate.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model.tag;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -9,15 +10,16 @@
/**
* Tests that a {@code Musician}'s {@code Genre} tag matches any of the keywords given.
*/
public class GenreMatchesPredicate extends TagMatchesPredicate {
public class GenreMatchesPredicate implements Predicate<Musician> {

public GenreMatchesPredicate(List<String> genre) {
super(genre);
private final List<String> genres;

public GenreMatchesPredicate(List<String> genres) {
this.genres = genres;
}

@Override
public boolean test(Musician musician) {
List<String> genres = super.getTagNames();
return genres.stream()
.anyMatch(genreToMatch -> musician.getGenres().stream().anyMatch(
musicianGenre -> StringUtil.containsWordIgnoreCase(musicianGenre.tagName, genreToMatch)
Expand All @@ -35,17 +37,18 @@ public boolean equals(Object other) {
return false;
}

return super.equals(other);
GenreMatchesPredicate otherGenreMatchesPredicate = (GenreMatchesPredicate) other;
return genres.equals(otherGenreMatchesPredicate.genres);
}

@Override
public int hashCode() {
return super.hashCode();
return genres.hashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this).add("genres", super.getTagNames()).toString();
return new ToStringBuilder(this).add("genres", genres).toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.model.tag;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -9,15 +10,16 @@
/**
* Tests that a {@code Musician}'s {@code Instrument} tag matches any of the keywords given.
*/
public class InstrumentMatchesPredicate extends TagMatchesPredicate {
public class InstrumentMatchesPredicate implements Predicate<Musician> {

public InstrumentMatchesPredicate(List<String> instrument) {
super(instrument);
private final List<String> instruments;

public InstrumentMatchesPredicate(List<String> instruments) {
this.instruments = instruments;
}

@Override
public boolean test(Musician musician) {
List<String> instruments = super.getTagNames();
return instruments.stream()
.anyMatch(instrumentToMatch -> musician.getInstruments().stream().anyMatch(
musicianInstrument -> StringUtil.containsWordIgnoreCase(
Expand All @@ -36,17 +38,17 @@ public boolean equals(Object other) {
return false;
}

return super.equals(other);
InstrumentMatchesPredicate otherInstrumentMatchesPredicate = (InstrumentMatchesPredicate) other;
return instruments.equals(otherInstrumentMatchesPredicate.instruments);
}

@Override
public int hashCode() {
return super.hashCode();
return instruments.hashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this).add("instruments", super.getTagNames()).toString();
return new ToStringBuilder(this).add("instruments", instruments).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public boolean test(Musician musician) {
));
}

protected List<String> getTagNames() {
return tagNames;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down

0 comments on commit 619ee52

Please sign in to comment.