Skip to content

Commit 195c6bf

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#70 from claraadora/meeting-fix
Convert to Optionals for Optional fields
2 parents dbb62fe + 1ad0673 commit 195c6bf

File tree

16 files changed

+226
-104
lines changed

16 files changed

+226
-104
lines changed

src/main/java/seedu/address/logic/commands/meeting/EditCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import seedu.address.logic.commands.CommandResult;
2121
import seedu.address.logic.commands.exceptions.CommandException;
2222
import seedu.address.model.meeting.ModelMeeting;
23-
import seedu.address.model.meeting.meeting.Contacts;
2423
import seedu.address.model.meeting.meeting.From;
2524
import seedu.address.model.meeting.meeting.Location;
2625
import seedu.address.model.meeting.meeting.Meeting;
2726
import seedu.address.model.meeting.meeting.To;
28-
import seedu.address.model.util.Description;
27+
import seedu.address.model.util.Contacts;
28+
import seedu.address.model.util.OptionalDescription;
2929
import seedu.address.model.util.Title;
3030

3131

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

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

src/main/java/seedu/address/logic/parser/meeting/AddCommandParser.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
import seedu.address.logic.parser.Parser;
1717
import seedu.address.logic.parser.Prefix;
1818
import seedu.address.logic.parser.exceptions.ParseException;
19-
import seedu.address.model.meeting.meeting.Contacts;
19+
import seedu.address.logic.parser.util.ParserCommon;
2020
import seedu.address.model.meeting.meeting.From;
2121
import seedu.address.model.meeting.meeting.Location;
2222
import seedu.address.model.meeting.meeting.Meeting;
2323
import seedu.address.model.meeting.meeting.To;
24-
import seedu.address.model.util.Description;
24+
import seedu.address.model.util.Contacts;
25+
import seedu.address.model.util.OptionalDescription;
2526
import seedu.address.model.util.Title;
2627

