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.bam; 018 019 import java.util.concurrent.TimeUnit; 020 import java.util.Date; 021 022 /** 023 * A fluent builder of times 024 * 025 * @version $Revision: $ 026 */ 027 public class TimeBuilder { 028 private long number; 029 private TimeUnit timeUnit = TimeUnit.MILLISECONDS; 030 private boolean configuredTime; 031 032 /** 033 * Creates a time which by default is in milliseconds unless 034 * specified using a time based builder method 035 */ 036 public static TimeBuilder time(long number) { 037 return new TimeBuilder(number); 038 } 039 040 public TimeBuilder(long number) { 041 this.number = number; 042 } 043 044 public long toMillis() { 045 return timeUnit.toMillis(number); 046 } 047 048 public Date toDate() { 049 return new Date(toMillis()); 050 } 051 052 public TimeBuilder millis() { 053 setTimeUnit(TimeUnit.MILLISECONDS); 054 return this; 055 } 056 057 public TimeBuilder nanos() { 058 setTimeUnit(TimeUnit.NANOSECONDS); 059 return this; 060 } 061 062 public TimeBuilder micros() { 063 setTimeUnit(TimeUnit.MICROSECONDS); 064 return this; 065 } 066 067 public TimeBuilder seconds() { 068 setTimeUnit(TimeUnit.SECONDS); 069 return this; 070 } 071 072 public TimeBuilder minutes() { 073 setTimeUnit(TimeUnit.SECONDS); 074 number = minutesAsSeconds(number); 075 return this; 076 } 077 078 public TimeBuilder hours() { 079 setTimeUnit(TimeUnit.SECONDS); 080 number = hoursAsSeconds(number); 081 return this; 082 } 083 084 public TimeBuilder days() { 085 setTimeUnit(TimeUnit.SECONDS); 086 number = daysAsSeconds(number); 087 return this; 088 } 089 090 public long getNumber() { 091 return number; 092 } 093 094 public TimeUnit getTimeUnit() { 095 return timeUnit; 096 } 097 098 public void setTimeUnit(TimeUnit timeUnit) { 099 if (configuredTime) { 100 throw new IllegalArgumentException("Cannot configure the time unit twice!"); 101 } 102 else { 103 configuredTime = true; 104 } 105 this.timeUnit = timeUnit; 106 } 107 108 protected long minutesAsSeconds(long value) { 109 return value * 60; 110 } 111 112 protected long hoursAsSeconds(long value) { 113 return minutesAsSeconds(value) * 60; 114 } 115 116 protected long daysAsSeconds(long value) { 117 return hoursAsSeconds(value) * 24; 118 } 119 120 }