Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#70 from claraadora/meeting-fix
Browse files Browse the repository at this point in the history
Convert to Optionals for Optional fields
  • Loading branch information
claraadora authored Oct 15, 2020
2 parents dbb62fe + 1ad0673 commit 195c6bf
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.meeting.ModelMeeting;
import seedu.address.model.meeting.meeting.Contacts;
import seedu.address.model.meeting.meeting.From;
import seedu.address.model.meeting.meeting.Location;
import seedu.address.model.meeting.meeting.Meeting;
import seedu.address.model.meeting.meeting.To;
import seedu.address.model.util.Description;
import seedu.address.model.util.Contacts;
import seedu.address.model.util.OptionalDescription;
import seedu.address.model.util.Title;


Expand Down Expand Up @@ -94,7 +94,7 @@ private static Meeting createEditedMeeting(Meeting meetingToEdit, EditMeetingDes
// String updatedLocation = editMeetingDescriptor.getLocation().orElse(meetingToEdit.getLocation());

// return new Meeting(updatedTitle, updatedDesc, updatedFrom, updatedTo, updatedContacts, updatedLocation);
return new Meeting(new Title("A"), new Description("B"), new From("2"),
return new Meeting(new Title("A"), new OptionalDescription("B"), new From("2"),
new To("3"), new Contacts("1,2,3"), new Location("SG"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.meeting.meeting.Contacts;
import seedu.address.logic.parser.util.ParserCommon;
import seedu.address.model.meeting.meeting.From;
import seedu.address.model.meeting.meeting.Location;
import seedu.address.model.meeting.meeting.Meeting;
import seedu.address.model.meeting.meeting.To;
import seedu.address.model.util.Description;
import seedu.address.model.util.Contacts;
import seedu.address.model.util.OptionalDescription;
import seedu.address.model.util.Title;

/**
* Parses input arguments and creates a new AddCommand object
*/
public class AddCommandParser implements Parser<AddCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
Expand All @@ -45,12 +45,11 @@ public AddCommand parse(String args) throws ParseException {
}

Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
Description description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
OptionalDescription description = ParserCommon.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION));
From from = ParserUtil.parseFrom(argMultimap.getValue(PREFIX_FROM).get());
To to = ParserUtil.parseTo(argMultimap.getValue(PREFIX_TO).get());
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get());
Contacts contacts = ParserUtil.parseContacts(argMultimap.getValue(PREFIX_CONTACTS).get());

Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION));
Contacts contacts = ParserCommon.parseContacts(argMultimap.getValue(PREFIX_CONTACTS));
Meeting meeting = new Meeting(title, description, from, to, contacts, location);

return new AddCommand(meeting);
Expand Down
42 changes: 7 additions & 35 deletions src/main/java/seedu/address/logic/parser/meeting/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import static java.util.Objects.requireNonNull;

import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.meeting.meeting.Contacts;
import seedu.address.model.meeting.meeting.From;
import seedu.address.model.meeting.meeting.Location;
import seedu.address.model.meeting.meeting.To;
import seedu.address.model.util.Description;
import seedu.address.model.util.Title;

