Skip to content

Commit

Permalink
Merge PR #182 from 'code-fix-and-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
wxwern authored Nov 13, 2023
2 parents b89cb52 + 9b8c8f8 commit ecda232
Show file tree
Hide file tree
Showing 40 changed files with 551 additions and 291 deletions.
10 changes: 5 additions & 5 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ It consists of several key components:

- **`AutocompleteSupplier`**:
- This class is responsible for generating possible flags and values to be used for suggestions.
- It takes an `AutocompleteDataSet` of flags, an optional `FlagValueSupplier` mapped to each flag, and can have corresponding `AutocompleteConstraint` applied to flags.
- It takes an `AutocompleteItemSet` of flags, an optional `FlagValueSupplier` mapped to each flag, and can have corresponding `AutocompleteConstraint` applied to flags.
- It helps determine what flags can be added to an existing command phrase based on constraints and existing flags.

- **`AutocompleteGenerator`**:
Expand All @@ -272,9 +272,9 @@ It offers static factory methods for quickly defining common rulesets. Examples
- `#where(item)#isPrerequisiteFor(dependents...)`: Defines dependencies between items, indicating that certain flags are prerequisites for others.
- `#where(item)#cannotExistAlongsideAnyOf(items...)`: Defines that an item cannot be present when any of the others are present.

#### `AutocompleteDataSet`
#### `AutocompleteItemSet`

The `AutocompleteDataSet` is a set of flags that retains knowledge of which flags have what rules and constraints. It helps determine which flags can be added to an existing set of flags given the known constraints.
The `AutocompleteItemSet` is a set of flags that retains knowledge of which flags have what rules and constraints. It helps determine which flags can be added to an existing set of flags given the known constraints.

This dataset can be constructed manually with flags and constraints, but it also offers static factory methods for quick creation of flag sets with common constraints. For example:
- `#oneAmongAllOf(items...)`: Creates a set where at most one out of all the provided items may appear.
Expand All @@ -297,9 +297,9 @@ By taking in both the command and the app model, it is possible to specify arbit

#### `AutocompleteSupplier`

The `AutocompleteSupplier` leverages the capabilities of `AutocompleteDataSet` and `FlagValueSupplier`.
The `AutocompleteSupplier` leverages the capabilities of `AutocompleteItemSet` and `FlagValueSupplier`.

Internally, it uses `AutocompleteDataSet` to determine what flags can be added after a given set of flags has been used in a command.
Internally, it uses `AutocompleteItemSet` to determine what flags can be added after a given set of flags has been used in a command.

This allows it to make suggestions based on constraints like "`--org` cannot exist together with `--rec`."

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/commons/core/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonValue;

/**
* Represents a version with major, minor and patch number
* Represents a version with major, minor and patch number.
*/
public class Version implements Comparable<Version> {

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/seedu/address/commons/util/EnumUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.address.commons.util;

import java.util.Arrays;

/**
* Helper class to for enums to perform common operations.
*/
public class EnumUtil {

private static final String ENUM_LOOKUP_FAILURE_STRING_FORMAT =
"'%s' is not a valid string representation for any enum constants of type '%s'";

/**
* Obtains the enum constant by looking up the enum's {@link Enum#toString()} value that matches the given input.
*
* @param input The input string to match against the enum's string with.
* @param cls The enum class.
* @param <E> The enum type.
* @return The enum constant.
* @throws IllegalArgumentException if the string does not match any of the enum's values.
*/
public static <E extends Enum<E>> E lookupByToString(Class<E> cls, String input)
throws IllegalArgumentException {

return Arrays.stream(cls.getEnumConstants())
.filter(e -> e.toString().equals(input))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format(
ENUM_LOOKUP_FAILURE_STRING_FORMAT,
input, cls.getSimpleName()
)));
}

