001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.converter; 018 019import java.time.Duration; 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.apache.camel.Converter; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027/** 028 * Converters for java.time.Duration. 029 * Provides a converter from a string (ISO-8601) to a Duration, 030 * a Duration to a string (ISO-8601) and 031 * a Duration to millis (long) 032 */ 033@Converter 034public final class DurationConverter { 035 private static final Logger LOG = LoggerFactory.getLogger(DurationConverter.class); 036 037 /** 038 * Utility classes should not have a public constructor. 039 */ 040 private DurationConverter() { 041 } 042 043 @Converter 044 public static long toMilliSeconds(Duration source) { 045 long milliseconds = source.toMillis(); 046 LOG.trace("source: {} milliseconds: ", source, milliseconds); 047 return milliseconds; 048 } 049 050 @Converter 051 public static Duration fromString(String source) { 052 Duration duration = Duration.parse(source); 053 LOG.trace("source: {} milliseconds: ", source, duration); 054 return duration; 055 } 056 057 @Converter 058 public static String asString(Duration source) { 059 String result = source.toString(); 060 LOG.trace("source: {} milliseconds: ", source, result); 061 return result; 062 } 063}