src/core/net/sf/basedb/util/Values.java

Code
Comments
Other
Rev Date Author Line
2634 12 Sep 06 nicklas 1 /*
2634 12 Sep 06 nicklas 2   $Id$
2634 12 Sep 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
3675 16 Aug 07 jari 5   Copyright (C) 2007 Johan Enell, Nicklas Nordborg
2634 12 Sep 06 nicklas 6
2634 12 Sep 06 nicklas 7   This file is part of BASE - BioArray Software Environment.
2634 12 Sep 06 nicklas 8   Available at http://base.thep.lu.se/
2634 12 Sep 06 nicklas 9
2634 12 Sep 06 nicklas 10   BASE is free software; you can redistribute it and/or
2634 12 Sep 06 nicklas 11   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
2634 12 Sep 06 nicklas 13   of the License, or (at your option) any later version.
2634 12 Sep 06 nicklas 14
2634 12 Sep 06 nicklas 15   BASE is distributed in the hope that it will be useful,
2634 12 Sep 06 nicklas 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
2634 12 Sep 06 nicklas 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2634 12 Sep 06 nicklas 18   GNU General Public License for more details.
2634 12 Sep 06 nicklas 19
2634 12 Sep 06 nicklas 20   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 21   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2634 12 Sep 06 nicklas 22 */
2634 12 Sep 06 nicklas 23 package net.sf.basedb.util;
2634 12 Sep 06 nicklas 24
2634 12 Sep 06 nicklas 25 import java.util.Date;
2634 12 Sep 06 nicklas 26 import java.util.regex.Pattern;
2634 12 Sep 06 nicklas 27 import java.util.regex.Matcher;
2634 12 Sep 06 nicklas 28 import java.util.Collection;
2634 12 Sep 06 nicklas 29
2942 22 Nov 06 nicklas 30 import net.sf.basedb.util.formatter.Formatter;
2942 22 Nov 06 nicklas 31
2634 12 Sep 06 nicklas 32 /**
2634 12 Sep 06 nicklas 33   This class contains a set of static methods that may be useful 
2634 12 Sep 06 nicklas 34   for handling strings, ints and other values.
2634 12 Sep 06 nicklas 35   
2634 12 Sep 06 nicklas 36   @author Nicklas
2634 12 Sep 06 nicklas 37   @version 2.0
2634 12 Sep 06 nicklas 38   @base.modified $Date$
2634 12 Sep 06 nicklas 39 */
2634 12 Sep 06 nicklas 40 public class Values
2634 12 Sep 06 nicklas 41 {
2634 12 Sep 06 nicklas 42
2634 12 Sep 06 nicklas 43   /**
2634 12 Sep 06 nicklas 44     Convert a string to an integer.
2634 12 Sep 06 nicklas 45
2634 12 Sep 06 nicklas 46     @param value The string to convert
2634 12 Sep 06 nicklas 47     @return The converted value, or 0 if the string could not be converted
2634 12 Sep 06 nicklas 48   */
2634 12 Sep 06 nicklas 49   public static final int getInt(String value)
2634 12 Sep 06 nicklas 50   {
2634 12 Sep 06 nicklas 51     return getInt(value, 0);
2634 12 Sep 06 nicklas 52   }
2634 12 Sep 06 nicklas 53
2634 12 Sep 06 nicklas 54   /**
2634 12 Sep 06 nicklas 55     Convert a string to an integer.
2634 12 Sep 06 nicklas 56
2634 12 Sep 06 nicklas 57     @param value The string to convert
2634 12 Sep 06 nicklas 58     @param defaultValue The value to return if the string cannot be converted
2634 12 Sep 06 nicklas 59     @return The converted value or the default value
2634 12 Sep 06 nicklas 60   */
2634 12 Sep 06 nicklas 61   public static final int getInt(String value, int defaultValue)
2634 12 Sep 06 nicklas 62   {
2634 12 Sep 06 nicklas 63     if (value != null)
2634 12 Sep 06 nicklas 64     {
3590 23 Jul 07 nicklas 65       try { return Double.valueOf(value).intValue(); }
2668 27 Sep 06 nicklas 66       catch (Throwable t) {}
2634 12 Sep 06 nicklas 67     }
2634 12 Sep 06 nicklas 68     return defaultValue;
2634 12 Sep 06 nicklas 69   }
2634 12 Sep 06 nicklas 70   
2634 12 Sep 06 nicklas 71   public static final Integer getInteger(String value, Integer defaultValue)
2634 12 Sep 06 nicklas 72   {
2634 12 Sep 06 nicklas 73     if (value != null)
2634 12 Sep 06 nicklas 74     {
3590 23 Jul 07 nicklas 75       try { return Double.valueOf(value).intValue(); }
2668 27 Sep 06 nicklas 76       catch (Throwable t) {}
2634 12 Sep 06 nicklas 77     }
2634 12 Sep 06 nicklas 78     return defaultValue;
2634 12 Sep 06 nicklas 79   }
2634 12 Sep 06 nicklas 80   
2634 12 Sep 06 nicklas 81   /**
2634 12 Sep 06 nicklas 82     Convert an array of strings to an array of Integer:s. Typically used
2634 12 Sep 06 nicklas 83     when you have an array of item id:s and need to use them in a query.
2634 12 Sep 06 nicklas 84     Positions with unconvertable values are skipped.
2634 12 Sep 06 nicklas 85     @param values The array of strings
2634 12 Sep 06 nicklas 86     @return The converted values
2634 12 Sep 06 nicklas 87   */
2634 12 Sep 06 nicklas 88   public static final Integer[] getInt(String[] values)
2634 12 Sep 06 nicklas 89   {
2634 12 Sep 06 nicklas 90     Integer[] result;
2634 12 Sep 06 nicklas 91     if (values != null)
2634 12 Sep 06 nicklas 92     {
2634 12 Sep 06 nicklas 93       Integer[] temp = new Integer[values.length];
2634 12 Sep 06 nicklas 94       int j = 0;
2634 12 Sep 06 nicklas 95       for (String v : values)
2634 12 Sep 06 nicklas 96       {
2634 12 Sep 06 nicklas 97         Integer i = getInteger(v, null);
2634 12 Sep 06 nicklas 98         if (i != null)
2634 12 Sep 06 nicklas 99         {
2634 12 Sep 06 nicklas 100           temp[j++] = i;
2634 12 Sep 06 nicklas 101         }
2634 12 Sep 06 nicklas 102       }
2634 12 Sep 06 nicklas 103       if (j < values.length)
2634 12 Sep 06 nicklas 104       {
2634 12 Sep 06 nicklas 105         result = new Integer[j];
2634 12 Sep 06 nicklas 106         System.arraycopy(temp, 0, result, 0, j);
2634 12 Sep 06 nicklas 107       }
2634 12 Sep 06 nicklas 108       else
2634 12 Sep 06 nicklas 109       {
2634 12 Sep 06 nicklas 110         result = temp;
2634 12 Sep 06 nicklas 111       }
2634 12 Sep 06 nicklas 112     }
2634 12 Sep 06 nicklas 113     else
2634 12 Sep 06 nicklas 114     {
2634 12 Sep 06 nicklas 115       result = new Integer[0];
2634 12 Sep 06 nicklas 116     }
2634 12 Sep 06 nicklas 117     return result;
2634 12 Sep 06 nicklas 118   }
2634 12 Sep 06 nicklas 119
2634 12 Sep 06 nicklas 120   /**
2634 12 Sep 06 nicklas 121     Convert a string to a long.
2634 12 Sep 06 nicklas 122
2634 12 Sep 06 nicklas 123     @param value The string to convert
2634 12 Sep 06 nicklas 124     @return The converted value, or 0 if the string could not be converted
2634 12 Sep 06 nicklas 125   */
2634 12 Sep 06 nicklas 126   public static final long getLong(String value)
2634 12 Sep 06 nicklas 127   {
2634 12 Sep 06 nicklas 128     return getLong(value, 0);
2634 12 Sep 06 nicklas 129   }
2634 12 Sep 06 nicklas 130   
2634 12 Sep 06 nicklas 131   /**
2634 12 Sep 06 nicklas 132     Convert a string to an long.
2634 12 Sep 06 nicklas 133
2634 12 Sep 06 nicklas 134     @param value The string to convert
2634 12 Sep 06 nicklas 135     @param defaultValue The value to return if the string cannot be converted
2634 12 Sep 06 nicklas 136     @return The converted value or the default value
2634 12 Sep 06 nicklas 137   */
2634 12 Sep 06 nicklas 138   public static final long getLong(String value, long defaultValue)
2634 12 Sep 06 nicklas 139   {
2634 12 Sep 06 nicklas 140     if (value != null)
2634 12 Sep 06 nicklas 141     {
2634 12 Sep 06 nicklas 142       try { return Long.parseLong(value); }
2634 12 Sep 06 nicklas 143       catch (Exception ex) {}
2634 12 Sep 06 nicklas 144     }
2634 12 Sep 06 nicklas 145     return defaultValue;
2634 12 Sep 06 nicklas 146   }
2634 12 Sep 06 nicklas 147
2634 12 Sep 06 nicklas 148   /**
2634 12 Sep 06 nicklas 149     Convert a string to an long.
2634 12 Sep 06 nicklas 150
2634 12 Sep 06 nicklas 151     @param value The string to convert
2634 12 Sep 06 nicklas 152     @param defaultValue The value to return if the string cannot be converted
2634 12 Sep 06 nicklas 153     @return The converted value or the default value
2634 12 Sep 06 nicklas 154   */
2634 12 Sep 06 nicklas 155   public static final Long getLong(String value, Long defaultValue)
2634 12 Sep 06 nicklas 156   {
2634 12 Sep 06 nicklas 157     if (value != null)
2634 12 Sep 06 nicklas 158     {
2634 12 Sep 06 nicklas 159       try { return Long.parseLong(value); }
2634 12 Sep 06 nicklas 160       catch (Exception ex) {}
2634 12 Sep 06 nicklas 161     }
2634 12 Sep 06 nicklas 162     return defaultValue;
2634 12 Sep 06 nicklas 163   }
2634 12 Sep 06 nicklas 164   
2634 12 Sep 06 nicklas 165   /**
2634 12 Sep 06 nicklas 166     Convert an array of strings to an array of Long:s.
2634 12 Sep 06 nicklas 167     Positions with unconvertable values are skipped.
2634 12 Sep 06 nicklas 168     @param values The array of strings
2634 12 Sep 06 nicklas 169     @return The converted values
2634 12 Sep 06 nicklas 170   */
2634 12 Sep 06 nicklas 171   public static final Long[] getLong(String[] values)
2634 12 Sep 06 nicklas 172   {
2634 12 Sep 06 nicklas 173     Long[] result;
2634 12 Sep 06 nicklas 174     if (values != null)
2634 12 Sep 06 nicklas 175     {
2634 12 Sep 06 nicklas 176       Long[] temp = new Long[values.length];
2634 12 Sep 06 nicklas 177       int j = 0;
2634 12 Sep 06 nicklas 178       for (String v : values)
2634 12 Sep 06 nicklas 179       {
2634 12 Sep 06 nicklas 180         Long i = getLong(v, null);
2634 12 Sep 06 nicklas 181         if (i != null)
2634 12 Sep 06 nicklas 182         {
2634 12 Sep 06 nicklas 183           temp[j++] = i;
2634 12 Sep 06 nicklas 184         }
2634 12 Sep 06 nicklas 185       }
2634 12 Sep 06 nicklas 186       if (j < values.length)
2634 12 Sep 06 nicklas 187       {
2634 12 Sep 06 nicklas 188         result = new Long[j];
2634 12 Sep 06 nicklas 189         System.arraycopy(temp, 0, result, 0, j);
2634 12 Sep 06 nicklas 190       }
2634 12 Sep 06 nicklas 191       else
2634 12 Sep 06 nicklas 192       {
2634 12 Sep 06 nicklas 193         result = temp;
2634 12 Sep 06 nicklas 194       }
2634 12 Sep 06 nicklas 195     }
2634 12 Sep 06 nicklas 196     else
2634 12 Sep 06 nicklas 197     {
2634 12 Sep 06 nicklas 198       result = new Long[0];
2634 12 Sep 06 nicklas 199     }
2634 12 Sep 06 nicklas 200     return result;
2634 12 Sep 06 nicklas 201   }
2634 12 Sep 06 nicklas 202
2634 12 Sep 06 nicklas 203   
2634 12 Sep 06 nicklas 204   /**
2634 12 Sep 06 nicklas 205     Convert a string to an float.
2634 12 Sep 06 nicklas 206
2634 12 Sep 06 nicklas 207     @param value The string to convert
2634 12 Sep 06 nicklas 208     @return The converted value, or 0 if the string could not be converted
2634 12 Sep 06 nicklas 209   */
2634 12 Sep 06 nicklas 210   public static final float getFloat(String value)
2634 12 Sep 06 nicklas 211   {
2634 12 Sep 06 nicklas 212     return getFloat(value, 0);
2634 12 Sep 06 nicklas 213   }
2634 12 Sep 06 nicklas 214
2634 12 Sep 06 nicklas 215   /**
2634 12 Sep 06 nicklas 216     Convert a string to an integer.
2634 12 Sep 06 nicklas 217
2634 12 Sep 06 nicklas 218     @param value The string to convert
2634 12 Sep 06 nicklas 219     @param defaultValue The value to return if the string cannot be converted
2634 12 Sep 06 nicklas 220     @return The converted value or the default value
2634 12 Sep 06 nicklas 221   */
2634 12 Sep 06 nicklas 222   public static final float getFloat(String value, float defaultValue)
2634 12 Sep 06 nicklas 223   {
2634 12 Sep 06 nicklas 224     if (value != null)
2634 12 Sep 06 nicklas 225     {
2634 12 Sep 06 nicklas 226       try { return Float.parseFloat(value); }
2634 12 Sep 06 nicklas 227       catch (Exception ex) {}
2634 12 Sep 06 nicklas 228     }
2634 12 Sep 06 nicklas 229     return defaultValue;
2634 12 Sep 06 nicklas 230   }
2634 12 Sep 06 nicklas 231
2634 12 Sep 06 nicklas 232   public static final Float getFloat(String value, Float defaultValue)
2634 12 Sep 06 nicklas 233   {
2634 12 Sep 06 nicklas 234     if (value != null)
2634 12 Sep 06 nicklas 235     {
2634 12 Sep 06 nicklas 236       try { return Float.parseFloat(value); }
2634 12 Sep 06 nicklas 237       catch (Exception ex) {}
2634 12 Sep 06 nicklas 238     }
2634 12 Sep 06 nicklas 239     return defaultValue;
2634 12 Sep 06 nicklas 240   }
2634 12 Sep 06 nicklas 241
2634 12 Sep 06 nicklas 242   /**
2634 12 Sep 06 nicklas 243     Convert an array of strings to an array of Float:s. 
2634 12 Sep 06 nicklas 244     @param values The array of strings
2634 12 Sep 06 nicklas 245     @return The converted values
2634 12 Sep 06 nicklas 246   */
2634 12 Sep 06 nicklas 247   public static final Float[] getFloat(String[] values)
2634 12 Sep 06 nicklas 248   {
2634 12 Sep 06 nicklas 249     Float[] result;
2634 12 Sep 06 nicklas 250     if (values != null)
2634 12 Sep 06 nicklas 251     {
2634 12 Sep 06 nicklas 252       Float[] temp = new Float[values.length];
2634 12 Sep 06 nicklas 253       int j = 0;
2634 12 Sep 06 nicklas 254       for (String v : values)
2634 12 Sep 06 nicklas 255       {
2634 12 Sep 06 nicklas 256         Float i = getFloat(v, null);
2634 12 Sep 06 nicklas 257         if (i != null)
2634 12 Sep 06 nicklas 258         {
2634 12 Sep 06 nicklas 259           temp[j++] = i;
2634 12 Sep 06 nicklas 260         }
2634 12 Sep 06 nicklas 261       }
2634 12 Sep 06 nicklas 262       if (j < values.length)
2634 12 Sep 06 nicklas 263       {
2634 12 Sep 06 nicklas 264         result = new Float[j];
2634 12 Sep 06 nicklas 265         System.arraycopy(temp, 0, result, 0, j);
2634 12 Sep 06 nicklas 266       }
2634 12 Sep 06 nicklas 267       else
2634 12 Sep 06 nicklas 268       {
2634 12 Sep 06 nicklas 269         result = temp;
2634 12 Sep 06 nicklas 270       }
2634 12 Sep 06 nicklas 271     }
2634 12 Sep 06 nicklas 272     else
2634 12 Sep 06 nicklas 273     {
2634 12 Sep 06 nicklas 274       result = new Float[0];
2634 12 Sep 06 nicklas 275     }
2634 12 Sep 06 nicklas 276     return result;
2634 12 Sep 06 nicklas 277   }
2634 12 Sep 06 nicklas 278
2634 12 Sep 06 nicklas 279   /**
2634 12 Sep 06 nicklas 280     Convert a string to a double.
2634 12 Sep 06 nicklas 281
2634 12 Sep 06 nicklas 282     @param value The string to convert
2634 12 Sep 06 nicklas 283     @return The converted value, or 0 if the string could not be converted
2634 12 Sep 06 nicklas 284   */
2634 12 Sep 06 nicklas 285   public static final double getDouble(String value)
2634 12 Sep 06 nicklas 286   {
2634 12 Sep 06 nicklas 287     return getDouble(value, 0);
2634 12 Sep 06 nicklas 288   }
2634 12 Sep 06 nicklas 289
2634 12 Sep 06 nicklas 290   /**
2634 12 Sep 06 nicklas 291     Convert a string to a double.
2634 12 Sep 06 nicklas 292
2634 12 Sep 06 nicklas 293     @param value The string to convert
2634 12 Sep 06 nicklas 294     @param defaultValue The value to return if the string cannot be converted
2634 12 Sep 06 nicklas 295     @return The converted value or the default value
2634 12 Sep 06 nicklas 296   */
2634 12 Sep 06 nicklas 297   public static final double getDouble(String value, double defaultValue)
2634 12 Sep 06 nicklas 298   {
2634 12 Sep 06 nicklas 299     if (value != null)
2634 12 Sep 06 nicklas 300     {
2634 12 Sep 06 nicklas 301       try { return Double.parseDouble(value); }
2634 12 Sep 06 nicklas 302       catch (Exception ex) {}
2634 12 Sep 06 nicklas 303     }
2634 12 Sep 06 nicklas 304     return defaultValue;
2634 12 Sep 06 nicklas 305   }
2634 12 Sep 06 nicklas 306
2634 12 Sep 06 nicklas 307   public static final Double getDouble(String value, Double defaultValue)
2634 12 Sep 06 nicklas 308   {
2634 12 Sep 06 nicklas 309     if (value != null)
2634 12 Sep 06 nicklas 310     {
2634 12 Sep 06 nicklas 311       try { return Double.parseDouble(value); }
2634 12 Sep 06 nicklas 312       catch (Exception ex) {}
2634 12 Sep 06 nicklas 313     }
2634 12 Sep 06 nicklas 314     return defaultValue;
2634 12 Sep 06 nicklas 315   }
2634 12 Sep 06 nicklas 316
2634 12 Sep 06 nicklas 317   /**
2634 12 Sep 06 nicklas 318     Convert an array of strings to an array of Double:s. 
2634 12 Sep 06 nicklas 319     @param values The array of strings
2634 12 Sep 06 nicklas 320     @return The converted values
2634 12 Sep 06 nicklas 321   */
2634 12 Sep 06 nicklas 322   public static final Double[] getDouble(String[] values)
2634 12 Sep 06 nicklas 323   {
2634 12 Sep 06 nicklas 324     Double[] result;
2634 12 Sep 06 nicklas 325     if (values != null)
2634 12 Sep 06 nicklas 326     {
2634 12 Sep 06 nicklas 327       Double[] temp = new Double[values.length];
2634 12 Sep 06 nicklas 328       int j = 0;
2634 12 Sep 06 nicklas 329       for (String v : values)
2634 12 Sep 06 nicklas 330       {
2634 12 Sep 06 nicklas 331         Double i = getDouble(v, null);
2634 12 Sep 06 nicklas 332         if (i != null)
2634 12 Sep 06 nicklas 333         {
2634 12 Sep 06 nicklas 334           temp[j++] = i;
2634 12 Sep 06 nicklas 335         }
2634 12 Sep 06 nicklas 336       }
2634 12 Sep 06 nicklas 337       if (j < values.length)
2634 12 Sep 06 nicklas 338       {
2634 12 Sep 06 nicklas 339         result = new Double[j];
2634 12 Sep 06 nicklas 340         System.arraycopy(temp, 0, result, 0, j);
2634 12 Sep 06 nicklas 341       }
2634 12 Sep 06 nicklas 342       else
2634 12 Sep 06 nicklas 343       {
2634 12 Sep 06 nicklas 344         result = temp;
2634 12 Sep 06 nicklas 345       }
2634 12 Sep 06 nicklas 346     }
2634 12 Sep 06 nicklas 347     else
2634 12 Sep 06 nicklas 348     {
2634 12 Sep 06 nicklas 349       result = new Double[0];
2634 12 Sep 06 nicklas 350     }
2634 12 Sep 06 nicklas 351     return result;
2634 12 Sep 06 nicklas 352   }
2634 12 Sep 06 nicklas 353   /**
2634 12 Sep 06 nicklas 354     Convert a string to a string, and map NULL to the empty string.
2634 12 Sep 06 nicklas 355     @param value The string to convert
2634 12 Sep 06 nicklas 356     @return The original string, or the empty string ("") if the original is null
2634 12 Sep 06 nicklas 357   */
2634 12 Sep 06 nicklas 358   public static final String getString(String value)
2634 12 Sep 06 nicklas 359   {
2634 12 Sep 06 nicklas 360     return getString(value, "");
2634 12 Sep 06 nicklas 361   }
2634 12 Sep 06 nicklas 362
2634 12 Sep 06 nicklas 363   /**
2634 12 Sep 06 nicklas 364     Convert a string to a string.
2634 12 Sep 06 nicklas 365
2634 12 Sep 06 nicklas 366     @param value The string to convert
2634 12 Sep 06 nicklas 367     @param defaultValue The value to return if the string is null
2634 12 Sep 06 nicklas 368     @return The original string, or the default value if the original is null
2634 12 Sep 06 nicklas 369   */
2634 12 Sep 06 nicklas 370   public static final String getString(String value, String defaultValue)
2634 12 Sep 06 nicklas 371   {
2634 12 Sep 06 nicklas 372     return value == null ? defaultValue : value;
2634 12 Sep 06 nicklas 373   }
2634 12 Sep 06 nicklas 374   
2634 12 Sep 06 nicklas 375   /**
2634 12 Sep 06 nicklas 376     Convert the empty string ("") to null after removing leading and trailing
2634 12 Sep 06 nicklas 377     whitespace.
2634 12 Sep 06 nicklas 378     
2634 12 Sep 06 nicklas 379     @param value The string to convert
2634 12 Sep 06 nicklas 380     @return The original string with leading and trailing whitespace has been
2634 12 Sep 06 nicklas 381       removed or null if the value is the empty string ("")
2634 12 Sep 06 nicklas 382   */
2634 12 Sep 06 nicklas 383   public static final String getStringOrNull(String value)
2634 12 Sep 06 nicklas 384   {
2634 12 Sep 06 nicklas 385     if (value != null) value = value.trim();
2634 12 Sep 06 nicklas 386     return "".equals(value) ? null : value;
2634 12 Sep 06 nicklas 387   }
2634 12 Sep 06 nicklas 388   
2634 12 Sep 06 nicklas 389   public static final String getString(Collection<?> values, String deliminator, boolean skipNull)
2634 12 Sep 06 nicklas 390   {
2942 22 Nov 06 nicklas 391     return getString(values, deliminator, skipNull, null);
2942 22 Nov 06 nicklas 392   }
2942 22 Nov 06 nicklas 393   
4925 08 May 09 nicklas 394   public static final <T> String getString(Collection<T> values, String deliminator, boolean skipNull, Formatter<? super T> formatter)
2942 22 Nov 06 nicklas 395   {
2634 12 Sep 06 nicklas 396     StringBuilder sb = new StringBuilder();
2634 12 Sep 06 nicklas 397     boolean firstElement = true;
2634 12 Sep 06 nicklas 398     if (values != null)
2634 12 Sep 06 nicklas 399     {
2942 22 Nov 06 nicklas 400       for (T value : values)
2634 12 Sep 06 nicklas 401       {
2634 12 Sep 06 nicklas 402         if (value == null)
2634 12 Sep 06 nicklas 403         {
2634 12 Sep 06 nicklas 404           if (!skipNull)
2634 12 Sep 06 nicklas 405           {
2634 12 Sep 06 nicklas 406             if (!firstElement) sb.append(deliminator);
2634 12 Sep 06 nicklas 407             firstElement = false;
2634 12 Sep 06 nicklas 408           }
2634 12 Sep 06 nicklas 409         }
2634 12 Sep 06 nicklas 410         else
2634 12 Sep 06 nicklas 411         {
2942 22 Nov 06 nicklas 412           String s;
2634 12 Sep 06 nicklas 413           if (!firstElement) sb.append(deliminator);
2942 22 Nov 06 nicklas 414           if (formatter != null)
2942 22 Nov 06 nicklas 415           {
2942 22 Nov 06 nicklas 416             s = formatter.format(value);
2942 22 Nov 06 nicklas 417           }
2942 22 Nov 06 nicklas 418           else 
2942 22 Nov 06 nicklas 419           {
2942 22 Nov 06 nicklas 420             s = value.toString();
2942 22 Nov 06 nicklas 421           }
2942 22 Nov 06 nicklas 422           sb.append(s);
2634 12 Sep 06 nicklas 423           firstElement = false;
2634 12 Sep 06 nicklas 424         }
2634 12 Sep 06 nicklas 425       }
2634 12 Sep 06 nicklas 426     }
2942 22 Nov 06 nicklas 427     return sb.toString();    
2634 12 Sep 06 nicklas 428   }
2634 12 Sep 06 nicklas 429   
2634 12 Sep 06 nicklas 430   /**
3973 16 Nov 07 nicklas 431     Convert a collection of objects to an array of strings. If a formatter
3973 16 Nov 07 nicklas 432     is specified, the {@link Formatter#format(Object)} method is used, otherwise
3973 16 Nov 07 nicklas 433     {@link Object#toString()}.
4034 05 Dec 07 martin 434      @param values Collection to be converted. 
4034 05 Dec 07 martin 435      @return A String array
3973 16 Nov 07 nicklas 436   */
3973 16 Nov 07 nicklas 437   public static final <T> String[] toStrings(Collection<T> values, Formatter<T> formatter)
3973 16 Nov 07 nicklas 438   {
3973 16 Nov 07 nicklas 439     String[] strings = null;
3973 16 Nov 07 nicklas 440     if (values != null)
3973 16 Nov 07 nicklas 441     {
3973 16 Nov 07 nicklas 442       strings = new String[values.size()];
3973 16 Nov 07 nicklas 443       int index = 0;
3973 16 Nov 07 nicklas 444       for (T value : values)
3973 16 Nov 07 nicklas 445       {
3973 16 Nov 07 nicklas 446         if (value == null)
3973 16 Nov 07 nicklas 447         {
3973 16 Nov 07 nicklas 448           strings[index] = null;
3973 16 Nov 07 nicklas 449         }
3973 16 Nov 07 nicklas 450         else
3973 16 Nov 07 nicklas 451         {
3973 16 Nov 07 nicklas 452           strings[index] = formatter == null ? value.toString() : formatter.format(value);
3973 16 Nov 07 nicklas 453         }
3973 16 Nov 07 nicklas 454         ++index;
3973 16 Nov 07 nicklas 455       }
3973 16 Nov 07 nicklas 456     }
3973 16 Nov 07 nicklas 457     return strings;    
3973 16 Nov 07 nicklas 458   }  
3973 16 Nov 07 nicklas 459   
3973 16 Nov 07 nicklas 460   /**
2634 12 Sep 06 nicklas 461     Trims long strings to a maximum of <code>maxLength</code>
2634 12 Sep 06 nicklas 462     characters.
2634 12 Sep 06 nicklas 463     @param value The string to trim
4034 05 Dec 07 martin 464      @return A string or null if value is null.
2634 12 Sep 06 nicklas 465   */
2634 12 Sep 06 nicklas 466   public static final String trimString(String value, int maxLength)
2634 12 Sep 06 nicklas 467   {
2634 12 Sep 06 nicklas 468     if (value == null) return null;
2634 12 Sep 06 nicklas 469     if (value.length() > maxLength) 
2634 12 Sep 06 nicklas 470     {
2634 12 Sep 06 nicklas 471       StringBuilder sb = new StringBuilder(maxLength+3);
2634 12 Sep 06 nicklas 472       sb.append(value.substring(0, maxLength-1));
2634 12 Sep 06 nicklas 473       sb.append("...");
2634 12 Sep 06 nicklas 474       value = sb.toString();
2634 12 Sep 06 nicklas 475     }
2634 12 Sep 06 nicklas 476     return value;
2634 12 Sep 06 nicklas 477   }
2634 12 Sep 06 nicklas 478
2634 12 Sep 06 nicklas 479   /**
2634 12 Sep 06 nicklas 480     Convert a string to a boolean. '0', 'no', 'false', '' and the null string are
4347 18 Jun 08 nicklas 481     converted to FALSE, all other values are converted to TRUE. Case is ignored.
2634 12 Sep 06 nicklas 482     @param value The value to convert
2634 12 Sep 06 nicklas 483     @return TRUE or FALSE
2634 12 Sep 06 nicklas 484   */
2634 12 Sep 06 nicklas 485   public static final boolean getBoolean(String value)
2634 12 Sep 06 nicklas 486   {
2634 12 Sep 06 nicklas 487     return (value == null
2634 12 Sep 06 nicklas 488       || value.equals("")
2634 12 Sep 06 nicklas 489       || value.equals("0")
4347 18 Jun 08 nicklas 490       || value.equalsIgnoreCase("no")
4347 18 Jun 08 nicklas 491       || value.equalsIgnoreCase("false")
2634 12 Sep 06 nicklas 492       ) ? false : true;
2634 12 Sep 06 nicklas 493   }
2634 12 Sep 06 nicklas 494   
2634 12 Sep 06 nicklas 495   /**
2634 12 Sep 06 nicklas 496     Convert a string to a boolean. If the string is null, the default
2634 12 Sep 06 nicklas 497     value is returned. '0', 'no', 'false' and '' are
2634 12 Sep 06 nicklas 498     converted to FALSE, all other values are converted to TRUE.
2634 12 Sep 06 nicklas 499     @param value The value to convert
2634 12 Sep 06 nicklas 500     @param defaultValue The value to return if the string is null
2634 12 Sep 06 nicklas 501     @return TRUE or FALSE
2634 12 Sep 06 nicklas 502   */
2634 12 Sep 06 nicklas 503   public static final boolean getBoolean(String value, boolean defaultValue)
2634 12 Sep 06 nicklas 504   {
2634 12 Sep 06 nicklas 505     return value == null ? defaultValue : getBoolean(value);
2634 12 Sep 06 nicklas 506   }
2634 12 Sep 06 nicklas 507
2942 22 Nov 06 nicklas 508   public static final Date[] getDate(String[] values, Formatter<Date> dateFormatter)
2942 22 Nov 06 nicklas 509   {
2942 22 Nov 06 nicklas 510     Date[] result;
2942 22 Nov 06 nicklas 511     if (values != null)
2942 22 Nov 06 nicklas 512     {
2942 22 Nov 06 nicklas 513       Date[] temp = new Date[values.length];
2942 22 Nov 06 nicklas 514       int j = 0;
2942 22 Nov 06 nicklas 515       for (String v : values)
2942 22 Nov 06 nicklas 516       {
2942 22 Nov 06 nicklas 517         Date i = dateFormatter.parseString(v);
2942 22 Nov 06 nicklas 518         if (i != null)
2942 22 Nov 06 nicklas 519         {
2942 22 Nov 06 nicklas 520           temp[j++] = i;
2942 22 Nov 06 nicklas 521         }
2942 22 Nov 06 nicklas 522       }
2942 22 Nov 06 nicklas 523       if (j < values.length)
2942 22 Nov 06 nicklas 524       {
2942 22 Nov 06 nicklas 525         result = new Date[j];
2942 22 Nov 06 nicklas 526         System.arraycopy(temp, 0, result, 0, j);
2942 22 Nov 06 nicklas 527       }
2942 22 Nov 06 nicklas 528       else
2942 22 Nov 06 nicklas 529       {
2942 22 Nov 06 nicklas 530         result = temp;
2942 22 Nov 06 nicklas 531       }
2942 22 Nov 06 nicklas 532     }
2942 22 Nov 06 nicklas 533     else
2942 22 Nov 06 nicklas 534     {
2942 22 Nov 06 nicklas 535       result = new Date[0];
2942 22 Nov 06 nicklas 536     }
2942 22 Nov 06 nicklas 537     return result;
2942 22 Nov 06 nicklas 538   }
2942 22 Nov 06 nicklas 539   
2634 12 Sep 06 nicklas 540   /**
2634 12 Sep 06 nicklas 541     Formats a decimal number with the specified number of decimals.
2634 12 Sep 06 nicklas 542
2634 12 Sep 06 nicklas 543     @param number The number to be formatted
2634 12 Sep 06 nicklas 544     @param decimals The number of decimals to display, use a negative value to
2634 12 Sep 06 nicklas 545       display all decimals
2634 12 Sep 06 nicklas 546     @return A string with the formatted number, or an empty string
2634 12 Sep 06 nicklas 547       if the number is null
2634 12 Sep 06 nicklas 548   */
2634 12 Sep 06 nicklas 549   public static final String formatNumber(Float number, int decimals)
2634 12 Sep 06 nicklas 550   {
2634 12 Sep 06 nicklas 551     return formatNumber(number, decimals, null);
2634 12 Sep 06 nicklas 552   }
2634 12 Sep 06 nicklas 553   
2634 12 Sep 06 nicklas 554   /**
2634 12 Sep 06 nicklas 555     Formats a decimal number with the specified number of decimals
2634 12 Sep 06 nicklas 556     and optionally adding a unit.
2634 12 Sep 06 nicklas 557
2634 12 Sep 06 nicklas 558     @param number The number to be formatted
2634 12 Sep 06 nicklas 559     @param decimals The number of decimals to display, use a negative value to
2634 12 Sep 06 nicklas 560       display all decimals
2634 12 Sep 06 nicklas 561     @param unit A string that is added to the end of the formatted number, or
2634 12 Sep 06 nicklas 562       null to not add anything
2634 12 Sep 06 nicklas 563     @return A string with the formatted number, or an empty string
2634 12 Sep 06 nicklas 564       if the number is null
2634 12 Sep 06 nicklas 565   */
2634 12 Sep 06 nicklas 566   public static final String formatNumber(Float number, int decimals, String unit)
2634 12 Sep 06 nicklas 567   {
2634 12 Sep 06 nicklas 568     if (number == null || number.isNaN()) return "";
2752 20 Oct 06 nicklas 569     double fNumber = Math.abs(number.doubleValue());
2634 12 Sep 06 nicklas 570     double exp = Math.pow(10, decimals);
2752 20 Oct 06 nicklas 571     StringBuilder result = new StringBuilder();
2752 20 Oct 06 nicklas 572     if (number < 0) result.append("-");
2634 12 Sep 06 nicklas 573     if (decimals == 0)
2634 12 Sep 06 nicklas 574     {
2752 20 Oct 06 nicklas 575       result.append(String.valueOf(Math.round(fNumber)));
2634 12 Sep 06 nicklas 576     }
2634 12 Sep 06 nicklas 577     else if (decimals < 0)
2634 12 Sep 06 nicklas 578     {
2752 20 Oct 06 nicklas 579       result.append(fNumber);
2634 12 Sep 06 nicklas 580     }
2634 12 Sep 06 nicklas 581     else
2634 12 Sep 06 nicklas 582     {
2752 20 Oct 06 nicklas 583       long lNumber = (long)fNumber;
4232 17 Apr 08 nicklas 584       long remain = Math.round(fNumber*exp-lNumber*exp);
4232 17 Apr 08 nicklas 585       if (remain >= exp) 
4232 17 Apr 08 nicklas 586       {
4232 17 Apr 08 nicklas 587         remain -= exp;
4232 17 Apr 08 nicklas 588         lNumber++;
4232 17 Apr 08 nicklas 589       }
4232 17 Apr 08 nicklas 590       String theDecimals = Long.toString(remain); 
2752 20 Oct 06 nicklas 591       result.append(lNumber).append(".");
4284 09 May 08 nicklas 592       for (int i = theDecimals.length(); i < decimals; ++i)
2752 20 Oct 06 nicklas 593       {
4284 09 May 08 nicklas 594         result.append('0');
2752 20 Oct 06 nicklas 595       }
4284 09 May 08 nicklas 596       result.append(theDecimals);
2634 12 Sep 06 nicklas 597     }
2752 20 Oct 06 nicklas 598     if (unit != null) result.append(unit);
2752 20 Oct 06 nicklas 599     return result.toString();
2634 12 Sep 06 nicklas 600   }
2634 12 Sep 06 nicklas 601
7168 08 Jun 16 nicklas 602   public static final long GB = 1073741824;
7168 08 Jun 16 nicklas 603   public static final long MB = 1048576;
7168 08 Jun 16 nicklas 604   public static final long kB = 1024;
2634 12 Sep 06 nicklas 605   
2634 12 Sep 06 nicklas 606   /**
2634 12 Sep 06 nicklas 607     Formats a value using units of bytes, kilobytes, megabytes or gigabytes.
2634 12 Sep 06 nicklas 608
2634 12 Sep 06 nicklas 609     @param bytes The value to format
2634 12 Sep 06 nicklas 610     @return A string with a decimal value follwed by bytes, Kb, Mb or Gb
2634 12 Sep 06 nicklas 611       or an empty string if the bytes value is null
2634 12 Sep 06 nicklas 612   */
2634 12 Sep 06 nicklas 613   public static final String formatBytes(Long bytes)
2634 12 Sep 06 nicklas 614   {
4232 17 Apr 08 nicklas 615     return formatBytes(bytes, 1);
4232 17 Apr 08 nicklas 616   }
4232 17 Apr 08 nicklas 617   
4232 17 Apr 08 nicklas 618   /**
4232 17 Apr 08 nicklas 619     Formats a value using units of bytes, kilobytes, megabytes or gigabytes.
5326 29 Apr 10 nicklas 620     @param bytes The value to format, -1 = unknown
4232 17 Apr 08 nicklas 621     @param decimals The number of decimals to display in KB, MB, and GB values
4232 17 Apr 08 nicklas 622     @since 2.6.2
4232 17 Apr 08 nicklas 623   */
4232 17 Apr 08 nicklas 624   public static final String formatBytes(Long bytes, int decimals)
4232 17 Apr 08 nicklas 625   {
2634 12 Sep 06 nicklas 626     if (bytes == null) return "";
2634 12 Sep 06 nicklas 627     if (bytes >= GB)
2634 12 Sep 06 nicklas 628     {
2634 12 Sep 06 nicklas 629       float gb = (float)bytes / GB;
4232 17 Apr 08 nicklas 630       return formatNumber(gb, decimals)+" GB";
2634 12 Sep 06 nicklas 631     }
2634 12 Sep 06 nicklas 632     else if (bytes >= MB)
2634 12 Sep 06 nicklas 633     {
2634 12 Sep 06 nicklas 634       float mb = (float)bytes / MB;
4232 17 Apr 08 nicklas 635       return formatNumber(mb, decimals)+" MB";
2634 12 Sep 06 nicklas 636     }
2634 12 Sep 06 nicklas 637     else if (bytes >= kB)
2634 12 Sep 06 nicklas 638     {
2634 12 Sep 06 nicklas 639       float kb = (float)bytes / kB;
4232 17 Apr 08 nicklas 640       return formatNumber(kb, decimals)+" kB";
2634 12 Sep 06 nicklas 641     }
5326 29 Apr 10 nicklas 642     else if (bytes >= 0)
2634 12 Sep 06 nicklas 643     {
2634 12 Sep 06 nicklas 644       return bytes + " bytes";
2634 12 Sep 06 nicklas 645     }
5326 29 Apr 10 nicklas 646     else
5326 29 Apr 10 nicklas 647     {
5326 29 Apr 10 nicklas 648       return "unknown";
5326 29 Apr 10 nicklas 649     }
2634 12 Sep 06 nicklas 650   }
2634 12 Sep 06 nicklas 651   
2634 12 Sep 06 nicklas 652   private static final Pattern BYTES_REGEXP = Pattern.compile("(\\d+\\.?\\d*)\\s*(G|M|K)?(b|bytes)?", Pattern.CASE_INSENSITIVE);
2634 12 Sep 06 nicklas 653
2634 12 Sep 06 nicklas 654   /**
2634 12 Sep 06 nicklas 655     Parses a string containing the number of bytes formatted as
2981 30 Nov 06 nicklas 656     the {@link #formatBytes(Long)} method.
2634 12 Sep 06 nicklas 657     @param fBytes The string containing the value to parse
2634 12 Sep 06 nicklas 658     @param defaultValue This value is returned if the string cannot be parsed
4034 05 Dec 07 martin 659      @return Bytes as a Long object. 
2634 12 Sep 06 nicklas 660   */
2634 12 Sep 06 nicklas 661   public static final Long parseBytes(String fBytes, Long defaultValue)
2634 12 Sep 06 nicklas 662   {
2634 12 Sep 06 nicklas 663     if (fBytes == null) return defaultValue;
2634 12 Sep 06 nicklas 664     Long bytes = defaultValue;
2634 12 Sep 06 nicklas 665     Matcher m = BYTES_REGEXP.matcher(fBytes);
2634 12 Sep 06 nicklas 666     if (m.matches())
2634 12 Sep 06 nicklas 667     {
2634 12 Sep 06 nicklas 668       double f = Double.parseDouble(m.group(1));
2634 12 Sep 06 nicklas 669       String unit = m.group(2);
2634 12 Sep 06 nicklas 670       if ("G".equalsIgnoreCase(unit))
2634 12 Sep 06 nicklas 671       {
2634 12 Sep 06 nicklas 672         bytes = (long)(f * GB);
2634 12 Sep 06 nicklas 673       }
2634 12 Sep 06 nicklas 674       else if ("M".equalsIgnoreCase(unit))
2634 12 Sep 06 nicklas 675       {
2634 12 Sep 06 nicklas 676         bytes = (long)(f * MB);
2634 12 Sep 06 nicklas 677       }
2634 12 Sep 06 nicklas 678       else if ("K".equalsIgnoreCase(unit))
2634 12 Sep 06 nicklas 679       {
2634 12 Sep 06 nicklas 680         bytes = (long)(f * kB);
2634 12 Sep 06 nicklas 681       }
2634 12 Sep 06 nicklas 682       else
2634 12 Sep 06 nicklas 683       {
2634 12 Sep 06 nicklas 684         bytes = (long)(f);
2634 12 Sep 06 nicklas 685       }
2634 12 Sep 06 nicklas 686     }
2634 12 Sep 06 nicklas 687     return bytes;
2634 12 Sep 06 nicklas 688   }
2634 12 Sep 06 nicklas 689
2634 12 Sep 06 nicklas 690   /**
2634 12 Sep 06 nicklas 691     Formats a number of seconds to a more proper time format. Example:
6898 12 May 15 nicklas 692     7925 -&gt; 2 hours 12 minutes 5 seconds
2634 12 Sep 06 nicklas 693
2634 12 Sep 06 nicklas 694     @param seconds The number of seconds
2634 12 Sep 06 nicklas 695     @return The formatted string
2634 12 Sep 06 nicklas 696   */
2634 12 Sep 06 nicklas 697   public static final String formatTime(long seconds)
2634 12 Sep 06 nicklas 698   {
2634 12 Sep 06 nicklas 699     StringBuilder sb = new StringBuilder();
5384 13 Aug 10 nicklas 700     if (seconds >= 86400L)
2634 12 Sep 06 nicklas 701     {
5384 13 Aug 10 nicklas 702       long days = seconds / 86400;
5384 13 Aug 10 nicklas 703       seconds = seconds - 86400L*days;
2634 12 Sep 06 nicklas 704       sb.append(days);
2634 12 Sep 06 nicklas 705       sb.append(days == 1 ? " day " : " days ");
2634 12 Sep 06 nicklas 706     }
5384 13 Aug 10 nicklas 707     if (seconds >= 3600L)
2634 12 Sep 06 nicklas 708     {
5384 13 Aug 10 nicklas 709       long hours = seconds / 3600;
5384 13 Aug 10 nicklas 710       seconds = seconds - 3600L*hours;
2634 12 Sep 06 nicklas 711       sb.append(hours);
2634 12 Sep 06 nicklas 712       sb.append(hours == 1 ? " hour " : " hours ");
2634 12 Sep 06 nicklas 713     }
5384 13 Aug 10 nicklas 714     if (seconds >= 60L)
2634 12 Sep 06 nicklas 715     {
5384 13 Aug 10 nicklas 716       long minutes = seconds / 60;
5384 13 Aug 10 nicklas 717       seconds = seconds - 60L*minutes;
2634 12 Sep 06 nicklas 718       sb.append(minutes);
2634 12 Sep 06 nicklas 719       sb.append(minutes == 1 ? " minute " : " minutes ");
2634 12 Sep 06 nicklas 720     }
2634 12 Sep 06 nicklas 721     sb.append(seconds);
2634 12 Sep 06 nicklas 722     sb.append(seconds == 1 ? " second" : " seconds");
2634 12 Sep 06 nicklas 723     return sb.toString();
2634 12 Sep 06 nicklas 724   }
2634 12 Sep 06 nicklas 725
2634 12 Sep 06 nicklas 726   /**
2634 12 Sep 06 nicklas 727     Insert the values of parameters into a template message.
2634 12 Sep 06 nicklas 728     @param message The message template, with placeholders of the
2634 12 Sep 06 nicklas 729       form {1}, {2}, etc. that will be replaced with the values
2634 12 Sep 06 nicklas 730       of the parameters
2634 12 Sep 06 nicklas 731     @param parameters The parameters values that will replace the placeholders
2634 12 Sep 06 nicklas 732       in the template message
2634 12 Sep 06 nicklas 733     @return The message with the placeholders replaced by the parameters
2634 12 Sep 06 nicklas 734   */
2634 12 Sep 06 nicklas 735   public static final String formatMessage(String message, Object... parameters)
2634 12 Sep 06 nicklas 736   {
2634 12 Sep 06 nicklas 737     if ((parameters != null) && (parameters.length > 0))
2634 12 Sep 06 nicklas 738     {
2634 12 Sep 06 nicklas 739       for (int i = parameters.length; i > 0; i--)
2634 12 Sep 06 nicklas 740       {
2634 12 Sep 06 nicklas 741         message = message.replaceAll("\\{"+i+"\\}", "<span class=\"parameter\">"+parameters[i-1].toString()+"</span>");
2634 12 Sep 06 nicklas 742       }
2634 12 Sep 06 nicklas 743     }
2634 12 Sep 06 nicklas 744     return message;
2634 12 Sep 06 nicklas 745   }
2643 15 Sep 06 nicklas 746   
2643 15 Sep 06 nicklas 747   /**
2643 15 Sep 06 nicklas 748     Trim a string to a maximum length. If the input string is longer than
2643 15 Sep 06 nicklas 749     maxLength the string is cut of at the end and ... is added to it.
4034 05 Dec 07 martin 750      @param s String to be trimmed. 
4034 05 Dec 07 martin 751      @param maxLength The maximum length the string is allowed to be.
4034 05 Dec 07 martin 752      @return A string or null if s is null.
2643 15 Sep 06 nicklas 753   */
2643 15 Sep 06 nicklas 754   public static String trim(String s, int maxLength)
2643 15 Sep 06 nicklas 755   {
2643 15 Sep 06 nicklas 756     if (s != null && s.length() > maxLength)
2643 15 Sep 06 nicklas 757     {
2643 15 Sep 06 nicklas 758       s = s.substring(0, maxLength - 3) + "...";
2643 15 Sep 06 nicklas 759     }
2643 15 Sep 06 nicklas 760     return s;
2643 15 Sep 06 nicklas 761   }
2643 15 Sep 06 nicklas 762   
2634 12 Sep 06 nicklas 763 }
2634 12 Sep 06 nicklas 764