-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Some users might want to monitor repositories for a moving window rather than from a specific date. We can give a CLI option that runs the analysis for a period of specific length e.g., `--period 7d` runs the analysis for the last 7 days, and `--period 1w` runs the analysis for the last 1 week. Period can combine with either since date or until date, but not both. If both not supplied, it will be the period before the current date.
- Loading branch information
Showing
5 changed files
with
170 additions
and
3 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
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,45 @@ | ||
package reposense.parser; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
import net.sourceforge.argparse4j.inf.Argument; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.ArgumentType; | ||
|
||
/** | ||
* Verifies and parses a string-formatted period to an integer. | ||
*/ | ||
public class PeriodArgumentType implements ArgumentType<Optional<Integer>> { | ||
private static final String PARSE_EXCEPTION_MESSAGE_NOT_IN_NUMERIC = | ||
"Invalid format. Period must be in the format of nd (n days) or nw (n weeks), " | ||
+ "where n is a number greater than 0."; | ||
private static final String PARSE_EXCEPTION_MESSAGE_SMALLER_THAN_ZERO = | ||
"Invalid format. Period must be greater than 0."; | ||
private static final String PARSE_EXCEPTION_MESSAGE_NUMBER_TOO_LARGE = | ||
"Invalid format. Input number may be too large."; | ||
private static final Pattern PERIOD_PATTERN = Pattern.compile("[0-9]+[dw]"); | ||
|
||
@Override | ||
public Optional<Integer> convert(ArgumentParser parser, Argument arg, String value) throws ArgumentParserException { | ||
if (!PERIOD_PATTERN.matcher(value).matches()) { | ||
throw new ArgumentParserException( | ||
String.format(PARSE_EXCEPTION_MESSAGE_NOT_IN_NUMERIC, value), parser); | ||
} | ||
|
||
int multiplier = value.substring(value.length() - 1).equals("d") ? 1 : 7; | ||
try { | ||
int convertedValue = Integer.parseInt(value.substring(0, value.length() - 1)) * multiplier; | ||
if (convertedValue <= 0) { | ||
throw new ArgumentParserException( | ||
String.format(PARSE_EXCEPTION_MESSAGE_SMALLER_THAN_ZERO, value), parser); | ||
} | ||
|
||
return Optional.of(convertedValue); | ||
} catch (NumberFormatException e) { | ||
throw new ArgumentParserException( | ||
String.format(PARSE_EXCEPTION_MESSAGE_NUMBER_TOO_LARGE, value), parser); | ||
} | ||
} | ||
} |
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