View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package pl.kernelpanic.dbmonster.util;
48  
49  /***
50   * Converter.
51   *
52   * @author Piotr Maj <piotr.maj@kernelpanic.pl>
53   *
54   * @version $Id: Converter.html,v 1.1 2007/06/21 08:38:14 sbahloul Exp $
55   */
56  public final class Converter {
57  
58      /***
59       * Creates a new converter.
60       */
61      private Converter() {
62  
63      }
64  
65      /***
66       * Formats the period.
67       *
68       * @param ms millis
69       *
70       * @return formatted time
71       */
72      public static final String formatTime(final long ms) {
73          if (ms < 0) {
74              return "Faster than light!"; //$NON-NLS-1$
75          }
76          long millis = ms;
77          long hours = millis / 3600000;
78          StringBuffer time = new StringBuffer();
79          if (hours != 0) {
80              time.append(hours);
81              time.append(" h."); //$NON-NLS-1$
82              millis -= (hours * 3600000);
83          }
84  
85          long minutes = millis / (60000);
86          if (minutes != 0) {
87              if (time.length() > 0) {
88                  time.append(" "); //$NON-NLS-1$
89              }
90              time.append(minutes);
91              time.append(" min."); //$NON-NLS-1$
92              millis -= (minutes * 60000);
93          }
94  
95          long seconds = millis / 1000;
96          if (seconds != 0) {
97              if (time.length() > 0) {
98                  time.append(" "); //$NON-NLS-1$
99              }
100             time.append(seconds);
101             time.append(" sec."); //$NON-NLS-1$
102             millis -= (seconds * 1000);
103         }
104 
105         if (millis != 0) {
106             if (time.length() > 0) {
107                 time.append(" "); //$NON-NLS-1$
108             }
109             time.append(millis);
110             time.append(" ms."); //$NON-NLS-1$
111         }
112 
113         if (time.length() == 0) {
114             time.append("0 ms."); //$NON-NLS-1$
115         }
116 
117         return time.toString();
118     }
119 
120     /***
121      * Check whether nulls is in range of 0 <= nulls <= 100
122      *
123      * @param nulls nulls.
124      *
125      * @return proper nulls
126      */
127     public static final int checkNulls(final int nulls) {
128         if (nulls >= 0 && nulls <= 100) {
129             return nulls;
130         }
131         if (nulls > 100) {
132             return 100;
133         } else {
134             return 0;
135         }
136     }
137 }