it.andynaz.utils
Class Utils

java.lang.Object
  extended by it.andynaz.utils.Utils

public abstract class Utils
extends Object

Generic useful methods.

Version:
2013/01/14
Author:
andynaz

Constructor Summary
private Utils()
           
 
Method Summary
static String concat(List l, String separator)
          Concat the elements in a list l, using the string sep.
static String concat(String[] s, String separator)
          Concat the elements in an array s, using the string sep.
static String getInitParam(String paramName, String[] args)
          Returns a paramenter taken from a string array (for instance the one passed to a 'main' method).
static boolean isEmpty(String s)
          Check if a string is empty.
static String pad(String s, String p, int l)
          Pad the string s on the left with the pattern p, till the length of (at least) l.
static Integer parseInt(String s)
          Get an Integer from a string.
static Long parseLong(String s)
          Get a Long from a string.
static void printException(String message, Throwable t)
          Write the log of an exception/error on the standard output, preceed by a custom message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

private Utils()
Method Detail

isEmpty

public static boolean isEmpty(String s)
Check if a string is empty. A String is considered empty if it is null or has a laength of 0.

Parameters:
s - the string to check
Returns:
true if the string is empty, false otherwise

printException

public static void printException(String message,
                                  Throwable t)
Write the log of an exception/error on the standard output, preceed by a custom message.

This method print (in this order):

  1. the custom message
  2. the exception/error's message
  3. the exception/error stack trace

Note: every element is printed only if it is "present", ie it is not null and not empty.

Parameters:
message - the custom message
t - exception or error to be logged

parseInt

public static Integer parseInt(String s)
Get an Integer from a string. If it is not possible to convert the string, null is returned.

Note: this method does not throw any NumberFormatException.

Parameters:
s - a string representing a number
Returns:
the Integer corresponding to the string, or null it does not represent an Integer

parseLong

public static Long parseLong(String s)
Get a Long from a string. If it is not possible to convert the string, null is returned.

Note: this method does not throw any NumberFormatException.

Parameters:
s - a string representing a number
Returns:
the Long corresponding to the string, or null it does not represent a Long

pad

public static String pad(String s,
                         String p,
                         int l)
Pad the string s on the left with the pattern p, till the length of (at least) l.

Note: the returned string can be longer than l.

Parameters:
s - a string to pad
p - pattern to use to pad the string
l - minimun length of the returned string
Throws:
NullPointerException - if at least one of s or p is null.
IllegalArgumentException - il l<0 or the pattern p is empty

concat

public static String concat(String[] s,
                            String separator)
Concat the elements in an array s, using the string sep. If the list is empty, this method returns an empty string (not null). If sep is empty, it concats the elements with no separator.

Parameters:
s - strings to be concated
separator - separator
Throws:
NullPointerException - if s==null

concat

public static String concat(List l,
                            String separator)
Concat the elements in a list l, using the string sep. If the list is empty, this method returns an empty string (not null). If sep is empty, it concats the elements with no separator.

Note: the method accepts a non-generic list because the access to the elements is in read-only mode.

Parameters:
l - a list
separator - a separator
Throws:
NullPointerException - if l==null

getInitParam

public static String getInitParam(String paramName,
                                  String[] args)
Returns a paramenter taken from a string array (for instance the one passed to a 'main' method). The parameter value is considered to be immediatly following the parameter name, as -name value.

The parameter in the array has to be preceed by the character '-'. If there is no match on the parameter name, the method returns null.

If the parameter name is repeated, only the first occourrence is considered. If the paramenter name is the last element of the array, it is not considered (and null is returned).

As an example, in the strings sequence

ab c -or de f
invoking the method as getInitParam("or", args), "de" is returned.

Parameters:
paramName - the name of the parameter
args - the array
Returns:
the value of the parameter, or null if there is no match


(c) 2013 andynaz - http://andynaz.altervista.org