-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathCommand.java
70 lines (58 loc) · 2.11 KB
/
Command.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package seedu.addressbook.commands;
import seedu.addressbook.common.Messages;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import java.util.List;
import static seedu.addressbook.ui.TextUi.DISPLAYED_INDEX_OFFSET;
/**
* Represents an executable command.
*/
public class Command {
protected AddressBook addressBook;
protected List<? extends ReadOnlyPerson> relevantPersons;
private int targetIndex = -1;
/**
* @param targetIndex last visible listing index of the target person
*/
public Command(int targetIndex) {
this.setTargetIndex(targetIndex);
}
protected Command() {
}
/**
* Constructs a feedback message to summarise an operation that displayed a listing of persons.
*
* @param personsDisplayed used to generate summary
* @return summary message for persons displayed
*/
public static String getMessageForPersonListShownSummary(List<? extends ReadOnlyPerson> personsDisplayed) {
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, personsDisplayed.size());
}
/**
* Executes the command and returns the result.
*/
public CommandResult execute(){
throw new UnsupportedOperationException("This method is to be implemented by child classes");
};
/**
* Supplies the data the command will operate on.
*/
public void setData(AddressBook addressBook, List<? extends ReadOnlyPerson> relevantPersons) {
this.addressBook = addressBook;
this.relevantPersons = relevantPersons;
}
/**
* Extracts the the target person in the last shown list from the given arguments.
*
* @throws IndexOutOfBoundsException if the target index is out of bounds of the last viewed listing
*/
protected ReadOnlyPerson getTargetPerson() throws IndexOutOfBoundsException {
return relevantPersons.get(getTargetIndex() - DISPLAYED_INDEX_OFFSET);
}
public int getTargetIndex() {
return targetIndex;
}
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}
}