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.concurrent.TimeUnit;
020 import java.util.Date;
021
022 /**
023 * A helper class for working with times in various units
024 *
025 * @version $Revision: $
026 */
027 public class Time {
028 private long number;
029 private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
030
031 public static Time millis(long value) {
032 return new Time(value, TimeUnit.MILLISECONDS);
033 }
034
035 public static Time micros(long value) {
036 return new Time(value, TimeUnit.MICROSECONDS);
037 }
038
039 public static Time nanos(long value) {
040 return new Time(value, TimeUnit.NANOSECONDS);
041 }
042
043 public static Time seconds(long value) {
044 return new Time(value, TimeUnit.SECONDS);
045 }
046
047 public static Time minutes(long value) {
048 return new Time(minutesAsSeconds(value), TimeUnit.MILLISECONDS);
049 }
050
051 public static Time hours(long value) {
052 return new Time(hoursAsSeconds(value), TimeUnit.MILLISECONDS);
053 }
054
055 public static Time days(long value) {
056 return new Time(daysAsSeconds(value), TimeUnit.MILLISECONDS);
057 }
058
059 public Time(long number, TimeUnit timeUnit) {
060 this.number = number;
061 this.timeUnit = timeUnit;
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 }