View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.util;
20  
21  import java.util.Calendar;
22  
23  
24  /**
25   *  The <code>Util</code> class implements various useful static methods
26   *  that can be invoked from anywhere.
27   *
28   *  @author Alberto Montresor
29   *  @author Hein Meling
30   *  @since Jgroup 0.1
31   */
32  public final class Util
33  {
34  
35    /**
36     *  Private constructor (Util cannot have instance objects)
37     */
38    private Util() { }
39  
40  
41    /**
42     * Searches the specified array of ints for the specified value using
43     * the sequential search algorithm. The array <strong>must</strong>
44     * be sorted prior to making this call.
45     *
46     * @param array       the array to be searched.
47     * @param key         the value to be searched for.
48     * @return            index of the search key, if it is contained in the array;
49     *                    otherwise, array.length.
50     */
51    public static int valueAt(int[] array, int key)
52    {
53      int         i;                      // Counter
54  
55      for (i=0; i < array.length && array[i] < key; i++);
56      return ((i < array.length && array[i] == key) ? i : -1);
57    }
58  
59    public static int valueAt(Object[] array, Object key)
60    {
61      int         i;                      // Counter
62  
63      try {
64        for(i=0; !array[i].equals(key); i++);
65        return i;
66      } catch (Exception e) {}
67      return -1;
68    }
69  
70    public static int referenceAt(Object[] array, Object key)
71    {
72      int         i;                      // Counter
73  
74      try {
75        for(i=0; array[i] != key; i++);
76        return i;
77      } catch (Exception e) {}
78      return -1;
79    }
80  
81    
82    /**
83     *  Return true if an object equal to <CODE>obj</CODE> is contained in
84     *  <CODE>array</CODE>.
85     */
86    public static boolean in(Object[] array, Object obj)
87    {
88      for (int i=array.length-1; i >=0; i--)
89        if (array[i].equals(obj))
90          return true;
91      return false;
92    }
93  
94  
95    /**
96     *  Return a stringified date value.  The format is as follows:<p>
97     *
98     *  <pre>yyyymmdd</pre>
99     *
100    *  @return A string representation of the current date.
101    */
102   public static String getDateString()
103   {
104     Calendar gc = Calendar.getInstance();
105     StringBuilder buffer = new StringBuilder();
106     buffer.append(Integer.toString(gc.get(Calendar.YEAR)));
107     int month = gc.get(Calendar.MONTH) + 1;
108     if (month < 10)
109       buffer.append("0");
110     buffer.append(Integer.toString(month));
111     int day = gc.get(Calendar.DAY_OF_MONTH);
112     if (day < 10)
113       buffer.append("0");
114     buffer.append(Integer.toString(day));
115     return buffer.toString();
116   }
117 
118 
119   /**
120    *  Return a stringified clock time value.  The format is as follows:<p>
121    *
122    *  <pre>hhmmss</pre>
123    *
124    *  @return A string representation of the current time.
125    */
126   public static String getTimeString()
127   {
128     Calendar gc = Calendar.getInstance();
129     StringBuilder buffer = new StringBuilder();
130     int hour = gc.get(Calendar.HOUR_OF_DAY);
131     if (hour < 10)
132       buffer.append("0");
133     buffer.append(hour);
134     int minute = gc.get(Calendar.MINUTE);
135     if (minute < 10)
136       buffer.append("0");
137     buffer.append(minute);
138     int second = gc.get(Calendar.SECOND);
139     if (second < 10)
140       buffer.append("0");
141     buffer.append(second);
142     return buffer.toString();
143   }
144 
145 
146   /**
147    *  Return a stringified date and clock time value.
148    *  The format is as follows:<p>
149    *
150    *  <pre>yyyymmdd-hhmm</pre>
151    *
152    *  @return A string representation of the current date and time.
153    */
154   public static String getDateTimeString()
155   {
156     Calendar gc = Calendar.getInstance();
157     StringBuilder buffer = new StringBuilder();
158     buffer.append(Integer.toString(gc.get(Calendar.YEAR)));
159     int month = gc.get(Calendar.MONTH) + 1;
160     if (month < 10)
161       buffer.append("0");
162     buffer.append(Integer.toString(month));
163     int day = gc.get(Calendar.DAY_OF_MONTH);
164     if (day < 10)
165       buffer.append("0");
166     buffer.append(Integer.toString(day));
167     buffer.append("-");
168     int hour = gc.get(Calendar.HOUR_OF_DAY);
169     if (hour < 10)
170       buffer.append("0");
171     buffer.append(Integer.toString(hour));
172     int minute = gc.get(Calendar.MINUTE);
173     if (minute < 10)
174       buffer.append("0");
175     buffer.append(Integer.toString(minute));
176     int second = gc.get(Calendar.SECOND);
177     if (second < 10)
178       buffer.append("0");
179     buffer.append(Integer.toString(second));
180     return buffer.toString();
181   }
182 
183 
184   /**
185    *  Converts the given <code>byte</code> value to a string value
186    *  aligned to three characters; it can be used to make message
187    *  outputs print nicely.
188    */
189   public static String byte2str(byte value)
190   {
191     int val = (value & 0xFF);
192     StringBuilder buf = new StringBuilder();
193     if (val < 100) {
194       buf.append("0");
195       if (val < 10)
196         buf.append("0");
197     }
198     buf.append(val);
199     return buf.toString();
200   }
201 
202 
203   /**
204    * Wrapper for the <code>Thread.sleep()</code> method.
205    * 
206    * @param time the time in milliseconds to sleep.
207    */
208   public static void sleep(long time)
209   {
210     try {
211       Thread.sleep(time);
212     } catch (InterruptedException e) {
213       // Ignore interruptions
214     }
215   }
216 
217 } // END Util