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     */
017    package org.apache.camel.util;
018    
019    import java.util.Date;
020    import java.util.concurrent.TimeUnit;
021    
022    /**
023     * A helper class for working with times in various units
024     * 
025     * @version $Revision: 303 $
026     */
027    public class Time {
028        private long number;
029        private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
030    
031        public Time(long number, TimeUnit timeUnit) {
032            this.number = number;
033            this.timeUnit = timeUnit;
034        }
035    
036        public static Time millis(long value) {
037            return new Time(value, TimeUnit.MILLISECONDS);
038        }
039    
040        public static Time micros(long value) {
041            return new Time(value, TimeUnit.MICROSECONDS);
042        }
043    
044        public static Time nanos(long value) {
045            return new Time(value, TimeUnit.NANOSECONDS);
046        }
047    
048        public static Time seconds(long value) {
049            return new Time(value, TimeUnit.SECONDS);
050        }
051    
052        public static Time minutes(long value) {
053            return new Time(minutesAsSeconds(value), TimeUnit.MILLISECONDS);
054        }
055    
056        public static Time hours(long value) {
057            return new Time(hoursAsSeconds(value), TimeUnit.MILLISECONDS);
058        }
059    
060        public static Time days(long value) {
061            return new Time(daysAsSeconds(value), TimeUnit.MILLISECONDS);
062        }
063    
064        public long toMillis() {
065            return timeUnit.toMillis(number);
066        }
067    
068        public Date toDate() {
069            return new Date(toMillis());
070        }
071    
072        public long getNumber() {
073            return number;
074        }
075    
076        public TimeUnit getTimeUnit() {
077            return timeUnit;
078        }
079    
080        protected static long minutesAsSeconds(long value) {
081            return value * 60;
082        }
083    
084        protected static long hoursAsSeconds(long value) {
085            return minutesAsSeconds(value) * 60;
086        }
087    
088        protected static long daysAsSeconds(long value) {
089            return hoursAsSeconds(value) * 24;
090        }
091    }