View Javadoc

1   /*
2    * #%L
3    * Bitrepository Protocol
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.common.utils;
26  
27  import java.math.BigInteger;
28  import java.util.Comparator;
29  import java.util.UnknownFormatConversionException;
30  
31  import org.bitrepository.bitrepositoryelements.TimeMeasureTYPE;
32  import org.bitrepository.bitrepositoryelements.TimeMeasureUnit;
33  import org.bitrepository.common.ArgumentValidator;
34  
35  /**
36   * Provides helper method for accessing {@link TimeMeasurementTYPE} objects. 
37   */
38  public class TimeMeasurementUtils {
39  
40      /**
41       * Private constructor. To prevent instantiation of this utility class.
42       */
43      private TimeMeasurementUtils() { }
44      
45      /**
46       * Generates a TimeMeasureTYPE object based on a milliseconds value.
47       * @param milliseconds The time measure in milliseconds.
48       * @return A corresponding <code>TimeMeasureTYPE</code> object.
49       */
50      public static TimeMeasureTYPE getTimeMeasurementFromMiliseconds(BigInteger milliseconds) {
51          TimeMeasureTYPE timeMeasure = new TimeMeasureTYPE();
52          timeMeasure.setTimeMeasureValue(milliseconds);
53          timeMeasure.setTimeMeasureUnit(TimeMeasureUnit.MILLISECONDS);
54          return timeMeasure; 
55      }
56      
57      /**
58       * Method for getting the maximum time. Uses the maximum value of Long and puts it in HOURS.
59       * @return The TimeMeasure for the maximum time.
60       */
61      public static TimeMeasureTYPE getMaximumTime() {
62          TimeMeasureTYPE timeMeasure = new TimeMeasureTYPE();
63          timeMeasure.setTimeMeasureValue(BigInteger.valueOf(Long.MAX_VALUE));
64          timeMeasure.setTimeMeasureUnit(TimeMeasureUnit.HOURS);
65          return timeMeasure; 
66      }
67      
68      /**
69       * Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first 
70       * argument is less than, equal to, or greater than the second.
71       * @param time1
72       * @param time2
73       * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or 
74       * greater than the second.
75       * @see Comparator
76       */
77      public static int compare(TimeMeasureTYPE time1, TimeMeasureTYPE time2) {
78          ArgumentValidator.checkNotNull(time1, "time1");
79          ArgumentValidator.checkNotNull(time2, "time2");
80          return convertToMilliSeconds(time1).compareTo(convertToMilliSeconds(time2));
81      }
82      
83      /** Normalizes <code>TimeMeasureTYPE</code> into milliseconds.
84       * 
85       * @param timeMeasure The time measure to convert
86       * @return The time measure in milliseconds
87       * @throws UnknownFormatConversionException Unable to interpret the supplied timeMeasure.
88       */
89      private static BigInteger convertToMilliSeconds(TimeMeasureTYPE timeMeasure) throws UnknownFormatConversionException {
90          if(TimeMeasureUnit.MILLISECONDS.equals(timeMeasure.getTimeMeasureUnit())) {
91              return timeMeasure.getTimeMeasureValue();
92          } else if ((TimeMeasureUnit.HOURS.equals(timeMeasure.getTimeMeasureUnit()))) {
93              return timeMeasure.getTimeMeasureValue().multiply(new BigInteger("3600000"));     
94          } else {
95              throw new UnknownFormatConversionException ("Unable to compare times, unknown unit " + 
96                      timeMeasure.getTimeMeasureUnit());
97          }
98      }
99      
100     /**
101      * Compares a TimeMeasure to a long representation in milliseconds.
102      * @param time1 The TimeMeasure to compare.
103      * @param time2 The time in milliseconds to compare.
104      * @return -1 if time2 is larger, 0 if they are equals, or 1 if time1 is larger.
105      */
106     public static int compare(TimeMeasureTYPE time1, long time2) {
107         return convertToMilliSeconds(time1).compareTo(BigInteger.valueOf(time2));
108     }
109     
110     /**
111      * Method for converting a TimeMeasure into a long.
112      * @param time1 The TimeMeasure to convert.
113      * @return The value of the TimeMeasure as a long.
114      */
115     public static long getTimeMeasureInLong(TimeMeasureTYPE time1) {
116         return convertToMilliSeconds(time1).longValue();
117     }
118 }