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.wicket.util.time;
018
019import java.time.Duration;
020import java.time.Instant;
021import java.util.Locale;
022
023import org.apache.wicket.util.string.StringValue;
024
025/**
026 * 
027 * Utility class for {@link Duration}
028 *
029 */
030public class Durations
031{
032
033        public static String toString(final Duration duration, final Locale locale)
034        {
035                if (duration.toMillis() >= 0)
036                {
037                        if (duration.toDays() >= 1.0)
038                        {
039                                return unitString(duration.toDays(), "day", locale);
040                        }
041
042                        if (duration.toHours() >= 1.0)
043                        {
044                                return unitString(duration.toHours(), "hour", locale);
045                        }
046
047                        if (duration.toMinutes() >= 1.0)
048                        {
049                                return unitString(duration.toMinutes(), "minute", locale);
050                        }
051
052                        if (duration.toSeconds() >= 1.0)
053                        {
054                                return unitString(duration.toSeconds(), "second", locale);
055                        }
056
057                        return unitString(duration.toMillis(), "millisecond", locale);
058                }
059                else
060                {
061                        return "N/A";
062                }
063        }
064
065        /**
066         * Converts a value to a unit-suffixed value, taking care of English singular/plural suffix.
067         * 
068         * @param value
069         *            a <code>double</code> value to format
070         * @param units
071         *            the units to apply singular or plural suffix to
072         * @param locale
073         *            the <code>Locale</code>
074         * @return a <code>String</code> representation
075         */
076        private static String unitString(final double value, final String units, final Locale locale)
077        {
078                return StringValue.valueOf(value, locale) + " " + units + ((value > 1.0) ? "s" : "");
079        }
080
081        /**
082         * Calculates the duration between a given {@link Instant} and the current one.
083         * 
084         * @param start
085         *            a given instant
086         * @return the duration between a given Instant and the current one
087         */
088        public static Duration elapsedSince(Instant start)
089        {
090                return Duration.between(start, Instant.now());
091        }
092}