Skip to content

Commit

Permalink
Merge pull request #117 from onepersonhere/branch-code-quality
Browse files Browse the repository at this point in the history
Fix code quality issues
  • Loading branch information
zbz-lvlv committed Oct 8, 2022
2 parents 9ac1d70 + 42960a6 commit ff02c14
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 72 deletions.
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, minecraftName, phone, email, address, socials, tags, servers, timeZone);
return CollectionUtil.isAnyNonNull(name, minecraftName, phone, email, address,
socials, tags, servers, timeZone);
}

public void setName(Name name) {
Expand Down Expand Up @@ -238,8 +239,8 @@ public void setSocials(Set<Social> socials) {
*/
public Optional<Set<Social>> getSocials() {
return (socials != null)
? Optional.of(Collections.unmodifiableSet(socials))
: Optional.empty();
? Optional.of(Collections.unmodifiableSet(socials))
: Optional.empty();
}

/**
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.*;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINECRAFT_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINECRAFT_SERVER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCIAL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIMEZONE;

import java.util.Set;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.utils.CheckedFunction;
import seedu.address.model.person.*;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.MinecraftName;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Social;
import seedu.address.model.person.TimeZone;
import seedu.address.model.server.Server;
import seedu.address.model.tag.Tag;

Expand All @@ -26,15 +41,17 @@ public class AddCommandParser implements Parser<AddCommand> {
public AddCommand parse(String args) throws ParseException {

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_MINECRAFT_NAME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

Name name = (Name) parseMandatoryArgument(PREFIX_NAME, argMultimap, ParserUtil::parseName);
MinecraftName mcName = (MinecraftName) parseMandatoryArgument(PREFIX_MINECRAFT_NAME, argMultimap, ParserUtil::parseMinecraftName);
MinecraftName mcName = (MinecraftName) parseMandatoryArgument(PREFIX_MINECRAFT_NAME, argMultimap,
ParserUtil::parseMinecraftName);
Phone phone = (Phone) parseOptionalArgument(PREFIX_PHONE, argMultimap, ParserUtil::parsePhone);
Email email = (Email) parseOptionalArgument(PREFIX_EMAIL, argMultimap, ParserUtil::parseEmail);
Address address = (Address) parseOptionalArgument(PREFIX_ADDRESS, argMultimap, ParserUtil::parseAddress);
Expand All @@ -49,7 +66,7 @@ public AddCommand parse(String args) throws ParseException {
}

private Object parseOptionalArgument(Prefix prefix, ArgumentMultimap argMultimap,
CheckedFunction<String, ?> parserFn) throws ParseException{
CheckedFunction<String, ?> parserFn) throws ParseException {
if (argMultimap.getValue(prefix).isPresent()) {
return parserFn.apply(argMultimap.getValue(prefix).get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.utils.CheckedFunction;
import seedu.address.model.person.Social;
import seedu.address.model.person.TimeZone;
import seedu.address.model.server.Server;
import seedu.address.model.tag.Tag;

Expand All @@ -43,7 +40,8 @@ public EditCommand parse(String args) throws ParseException {
requireNonNull(args);

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);

Index index;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.exceptions.SocialNotFoundException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.MinecraftName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.address.logic.parser.exceptions;

/**
* Signals that the operation is unable to find the specified social media platform.
*/
public class SocialNotFoundException extends ParseException {
public SocialNotFoundException() {
super("Social media handle format is incorrect.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import seedu.address.logic.parser.exceptions.ParseException;

/**
* This is a functional interface which represents a function that can throw {@code ParseException}.
*
* @param <T> the type of the exception thrown by the function
* @param <R> the type of the result of the function
*/
@FunctionalInterface
public interface CheckedFunction<T, R> {
R apply(T t) throws ParseException;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {

private static final String SPECIAL_CHARACTERS = "+_.-";
public static final String MESSAGE_CONSTRAINTS = "Any string can be accepted";
public static final String VALIDATION_REGEX = ".*";
public static final String MESSAGE_CONSTRAINTS = "Any string can be accepted";
private static final String SPECIAL_CHARACTERS = "+_.-";

public final String value;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import seedu.address.model.server.Server;
import seedu.address.model.tag.Tag;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/address/model/person/Platform.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package seedu.address.model.person;

/**
* Represents a Person's platform in the address book.
*/
public enum Platform {
fb, // Facebook
ig, // Instagram
sc // Snapchat
sc // Snapchat
}


11 changes: 10 additions & 1 deletion src/main/java/seedu/address/model/person/PlatformConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

import seedu.address.logic.parser.exceptions.SocialNotFoundException;

/**
* Parses a Person's social media platform from a string.
*/
public class PlatformConverter {

/**
* Parses a string to a Platform enum.
*
* @param platformStr String to be parsed.
* @return Platform enum.
* @throws SocialNotFoundException If the string is not a valid platform.
*/
public static Platform stringToPlatform(String platformStr) throws SocialNotFoundException {
switch (platformStr) {
case "fb":
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/address/model/person/Social.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package seedu.address.model.person;

import seedu.address.logic.parser.exceptions.SocialNotFoundException;

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

import seedu.address.logic.parser.exceptions.SocialNotFoundException;

/**
* Represents a Person's social media in the address book.
*/
public class Social {

public static final String MESSAGE_CONSTRAINTS =
Expand All @@ -16,8 +19,8 @@ public class Social {
*/
public static final String VALIDATION_REGEX = ".*@.*";

public String handle;
public Platform platform;
private String handle;
private Platform platform;

/**
* Constructs a {@code Social}.
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/seedu/address/model/person/TimeZone.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package seedu.address.model.person;

import java.util.Calendar;
import java.util.GregorianCalendar;

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

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
* Represents a Person's timezone in the address book.
*/
public class TimeZone {

public static final String MESSAGE_CONSTRAINTS = "Timezone offsets should be double digit hours, " +
"followed by \":\", then double digit minutes";
public static final String MESSAGE_CONSTRAINTS = "Timezone offsets should be double digit hours, "
+ "followed by \":\", then double digit minutes";

private static final String OFFSET_REGEX = "^(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$";

private static final int HOURS_TO_MINUTES = 60;
private final String offset;

/**
* Constructs a {@code TimeZone}.
* @param offset
*/
public TimeZone(String offset) {
requireNonNull(offset);
if (offset.equals("")) {
Expand Down Expand Up @@ -43,6 +50,12 @@ public static boolean isValidTimeZone(String test) {
return test.matches(OFFSET_REGEX);
}

/**
* Returns the offset of the timezone.
*
* @param strOffset
* @return
*/
public static int convertToIntOffset(String strOffset) {
String [] strArray = strOffset.split(":");
return Integer.parseInt(strArray[0].trim()) * HOURS_TO_MINUTES //calculate hours
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/seedu/address/model/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Server in the address book.
*/
public class Server {

private static final String VALIDATION_REGEX = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!$)|$)){4}$";
private static final String VALIDATION_REGEX =
"^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!$)|$)){4}$";

private static final String SERVER_CONSTRAINTS =
"Server address should only contain numbers and dots. " +
"There can only be a maximum of 4 dots in the address.";
"Server address should only contain numbers and dots. "
+ "There can only be a maximum of 4 dots in the address.";

private final String serverName;


/**
* Constructs a {@code Server}.
* @param serverName A valid server name.
*/
public Server(String serverName) {
requireNonNull(serverName);
checkArgument(isValidServerName(serverName), SERVER_CONSTRAINTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.server.Server;

/**
* Json model of minecraft server address.
*/
public class JsonAdaptedMinecraftServer {

private final String serverName;
Expand All @@ -24,6 +27,11 @@ public String getServerName() {
return serverName;
}

/**
* Parses the serverName to produce a Server object
* @return Server object
* @throws IllegalValueException if the serverName is invalid
*/
public Server toModelType() throws IllegalValueException {
if (!Server.isValidServerName(serverName)) {
throw new IllegalValueException(Server.getServerConstraints());
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.storage;

import java.sql.Time;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -44,8 +42,13 @@ class JsonAdaptedPerson {
* Constructs a {@code JsonAdaptedPerson} with the given person details.
*/
@JsonCreator
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("minecraftName") String minecraftName, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address, @JsonProperty("socials") List<JsonAdaptedSocial> socials, @JsonProperty("tags") List<JsonAdaptedTag> tags, @JsonProperty("servers") List<JsonAdaptedMinecraftServer> servers, @JsonProperty("timeZone") String timeZone) {
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("minecraftName") String minecraftName,
@JsonProperty("phone") String phone, @JsonProperty("email") String email,
@JsonProperty("address") String address,
@JsonProperty("socials") List<JsonAdaptedSocial> socials,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("servers") List<JsonAdaptedMinecraftServer> servers,
@JsonProperty("timeZone") String timeZone) {
this.name = name;
this.minecraftName = minecraftName;
this.phone = phone;
Expand Down Expand Up @@ -106,9 +109,9 @@ public Person toModelType() throws IllegalValueException {
for (JsonAdaptedSocial social : this.socials) {
socials.add(social.toModelType());
}

final List<Server> servers = new ArrayList<>();
for (JsonAdaptedMinecraftServer server: this.servers) {
for (JsonAdaptedMinecraftServer server : this.servers) {
servers.add(server.toModelType());
}

Expand All @@ -121,7 +124,8 @@ public Person toModelType() throws IllegalValueException {
final Name modelName = new Name(name);

if (minecraftName == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, MinecraftName.class.getSimpleName()));
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
MinecraftName.class.getSimpleName()));
}
if (!MinecraftName.isValidMinecraftName(minecraftName)) {
throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS);
Expand Down Expand Up @@ -159,7 +163,8 @@ public Person toModelType() throws IllegalValueException {
final Set<Server> modelServers = new HashSet<>(servers);

if (timeZone == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, TimeZone.class.getSimpleName()));
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
TimeZone.class.getSimpleName()));
}
if (!TimeZone.isValidTimeZone(timeZone)) {
throw new IllegalValueException(TimeZone.MESSAGE_CONSTRAINTS);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/storage/JsonAdaptedSocial.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Jackson-friendly version of {@link Social}.
*/
class JsonAdaptedSocial{
class JsonAdaptedSocial {

private final String social;

Expand Down

0 comments on commit ff02c14

Please sign in to comment.