/**
 * Copyright (C) 2014-2018 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.linkedin.pinot.common.config;

import java.util.concurrent.TimeUnit;


/**
 * DSL for durations, which turns "5 days" into a duration.
 */
public class DurationDsl implements SingleKeyDsl<Duration> {
  @Override
  public Duration parse(String text) {
    if (text == null || text.isEmpty()) {
      return null;
    }

    String[] parts = text.split(" ");
    final String unit = parts[1].toUpperCase();
    final String unitCount = parts[0];
    return new Duration(TimeUnit.valueOf(unit), Integer.parseInt(unitCount));
  }

  @Override
  public String unparse(Duration value) {
    return value.getUnitCount() + " " + value.getUnit();
  }
}

@@ -712,16 +712,73 @@
 meUnit;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 %0A%0A/**%0A *
@@ -900,16 +900,100 @@
 tion%3E %7B%0A
+  private static final Logger LOGGER = LoggerFactory.getLogger(DurationDsl.class);%0A%0A
   @Overr
@@ -1103,16 +1103,28 @@
 %0A    %7D%0A%0A
+    try %7B%0A  
     Stri
@@ -1153,16 +1153,18 @@
 t(%22 %22);%0A
+  
     fina
@@ -1203,16 +1203,18 @@
 Case();%0A
+  
     fina
@@ -1244,24 +1244,26 @@
 rts%5B0%5D;%0A    
+  
 return new D
@@ -1324,16 +1324,177 @@
 ount));%0A
+    %7D catch (Exception e) %7B%0A      LOGGER.warn(%22Caught exception while parsing duration %7B%7D, discarding the invalid duration.%22, e, text);%0A      return null;%0A    %7D%0A
   %7D%0A%0A  @
@@ -1540,24 +1540,51 @@
 on value) %7B%0A
+    if (value != null) %7B%0A  
     return v
@@ -1628,14 +1628,52 @@
 Unit();%0A
+    %7D else %7B%0A      return null;%0A    %7D%0A
   %7D%0A%7D%0A

Handle invalid durations in the duration DSL (#3505)