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 org.apache.camel.Converter; 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023 024/** 025 * Converters for java.time.Duration. 026 * Provides a converter from a string (ISO-8601) to a Duration, 027 * a Duration to a string (ISO-8601) and 028 * a Duration to millis (long) 029 */ 030@Converter 031public final class DurationConverter { 032 private static final Logger LOG = LoggerFactory.getLogger(DurationConverter.class); 033 034 /** 035 * Utility classes should not have a public constructor. 036 */ 037 private DurationConverter() { 038 } 039 040 @Converter 041 public static long toMilliSeconds(Duration source) { 042 long milliseconds = source.toMillis(); 043 LOG.trace("source: {} milliseconds: ", source, milliseconds); 044 return milliseconds; 045 } 046 047 @Converter 048 public static Duration fromString(String source) { 049 Duration duration = Duration.parse(source); 050 LOG.trace("source: {} milliseconds: ", source, duration); 051 return duration; 052 } 053 054 @Converter 055 public static String asString(Duration source) { 056 String result = source.toString(); 057 LOG.trace("source: {} milliseconds: ", source, result); 058 return result; 059 } 060}