/**
* Checks whether there exists an enum constant whose {@link Enum#toString()} value matches the given input.
*
* @param input The input string to match against the enum's string with.
* @param cls The enum class to enumerate through.
* @param <E> The enum type.
* @return true if an enum constant with the given input string as text representation exists, false otherwise.
*/
public static <E extends Enum<E>> boolean hasMatchingToString(Class<E> cls, String input) {
return Arrays.stream(cls.getEnumConstants()).anyMatch(e -> e.toString().equals(input));
}

}
8 changes: 5 additions & 3 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public interface Logic {
/**
* Executes the command and returns the result.
*
* @param commandText The command as entered by the user.
* @return the result of the command execution.
* @throws CommandException If an error occurs during command execution.
Expand All @@ -27,6 +28,7 @@ public interface Logic {

/**
* Parses the command and returns any autocompletion results.
*
* @param commandText The command as entered by the user.
* @return the result of the command execution.
*/
Expand All @@ -39,10 +41,10 @@ public interface Logic {
*/
ReadOnlyAddressBook getAddressBook();

/** Returns a view of the list of contacts */
/** Returns an unmodifiable view of the filtered and sorted list of contacts. */
ObservableList<Contact> getDisplayedContactList();

/** Returns an unmodifiable view of the filtered list of applications. */
/** Returns an unmodifiable view of the filtered and sorted list of applications. */
ObservableList<JobApplication> getDisplayedApplicationList();

/**
Expand All @@ -56,7 +58,7 @@ public interface Logic {
GuiSettings getGuiSettings();

/**
* Set the user prefs' GUI settings.
* Sets the user prefs' GUI settings.
*/
void setGuiSettings(GuiSettings guiSettings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Stream;

import seedu.address.commons.util.StringUtil;
import seedu.address.logic.autocomplete.components.PartitionedCommand;
import seedu.address.logic.parser.Flag;
import seedu.address.model.Model;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.function.Consumer;
import java.util.stream.Stream;

import seedu.address.logic.autocomplete.data.AutocompleteDataSet;
import seedu.address.logic.autocomplete.components.AutocompleteItemSet;
import seedu.address.logic.autocomplete.components.FlagValueSupplier;
import seedu.address.logic.autocomplete.components.PartitionedCommand;
import seedu.address.logic.parser.Flag;
import seedu.address.model.Model;

Expand All @@ -16,7 +18,7 @@
*/
public class AutocompleteSupplier {

private final AutocompleteDataSet<Flag> flags;
private final AutocompleteItemSet<Flag> flags;
private final Map<Flag, FlagValueSupplier> values;

/**
Expand All @@ -27,7 +29,7 @@ public class AutocompleteSupplier {
* @param values A map of auto-completable values for each flag that may be obtained via a model.
*/
public AutocompleteSupplier(
AutocompleteDataSet<Flag> flags,
AutocompleteItemSet<Flag> flags,
Map<Flag, FlagValueSupplier> values
) {
// Create new copies to prevent external modification.
Expand All @@ -41,9 +43,9 @@ public AutocompleteSupplier(
*
* @param flags The set of flags that should be used as part of the autocomplete results.
*
* @see #AutocompleteSupplier(AutocompleteDataSet, Map)
* @see #AutocompleteSupplier(AutocompleteItemSet, Map)
*/
public static AutocompleteSupplier from(AutocompleteDataSet<Flag> flags) {
public static AutocompleteSupplier from(AutocompleteItemSet<Flag> flags) {
return new AutocompleteSupplier(flags, Map.of());
}

Expand All @@ -54,8 +56,8 @@ public static AutocompleteSupplier from(AutocompleteDataSet<Flag> flags) {
* @param flagSets The sets of flags that should be used together as part of the autocomplete results.
*/
@SafeVarargs
public static AutocompleteSupplier from(AutocompleteDataSet<Flag>... flagSets) {
return from(AutocompleteDataSet.concat(flagSets));
public static AutocompleteSupplier from(AutocompleteItemSet<Flag>... flagSets) {
return from(AutocompleteItemSet.concat(flagSets));
}

/**
Expand All @@ -67,7 +69,7 @@ public static AutocompleteSupplier from(AutocompleteDataSet<Flag>... flagSets) {
*/
public static AutocompleteSupplier fromUniqueFlags(Flag... uniqueFlags) {
return AutocompleteSupplier.from(
AutocompleteDataSet.onceForEachOf(uniqueFlags)
AutocompleteItemSet.onceForEachOf(uniqueFlags)
);
}

Expand All @@ -80,7 +82,7 @@ public static AutocompleteSupplier fromUniqueFlags(Flag... uniqueFlags) {
*/
public static AutocompleteSupplier fromRepeatableFlags(Flag... repeatableFlags) {
return AutocompleteSupplier.from(
AutocompleteDataSet.anyNumberOf(repeatableFlags)
AutocompleteItemSet.anyNumberOf(repeatableFlags)
);
}

Expand Down Expand Up @@ -108,8 +110,7 @@ public Set<Flag> getOtherPossibleFlagsAsideFromFlagsPresent(Set<Flag> flagsPrese
*
* @param flag The flag to check against. This may be null to represent the preamble.
* @param currentCommand The current command structure. This should not be null.
* @param model The model to be supplied for generation. This may be null if model-data is not essential
* for any purpose.
* @param model The model to be supplied for generation. This may be null if the model is unavailable.
*/
public Optional<Stream<String>> getValidValues(Flag flag, PartitionedCommand currentCommand, Model model) {
try {
Expand All @@ -129,7 +130,7 @@ public Optional<Stream<String>> getValidValues(Flag flag, PartitionedCommand cur
* Configures the set of flags within this autocomplete supplier using the given {@code operator}.
* This also returns {@code this} instance, which is useful for chaining.
*/
public AutocompleteSupplier configureFlagSet(Consumer<AutocompleteDataSet<Flag>> operator) {
public AutocompleteSupplier configureFlagSet(Consumer<AutocompleteItemSet<Flag>> operator) {
operator.accept(this.flags);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.logic.autocomplete.data;
package seedu.address.logic.autocomplete.components;

import java.util.Collection;
import java.util.Set;
Expand All @@ -11,8 +11,12 @@
@FunctionalInterface
public interface AutocompleteConstraint<T> {

/**
* Returns whether {@code input} can be added to the set of {@code existingElements}.
*/
boolean isAllowed(T input, Set<? extends T> existingElements);


// Constraint operators

/**
Expand Down
Loading

0 comments on commit ecda232

Please sign in to comment.