omkreddy commented on code in PR #17972:
URL: https://github.com/apache/kafka/pull/17972#discussion_r1863009418
##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/AutoOffsetResetStrategy.java:
##########
@@ -39,29 +43,67 @@ public String toString() {
public static final AutoOffsetResetStrategy NONE = new
AutoOffsetResetStrategy(StrategyType.NONE);
private final StrategyType type;
+ private final Optional<Duration> duration;
private AutoOffsetResetStrategy(StrategyType type) {
+ this(type, Optional.empty());
+ }
+
+ private AutoOffsetResetStrategy(StrategyType type, Optional<Duration>
duration) {
this.type = type;
+ this.duration = duration;
}
+ /**
+ * Returns true if the given string is a valid auto offset reset strategy.
+ */
public static boolean isValid(String offsetStrategy) {
- return
Arrays.asList(Utils.enumOptions(StrategyType.class)).contains(offsetStrategy);
+ if (offsetStrategy == null) {
+ return false;
+ } else if (StrategyType.BY_DURATION.toString().equals(offsetStrategy))
{
+ return false;
+ } else if
(Arrays.asList(Utils.enumOptions(StrategyType.class)).contains(offsetStrategy))
{
+ return true;
+ } else if (offsetStrategy.startsWith(StrategyType.BY_DURATION + ":")) {
+ String isoDuration =
offsetStrategy.substring(StrategyType.BY_DURATION.toString().length() + 1);
+ try {
+ Duration duration = Duration.parse(isoDuration);
+ return !duration.isNegative();
+ } catch (Exception e) {
+ return false;
+ }
+ } else {
+ return false;
+ }
}
+ /**
+ * Returns the AutoOffsetResetStrategy from the given string.
+ */
public static AutoOffsetResetStrategy fromString(String offsetStrategy) {
- if (offsetStrategy == null || !isValid(offsetStrategy)) {
+ if (!isValid(offsetStrategy)) {
throw new IllegalArgumentException("Unknown auto offset reset
strategy: " + offsetStrategy);
}
- StrategyType type =
StrategyType.valueOf(offsetStrategy.toUpperCase(Locale.ROOT));
- switch (type) {
- case EARLIEST:
- return EARLIEST;
- case LATEST:
- return LATEST;
- case NONE:
- return NONE;
- default:
- throw new IllegalArgumentException("Unknown auto offset reset
strategy: " + offsetStrategy);
+
+ if (offsetStrategy.startsWith(StrategyType.BY_DURATION + ":")) {
+ String isoDuration =
offsetStrategy.substring(StrategyType.BY_DURATION.toString().length() + 1);
+ return new AutoOffsetResetStrategy(StrategyType.BY_DURATION,
Optional.of(Duration.parse(isoDuration)));
Review Comment:
Thanks for the review. I have removed isValid method to avoid duplication.
Now I am using `fromString` method in the validator also.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]