/**
Expand Down Expand Up @@ -46,21 +46,6 @@ public static Title parseTitle(String title) throws ParseException {
return new Title(trimmedTitle);
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code phone} is invalid.
*/
public static Description parseDescription(String description) throws ParseException {
requireNonNull(description);
String trimmedDescription = description.trim();
if (!Description.isValidDescription(trimmedDescription)) {
throw new ParseException(Title.MESSAGE_CONSTRAINTS);
}
return new Description(trimmedDescription);
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
Expand Down Expand Up @@ -91,30 +76,17 @@ public static From parseFrom(String from) throws ParseException {
return new From(trimmedFrom);
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code address} is invalid.
*/
public static Contacts parseContacts(String contacts) throws ParseException {
requireNonNull(contacts);
String trimmedContacts = contacts.trim();
if (!Contacts.isValidContacts(trimmedContacts)) {
throw new ParseException(Contacts.MESSAGE_CONSTRAINTS);
}
return new Contacts(trimmedContacts);
}

/**
* Parses a {@code String address} into an {@code Address}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code location} is invalid.
*/
public static Location parseLocation(String location) throws ParseException {
requireNonNull(location);
String trimmedLocation = location.trim();
public static Location parseLocation(Optional<String> location) throws ParseException {
if (location.isEmpty()) {
return new Location(location);
}
String trimmedLocation = location.get().trim();
if (!Location.isValidLocation(trimmedLocation)) {
throw new ParseException(Location.MESSAGE_CONSTRAINTS);
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/logic/parser/util/ParserCommon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.parser.util;

import java.util.Optional;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.util.Contacts;
import seedu.address.model.util.OptionalDescription;
import seedu.address.model.util.Title;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
*/
public class ParserCommon {

/**
* Parses a {@code Optional<String> description} into a {@code Description}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code phone} is invalid.
*/
public static OptionalDescription parseDescription(Optional<String> description) throws ParseException {
if (description.isEmpty()) {
return new OptionalDescription(description);
}
String trimmedDescription = description.get().trim();
if (!OptionalDescription.isValidDescription(trimmedDescription)) {
throw new ParseException(Title.MESSAGE_CONSTRAINTS);
}
return new OptionalDescription(trimmedDescription);
}

/**
* Parses a {@code Optional<String> contacts} into an {@code Contacts}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code contacts} is invalid.
*/
public static Contacts parseContacts(Optional<String> contacts) throws ParseException {
if (contacts.isEmpty()) {
return new Contacts(contacts);
}
String trimmedContacts = contacts.get().trim();
if (!Contacts.isValidContacts(trimmedContacts)) {
throw new ParseException(Contacts.MESSAGE_CONSTRAINTS);
}
return new Contacts(trimmedContacts);
}
}
25 changes: 21 additions & 4 deletions src/main/java/seedu/address/model/meeting/meeting/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.util.Optional;

/**
* Represents a Person's location in the location book.
* Guarantees: immutable; is valid as declared in {@link #isValidLocation(String)}
*/
public class Location {

public static final String EMPTY_LOCATION_FIELD = "-";
public static final String MESSAGE_CONSTRAINTS = "Locations can take any values, and it should not be blank";

/*
Expand All @@ -17,7 +19,22 @@ public class Location {
*/
public static final String VALIDATION_REGEX = "[^\\s].*";

public final String value;
/*
* Represents the value of Location.
*/
public final Optional<String> value;

/**
* Constructs an {@code Location}.
*
* @param location A valid location.
*/
public Location(Optional<String> location) {
if (location.isPresent()) {
checkArgument(isValidLocation(location.get()), MESSAGE_CONSTRAINTS);
}
value = location;
}

/**
* Constructs an {@code Location}.
Expand All @@ -27,7 +44,7 @@ public class Location {
public Location(String location) {
requireNonNull(location);
checkArgument(isValidLocation(location), MESSAGE_CONSTRAINTS);
value = location;
value = Optional.of(location);
}

/**
Expand All @@ -39,7 +56,7 @@ public static boolean isValidLocation(String test) {

@Override
public String toString() {
return value;
return value.orElse(EMPTY_LOCATION_FIELD);
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/model/meeting/meeting/Meeting.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import java.util.Objects;

import seedu.address.model.util.Description;
import seedu.address.model.util.Contacts;
import seedu.address.model.util.OptionalDescription;
import seedu.address.model.util.Title;

/**
Expand All @@ -14,7 +15,7 @@
public class Meeting {

private final Title title;
private final Description description;
private final OptionalDescription description;
private final From from;
private final To to;
private final Contacts contacts;
Expand All @@ -23,7 +24,8 @@ public class Meeting {
/**
* Every field must be present and not null.
*/
public Meeting(Title title, Description description, From from, To to, Contacts contacts, Location location) {
public Meeting(Title title, OptionalDescription description, From from, To to,
Contacts contacts, Location location) {
requireAllNonNull(title, description, from, to, contacts, location);
this.title = title;
this.description = description;
Expand All @@ -37,7 +39,7 @@ public Title getTitle() {
return title;
}

public Description getDescription() {
public OptionalDescription getDescription() {
return description;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package seedu.address.model.meeting.meeting;
package seedu.address.model.util;

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

import java.util.Optional;

/**
* Represents a Meeting's contacts in the meeting book.
*/
public class Contacts {
public static final String EMPTY_CONTACTS_FIELD = "-";
public static final String MESSAGE_CONSTRAINTS =
"Contacts can only take numerical values separated with commas";

Expand All @@ -19,17 +22,32 @@ public class Contacts {
*/
public static final String VALIDATION_REGEX = "(\\d+)(,\\s*\\d+)*";

public final String value;
/*
* Represents the value of Contacts.
*/
public final Optional<String> value;

/**
* Constructs a {@code Contacts}.
*
* @param contacts A valid Optional of contact name strings.
*/
public Contacts(Optional<String> contacts) {
if (contacts.isPresent()) {
checkArgument(isValidContacts(contacts.get()), MESSAGE_CONSTRAINTS);
}
value = contacts;
}

/**
* Constructs a {@code Contact}.
* Constructs a {@code Contacts}.
*
* @param contacts A valid contacts.
* @param contacts A valid Optional of contact name strings.
*/
public Contacts(String contacts) {
requireNonNull(contacts);
checkArgument(isValidContacts(contacts), MESSAGE_CONSTRAINTS);
value = contacts;
value = Optional.of(contacts);
}

/**
Expand All @@ -41,7 +59,7 @@ public static boolean isValidContacts(String test) {

@Override
public String toString() {
return value;
return value.orElse(EMPTY_CONTACTS_FIELD);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/util/Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Represents a Deliverable's description in the deliverable book.
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)}
*/

// TODO: to be deprecated
public class Description {

public static final String MESSAGE_CONSTRAINTS = "Descriptions can take any values, and it should not be blank";
Expand Down
Loading

0 comments on commit 195c6bf

Please sign in to comment.