Coverage Report - org.apache.camel.bam.TimeBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
TimeBuilder
0% 
0% 
1.118
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.bam;
 18  
 
 19  
 import java.util.concurrent.TimeUnit;
 20  
 import java.util.Date;
 21  
 
 22  
 /**
 23  
  * A fluent builder of times
 24  
  *
 25  
  * @version $Revision: $
 26  
  */
 27  
 public class TimeBuilder {
 28  0
     private long number;
 29  0
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
 30  
     private boolean configuredTime;
 31  
 
 32  
     /**
 33  
      * Creates a time which by default is in milliseconds unless
 34  
      * specified using a time based builder method
 35  
      */
 36  0
     public static TimeBuilder time(long number) {
 37  0
         return new TimeBuilder(number);
 38  
     }
 39  0
 
 40  0
     public TimeBuilder(long number) {
 41  0
         this.number = number;
 42  0
     }
 43  
 
 44  0
     public long toMillis() {
 45  0
         return timeUnit.toMillis(number);
 46  
     }
 47  
     
 48  0
     public Date toDate() {
 49  0
         return new Date(toMillis());
 50  
     }
 51  
 
 52  
     public TimeBuilder millis() {
 53  0
         setTimeUnit(TimeUnit.MILLISECONDS);
 54  0
         return this;
 55  
     }
 56  
 
 57  
     public TimeBuilder nanos() {
 58  0
         setTimeUnit(TimeUnit.NANOSECONDS);
 59  0
         return this;
 60  
     }
 61  
 
 62  
     public TimeBuilder micros() {
 63  0
         setTimeUnit(TimeUnit.MICROSECONDS);
 64  0
         return this;
 65  
     }
 66  
 
 67  
     public TimeBuilder seconds() {
 68  0
         setTimeUnit(TimeUnit.SECONDS);
 69  0
         return this;
 70  0
     }
 71  
 
 72  
     public TimeBuilder minutes() {
 73  0
         setTimeUnit(TimeUnit.SECONDS);
 74  0
         number = minutesAsSeconds(number);
 75  0
         return this;
 76  0
     }
 77  
 
 78  
     public TimeBuilder hours() {
 79  0
         setTimeUnit(TimeUnit.SECONDS);
 80  0
         number = hoursAsSeconds(number);
 81  0
         return this;
 82  0
     }
 83  
 
 84  
     public TimeBuilder days() {
 85  0
         setTimeUnit(TimeUnit.SECONDS);
 86  0
         number = daysAsSeconds(number);
 87  0
         return this;
 88  
     }
 89  
 
 90  0
     public long getNumber() {
 91  0
         return number;
 92  
     }
 93  
 
 94  0
     public TimeUnit getTimeUnit() {
 95  0
         return timeUnit;
 96  
     }
 97  
 
 98  0
     public void setTimeUnit(TimeUnit timeUnit) {
 99  0
         if (configuredTime) {
 100  0
             throw new IllegalArgumentException("Cannot configure the time unit twice!");
 101  0
         }
 102  
         else {
 103  0
            configuredTime = true;
 104  0
         }
 105  0
         this.timeUnit = timeUnit;
 106  0
     }
 107  
 
 108  0
     protected long minutesAsSeconds(long value) {
 109  0
         return value * 60;
 110  
     }
 111  
 
 112  0
     protected long hoursAsSeconds(long value) {
 113  0
         return minutesAsSeconds(value) * 60;
 114  
     }
 115  
 
 116  
     protected long daysAsSeconds(long value) {
 117  0
         return hoursAsSeconds(value) * 24;
 118  
     }
 119  
 
 120  
 }