KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > multiple > Utils


1 /*
2 Copyright (c) 2002,2003, Sosnoski Software Solutions, Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package multiple;
30
31 import java.util.Date JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Static utility class providing comparison methods.
36  *
37  * @author Dennis M. Sosnoski
38  * @version 0.8
39  */

40
41 public abstract class Utils
42 {
43     public static boolean equalStrings(String JavaDoc a, String JavaDoc b) {
44         return (a == null && b == null) || a.equals(b);
45     }
46     
47     public static boolean equalLists(List JavaDoc a, List JavaDoc b) {
48         return (a == null && b == null) || a.equals(b);
49     }
50
51     public static boolean equalAirports(AirportBean a, AirportBean b) {
52         return (a == null && b == null) || (a != null && a.equals(b));
53     }
54
55     public static boolean equalCarriers(CarrierBean a, CarrierBean b) {
56         return (a == null && b == null) || (a != null && a.equals(b));
57     }
58
59     public static boolean equalDates(Date JavaDoc a, Date JavaDoc b) {
60         return (a == null && b == null) || (a != null && a.equals(b));
61     }
62     
63     public static int timeToMinute(String JavaDoc time) {
64         
65         // find split between hour and minute
66
int mark = time.indexOf(':');
67         if (mark > 0) {
68             
69             // convert hour to adjusted range (0-11)
70
int hour = Integer.parseInt(time.substring(0, mark));
71             if (hour == 12) {
72                 hour = 0;
73             }
74             
75             // find minute value
76
String JavaDoc mins = time.substring(mark+1, time.length()-1);
77             int minute = Integer.parseInt(mins);
78             
79             // check am/pm flag
80
char last = time.charAt(time.length()-1);
81             boolean pm = Character.toLowerCase(last) == 'p';
82             
83             // return minute number of flight time
84
return ((pm ? 12 : 0) + hour)*60 + minute;
85             
86         } else {
87             throw new IllegalArgumentException JavaDoc("Not a valid time: " + time);
88         }
89     }
90     
91     public static String JavaDoc minuteToTime(int minute) {
92         
93         // find hour in quirky am/pm form
94
int hour = minute / 60;
95         boolean pm = false;
96         if (hour >= 12) {
97             hour -= 12;
98             pm = true;
99         }
100         if (hour == 0) {
101             hour = 12;
102         }
103         
104         // convert minute value to double-digit text
105
minute %= 60;
106         String JavaDoc mtext = Integer.toString(minute);
107         if (mtext.length() == 1) {
108             mtext = '0' + mtext;
109         }
110         
111         // return complete time text
112
return Integer.toString(hour) + ':' + mtext + (pm ? 'p' : 'a');
113     }
114 }
115
Popular Tags