2728
/**
2829
* Parses input arguments and creates a new AddCommand object
2930
*/
3031
public class AddCommandParser implements Parser<AddCommand> {
31-
3232
/**
3333
* Parses the given {@code String} of arguments in the context of the AddCommand
3434
* and returns an AddCommand object for execution.
@@ -45,12 +45,11 @@ public AddCommand parse(String args) throws ParseException {
4545
}
4646

4747
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
48-
Description description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
48+
OptionalDescription description = ParserCommon.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION));
4949
From from = ParserUtil.parseFrom(argMultimap.getValue(PREFIX_FROM).get());
5050
To to = ParserUtil.parseTo(argMultimap.getValue(PREFIX_TO).get());
51-
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get());
52-
Contacts contacts = ParserUtil.parseContacts(argMultimap.getValue(PREFIX_CONTACTS).get());
53-
51+
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION));
52+
Contacts contacts = ParserCommon.parseContacts(argMultimap.getValue(PREFIX_CONTACTS));
5453
Meeting meeting = new Meeting(title, description, from, to, contacts, location);
5554

5655
return new AddCommand(meeting);

src/main/java/seedu/address/logic/parser/meeting/ParserUtil.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import static java.util.Objects.requireNonNull;
44

5+
import java.util.Optional;
6+
57
import seedu.address.commons.core.index.Index;
68
import seedu.address.commons.util.StringUtil;
79
import seedu.address.logic.parser.exceptions.ParseException;
8-
import seedu.address.model.meeting.meeting.Contacts;
910
import seedu.address.model.meeting.meeting.From;
1011
import seedu.address.model.meeting.meeting.Location;
1112
import seedu.address.model.meeting.meeting.To;
12-
import seedu.address.model.util.Description;
1313
import seedu.address.model.util.Title;
1414

1515
/**
@@ -46,21 +46,6 @@ public static Title parseTitle(String title) throws ParseException {
4646
return new Title(trimmedTitle);
4747
}
4848

49-
/**
50-
* Parses a {@code String phone} into a {@code Phone}.
51-
* Leading and trailing whitespaces will be trimmed.
52-
*
53-
* @throws ParseException if the given {@code phone} is invalid.
54-
*/
55-
public static Description parseDescription(String description) throws ParseException {
56-
requireNonNull(description);
57-
String trimmedDescription = description.trim();
58-
if (!Description.isValidDescription(trimmedDescription)) {
59-
throw new ParseException(Title.MESSAGE_CONSTRAINTS);
60-
}
61-
return new Description(trimmedDescription);
62-
}
63-
6449
/**
6550
* Parses a {@code String address} into an {@code Address}.
6651
* Leading and trailing whitespaces will be trimmed.
@@ -91,30 +76,17 @@ public static From parseFrom(String from) throws ParseException {
9176
return new From(trimmedFrom);
9277
}
9378

94-
/**
95-
* Parses a {@code String address} into an {@code Address}.
96-
* Leading and trailing whitespaces will be trimmed.
97-
*
98-
* @throws ParseException if the given {@code address} is invalid.
99-
*/
100-
public static Contacts parseContacts(String contacts) throws ParseException {
101-
requireNonNull(contacts);
102-
String trimmedContacts = contacts.trim();
103-
if (!Contacts.isValidContacts(trimmedContacts)) {
104-
throw new ParseException(Contacts.MESSAGE_CONSTRAINTS);
105-
}
106-
return new Contacts(trimmedContacts);
107-
}
108-
10979
/**
11080
* Parses a {@code String address} into an {@code Address}.
11181
* Leading and trailing whitespaces will be trimmed.
11282
*
11383
* @throws ParseException if the given {@code location} is invalid.
11484
*/
115-
public static Location parseLocation(String location) throws ParseException {
116-
requireNonNull(location);
117-
String trimmedLocation = location.trim();
85+
public static Location parseLocation(Optional<String> location) throws ParseException {
86+
if (location.isEmpty()) {
87+
return new Location(location);
88+
}
89+
String trimmedLocation = location.get().trim();
11890
if (!Location.isValidLocation(trimmedLocation)) {
11991
throw new ParseException(Location.MESSAGE_CONSTRAINTS);
12092
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package seedu.address.logic.parser.util;
2+
3+
import java.util.Optional;
4+
5+
import seedu.address.logic.parser.exceptions.ParseException;
6+
import seedu.address.model.util.Contacts;
7+
import seedu.address.model.util.OptionalDescription;
8+
import seedu.address.model.util.Title;
9+
10+
/**
11+
* Contains utility methods used for parsing strings in the various *Parser classes.
12+
*/
13+
public class ParserCommon {
14+
15+
/**
16+
* Parses a {@code Optional<String> description} into a {@code Description}.
17+
* Leading and trailing whitespaces will be trimmed.
18+
*
19+
* @throws ParseException if the given {@code phone} is invalid.
20+
*/
21+
public static OptionalDescription parseDescription(Optional<String> description) throws ParseException {
22+
if (description.isEmpty()) {
23+
return new OptionalDescription(description);
24+
}
25+
String trimmedDescription = description.get().trim();
26+
if (!OptionalDescription.isValidDescription(trimmedDescription)) {
27+
throw new ParseException(Title.MESSAGE_CONSTRAINTS);
28+
}
29+
return new OptionalDescription(trimmedDescription);
30+
}
31+
32+
/**
33+
* Parses a {@code Optional<String> contacts} into an {@code Contacts}.
34+
* Leading and trailing whitespaces will be trimmed.
35+
*
36+
* @throws ParseException if the given {@code contacts} is invalid.
37+
*/
38+
public static Contacts parseContacts(Optional<String> contacts) throws ParseException {
39+
if (contacts.isEmpty()) {
40+
return new Contacts(contacts);
41+
}
42+
String trimmedContacts = contacts.get().trim();
43+
if (!Contacts.isValidContacts(trimmedContacts)) {
44+
throw new ParseException(Contacts.MESSAGE_CONSTRAINTS);
45+
}
46+
return new Contacts(trimmedContacts);
47+
}
48+
}

src/main/java/seedu/address/model/meeting/meeting/Location.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import static java.util.Objects.requireNonNull;
44
import static seedu.address.commons.util.AppUtil.checkArgument;
55

6+
import java.util.Optional;
7+
68
/**
79
* Represents a Person's location in the location book.
810
* Guarantees: immutable; is valid as declared in {@link #isValidLocation(String)}
911
*/
1012
public class Location {
11-
13+
public static final String EMPTY_LOCATION_FIELD = "-";
1214
public static final String MESSAGE_CONSTRAINTS = "Locations can take any values, and it should not be blank";
1315

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

20-
public final String value;
22+
/*
23+
* Represents the value of Location.
24+
*/
25+
public final Optional<String> value;
26+
27+
/**
28+
* Constructs an {@code Location}.
29+
*
30+
* @param location A valid location.
31+
*/
32+
public Location(Optional<String> location) {
33+
if (location.isPresent()) {
34+
checkArgument(isValidLocation(location.get()), MESSAGE_CONSTRAINTS);
35+
}
36+
value = location;
37+
}
2138

2239
/**
2340
* Constructs an {@code Location}.
@@ -27,7 +44,7 @@ public class Location {
2744
public Location(String location) {
2845
requireNonNull(location);
2946
checkArgument(isValidLocation(location), MESSAGE_CONSTRAINTS);
30-
value = location;
47+
value = Optional.of(location);
3148
}
3249

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

4057
@Override
4158
public String toString() {
42-
return value;
59+
return value.orElse(EMPTY_LOCATION_FIELD);
4360
}
4461

4562
@Override

src/main/java/seedu/address/model/meeting/meeting/Meeting.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import java.util.Objects;
66

7-
import seedu.address.model.util.Description;
7+
import seedu.address.model.util.Contacts;
8+
import seedu.address.model.util.OptionalDescription;
89
import seedu.address.model.util.Title;
910

1011
/**
@@ -14,7 +15,7 @@
1415
public class Meeting {
1516

1617
private final Title title;
17-
private final Description description;
18+
private final OptionalDescription description;
1819
private final From from;
1920
private final To to;
2021
private final Contacts contacts;
@@ -23,7 +24,8 @@ public class Meeting {
2324
/**
2425
* Every field must be present and not null.
2526
*/
26-
public Meeting(Title title, Description description, From from, To to, Contacts contacts, Location location) {
27+
public Meeting(Title title, OptionalDescription description, From from, To to,
28+
Contacts contacts, Location location) {
2729
requireAllNonNull(title, description, from, to, contacts, location);
2830
this.title = title;
2931
this.description = description;
@@ -37,7 +39,7 @@ public Title getTitle() {
3739
return title;
3840
}
3941

40-
public Description getDescription() {
42+
public OptionalDescription getDescription() {
4143
return description;
4244
}
4345

src/main/java/seedu/address/model/meeting/meeting/Contacts.java renamed to src/main/java/seedu/address/model/util/Contacts.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
package seedu.address.model.meeting.meeting;
1+
package seedu.address.model.util;
22

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

6+
import java.util.Optional;
7+
68
/**
79
* Represents a Meeting's contacts in the meeting book.
810
*/
911
public class Contacts {
12+
public static final String EMPTY_CONTACTS_FIELD = "-";
1013
public static final String MESSAGE_CONSTRAINTS =
1114
"Contacts can only take numerical values separated with commas";
1215

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

22-
public final String value;
25+
/*
26+
* Represents the value of Contacts.
27+
*/
28+
public final Optional<String> value;
29+
30+
/**
31+
* Constructs a {@code Contacts}.
32+
*
33+
* @param contacts A valid Optional of contact name strings.
34+
*/
35+
public Contacts(Optional<String> contacts) {
36+
if (contacts.isPresent()) {
37+
checkArgument(isValidContacts(contacts.get()), MESSAGE_CONSTRAINTS);
38+
}
39+
value = contacts;
40+
}
2341

2442
/**
25-
* Constructs a {@code Contact}.
43+
* Constructs a {@code Contacts}.
2644
*
27-
* @param contacts A valid contacts.
45+
* @param contacts A valid Optional of contact name strings.
2846
*/
2947
public Contacts(String contacts) {
3048
requireNonNull(contacts);
3149
checkArgument(isValidContacts(contacts), MESSAGE_CONSTRAINTS);
32-
value = contacts;
50+
value = Optional.of(contacts);
3351
}
3452

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

4260
@Override
4361
public String toString() {
44-
return value;
62+
return value.orElse(EMPTY_CONTACTS_FIELD);
4563
}
4664

4765
@Override

src/main/java/seedu/address/model/util/Description.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Represents a Deliverable's description in the deliverable book.
88
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)}
99
*/
10+
11+
// TODO: to be deprecated
1012
public class Description {
1113

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

0 commit comments

Comments
 (0)