forked from nusCS2113-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from jitwei98/jitwei98/add-exportall-class
Add a Filetype class and relevant tests
- Loading branch information
Showing
8 changed files
with
163 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package seedu.address.model; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
import java.util.EnumSet; | ||
|
||
/** | ||
* Represents filetype available for import/export. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidFiletype(String)} | ||
*/ | ||
public class Filetype { | ||
|
||
public static final String MESSAGE_FILETYPE_CONSTRAINTS = | ||
"Filetype can take either \"csv\" or \"vcf\", and it should not be blank"; | ||
|
||
/* | ||
* The first character of the filetype must not be a whitespace, | ||
* otherwise " " (a blank string) becomes a valid input. | ||
*/ | ||
public static final String FILETYPE_VALIDATION_REGEX = "[^\\s].*"; | ||
|
||
/** | ||
* Filetypes that can be used to export contacts. | ||
*/ | ||
public enum Extension { | ||
csv, vcf | ||
} | ||
|
||
private final String value; | ||
|
||
/** | ||
* Constructs an {@code Filetype}. | ||
* | ||
* @param filetype A valid filetype. | ||
*/ | ||
public Filetype(String filetype) { | ||
requireNonNull(filetype); | ||
checkArgument(isValidFiletype(filetype), MESSAGE_FILETYPE_CONSTRAINTS); | ||
value = filetype; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid filetype. | ||
*/ | ||
public static boolean isValidFiletype(String test) { | ||
return test.matches(FILETYPE_VALIDATION_REGEX) && isValidExtension(test); | ||
} | ||
|
||
/** | ||
* Returns true if a given string matches any of the valid Extension. | ||
*/ | ||
public static boolean isValidExtension(String extension) { | ||
return contains(Extension.class, extension); | ||
} | ||
|
||
// Reused from | ||
// http://www.java2s.com/Tutorials/Java/Data_Type_How_to/String/ | ||
// Check_if_enum_contains_a_given_string.html with minor modifications | ||
|
||
/** | ||
* Returns true if an {@code enumClass} contains a specific {@code value}. | ||
* @param enumClass | ||
* @param value | ||
*/ | ||
private static <E extends Enum<E>> boolean contains(Class<E> enumClass, | ||
String value) { | ||
try { | ||
return EnumSet.allOf(enumClass) | ||
.contains(Enum.valueOf(enumClass, value)); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof Filetype // instanceof handles nulls | ||
&& value.equals(((Filetype) other).value)); // state check | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package seedu.address.model; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.testutil.Assert; | ||
|
||
class FiletypeTest { | ||
|
||
@Test | ||
public void constructor_null_throwsNullPointerException() { | ||
Assert.assertThrows(NullPointerException.class, () -> new Filetype(null)); | ||
} | ||
|
||
@Test | ||
public void constructor_invalidFiletype_throwsIllegalArgumentException() { | ||
String invalidFiletype = "ccc"; | ||
Assert.assertThrows(IllegalArgumentException.class, () -> new Filetype(invalidFiletype)); | ||
} | ||
|
||
@Test | ||
public void isValidFiletype() { | ||
// null filetype | ||
Assert.assertThrows(NullPointerException.class, () -> Filetype.isValidFiletype(null)); | ||
|
||
// invalid filetype | ||
assertFalse(Filetype.isValidFiletype("")); // empty string | ||
assertFalse(Filetype.isValidFiletype(" ")); // spaces only | ||
assertFalse(Filetype.isValidFiletype("coconut")); // not "csv" or "vcf" | ||
|
||
// valid filetype | ||
assertTrue(Filetype.isValidFiletype("csv")); | ||
assertTrue(Filetype.isValidFiletype("vcf")); | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
src/test/java/seedu/address/testutil/TypicalFiletypes.java
This file was deleted.
Oops, something went wrong.