Servoy 3.5.x Public API

com.servoy.j2db.util
Class Utils

java.lang.Object
  extended by com.servoy.j2db.util.Utils

public class Utils
extends Object



Normal Use: static methods


Field Summary
static double DEFAULT_EQUALS_PRECISION
           
static float PPI
           
 
Method Summary
static boolean arrayContentEquals(Object[] a, Object[] a2)
           
static Object[] arrayInsert(Object[] src, Object[] toAdd, int position, int n)
          Insert an array into another array at a certain position.
static String arrayToString(Object[] array)
           
static String calculateMD5Hash(String password)
           
static String changeClassNameToFileName(String name)
          Change the passed class name to its corresponding file name.
static String changeFileNameToClassName(String name)
          Change the passed file name to its corresponding class name.
static Connection closeConnection(Connection connection)
           
static InputStream closeInputStream(InputStream is)
           
static OutputStream closeOutputStream(OutputStream os)
           
static Reader closeReader(Reader r)
           
static ResultSet closeResultSet(ResultSet resultSet)
           
static PreparedStatement closeStatement(PreparedStatement statement)
           
static Statement closeStatement(Statement statement)
           
static Writer closeWriter(Writer w)
           
static int compare(double d1, double d2)
           
static int compare(String s1, String s2)
          A String.compareTo() wrapper that will not choke on null values.
static double convertPageFormatUnit(int oldUnit, int newUnit, double value)
           
static String convertToString(Object o)
          Convert to string representation, remove trailing '.0' for numbers.
static int countNumbers(String s)
          count the numbers in a string
static PageFormat createPageFormat(double width, double height, double lm, double rm, double tm, double bm, int orientation, int units)
          Create a PageFormat object from the dimensions and margins.
static byte[] decodeBASE64(String data)
           
static String encodeBASE64(byte[] data)
           
static boolean equalObjects(Object oldObj, Object obj)
           
static boolean equalObjects(Object oldObj, Object obj, double equalsPrecision)
           
static CharSequence escapeMarkup(String s)
          Converts a Java String to an HTML markup string, but does not convert normal spaces to non-breaking space entities (<nbsp>).
static CharSequence escapeMarkup(String s, boolean escapeSpaces)
          Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
static CharSequence escapeMarkup(String s, boolean escapeSpaces, boolean convertToHtmlUnicodeEscapes)
          Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate.
static String findLastNumber(String s)
          hardcore try to find the last number in a string
static String findNumber(String s)
          hardcore try to find a number in a string
static String firstLetterToUpperCase(String string)
           
static String formatNumber(Locale locale, double param, int digits)
          Format a given number (Visitor (locale) specific)
static String formatNumber(Locale locale, double param, String digits)
          Format a given number (Visitor (locale) specific)
static String formatNumber(Locale locale, Object param, String digits)
          Format a given number (Visitor (locale) specific)
static String formatNumber(Locale locale, String param, String digits)
          Format a given number (Visitor (locale) specific)
static String formatTime(int msec, String format)
          Format a given number of miliseconds in a formatted time Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone)
static String formatTime(long msec, String format)
          Format a given number of miliseconds in a formatted time Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone)
static boolean getAsBoolean(Object o)
          Try to parse the given object as a boolean
static boolean getAsBoolean(String s)
          Try to parse the given string as a boolean
static double getAsDouble(Object o)
          Try to parse the given object as a double
static double getAsDouble(Object o, boolean throwOnFail)
          Try to parse the given object as a double
static double getAsDouble(String s)
          Try to parse the given string as a double
static double getAsDouble(String s, boolean throwOnFail)
          Try to parse the given string as a double
static float getAsFloat(Object o)
          Try to parse the given object as a float
static float getAsFloat(Object o, boolean throwOnException)
          Try to parse the given object as a float
static float getAsFloat(String s)
          Try to parse the given string as a float
static float getAsFloat(String s, boolean throwOnException)
          Try to parse the given string as a float
static int getAsInteger(Object o)
          Try to parse the given object as an integer
static int getAsInteger(Object o, boolean throwOnException)
          Try to parse the given object as an integer
static int getAsInteger(String s)
          Try to parse the given string as an integer
static int getAsInteger(String s, boolean throwOnException)
          Try to parse the given string as an integer
static long getAsLong(Object o)
          Try to parse the given object as a long
static long getAsLong(Object o, boolean throwOnException)
          Try to parse the given object as a long
static long getAsLong(String s)
          Try to parse the given string as a long
static long getAsLong(String s, boolean throwOnException)
          Try to parse the given string as a long
static byte[] getFileContent(File f)
           
static String getTXTFileContent(File f)
           
static byte[] getURLContent(String url)
           
static int indexOf(String string, int ch, int escape)
           
static int indexOf(String string, int ch, int escape, int beginIndex)
           
static void invokeLater(com.servoy.j2db.IEventDelegator delegator, List runnables)
           
static boolean isAppleMacOS()
           
static String longToHexString(long n, int digits)
           
static void main(String[] args)
           
static Object mapToNullIfUnmanageble(Object value)
           
static String Matcher_quoteReplacement(String s)
          Borrowed from java.util.regex.Matcher (since jre 1.5).
static Timestamp parseDate(String datetime, String format)
          Create a Date (time) from a String, returns null on failure
Example: Timestamp t = Utils.parseDate("23-06-1975 6:08 AM", "dd-MM-yyyy hh:mm a");
static int readerWriterCopy(Reader is, Writer os)
           
static void releaseConnection(Connection connection)
           
static void rollback(Connection connection)
           
static String[] split(String string, int separator, int escape)
           
static int streamCopy(InputStream is, OutputStream os)
           
static int streamCopy(InputStream is, OutputStream os, ActionListener l, int actionSize)
           
static boolean stringEquals(String left, String right)
          Compares two strings no matter if they are null
static boolean stringIsEmpty(String param)
          tells whether a given String is an empty String
static String stringJoin(Iterator iterator, char separator)
          Joins the elements of the provided Iterator into a single String containing the provided elements.
static String stringJoin(Object[] array, char separator)
           
static String stringReplace(String org, String source, String destination)
           
static String stringReplace(String org, String source, String destination, int replaceOccurence)
           
static String stringReplace(String org, String source, String destination, int replaceOccurence, boolean mustExact, boolean caseInsensitiveSearch)
           
static String stringReplaceCaseInsensitiveSearch(String org, String source, String destination)
           
static String stringReplaceExact(String org, String source, String destination)
          Method for replacing part of a string with a string
static String stringReplaceRecursive(String org, String source, String destination)
           
static CharSequence toMultilineMarkup(CharSequence s)
          Converts a String to multiline HTML markup by replacing newlines with line break entities (<br/>) and multiple occurrences of newline with paragraph break entities (<p>).
static String unescape(String string, int escape)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_EQUALS_PRECISION

public static final double DEFAULT_EQUALS_PRECISION
See Also:
Constant Field Values

PPI

public static final float PPI
See Also:
Constant Field Values
Method Detail

arrayContentEquals

public static boolean arrayContentEquals(Object[] a,
                                         Object[] a2)

arrayInsert

public static Object[] arrayInsert(Object[] src,
                                   Object[] toAdd,
                                   int position,
                                   int n)
Insert an array into another array at a certain position. Both arrays may be null, resulting array will be extended to fit. Element type will be preserved.

Parameters:
src -
toAdd -
position -
n -
Returns:
the resulting array

arrayToString

public static String arrayToString(Object[] array)

calculateMD5Hash

public static String calculateMD5Hash(String password)

changeClassNameToFileName

public static String changeClassNameToFileName(String name)
Change the passed class name to its corresponding file name. E.G. change "Utilities" to "Utilities.class".

Parameters:
name - Class name to be changed.
Throws:
IllegalArgumentException - If a null name passed.

changeFileNameToClassName

public static String changeFileNameToClassName(String name)
Change the passed file name to its corresponding class name. E.G. change "Utilities.class" to "Utilities".

Parameters:
name - Class name to be changed. If this does not represent a Java class then null is returned.
Throws:
IllegalArgumentException - If a null name passed.

closeConnection

public static Connection closeConnection(Connection connection)

closeInputStream

public static InputStream closeInputStream(InputStream is)

closeOutputStream

public static OutputStream closeOutputStream(OutputStream os)

closeReader

public static Reader closeReader(Reader r)

closeResultSet

public static ResultSet closeResultSet(ResultSet resultSet)

closeStatement

public static PreparedStatement closeStatement(PreparedStatement statement)

closeStatement

public static Statement closeStatement(Statement statement)

closeWriter

public static Writer closeWriter(Writer w)

compare

public static int compare(double d1,
                          double d2)

compare

public static int compare(String s1,
                          String s2)
A String.compareTo() wrapper that will not choke on null values. Returns 0 if s1 and s2 are equal, also when they are null.

Parameters:
s1 - left string
s2 - right string
Returns:
the integer comparison result
See Also:
String.compareTo(java.lang.String)

convertPageFormatUnit

public static double convertPageFormatUnit(int oldUnit,
                                           int newUnit,
                                           double value)

convertToString

public static String convertToString(Object o)
Convert to string representation, remove trailing '.0' for numbers.

Returns:

countNumbers

public static int countNumbers(String s)
count the numbers in a string

Parameters:
s - the string with the numbers
Returns:
the count

createPageFormat

public static PageFormat createPageFormat(double width,
                                          double height,
                                          double lm,
                                          double rm,
                                          double tm,
                                          double bm,
                                          int orientation,
                                          int units)
Create a PageFormat object from the dimensions and margins.

Parameters:
width -
height -
lm - left margin
rm - right margin
tm - top margin
bm - bottom margin
orientation -
units -
Returns:
the required PageFormat object

decodeBASE64

public static byte[] decodeBASE64(String data)

encodeBASE64

public static String encodeBASE64(byte[] data)

equalObjects

public static final boolean equalObjects(Object oldObj,
                                         Object obj)

equalObjects

public static final boolean equalObjects(Object oldObj,
                                         Object obj,
                                         double equalsPrecision)

escapeMarkup

public static CharSequence escapeMarkup(String s)
Converts a Java String to an HTML markup string, but does not convert normal spaces to non-breaking space entities (<nbsp>).

Parameters:
s - The string to be escaped
Returns:
The escaped string
See Also:
escapeMarkup(String, boolean)

escapeMarkup

public static CharSequence escapeMarkup(String s,
                                        boolean escapeSpaces)
Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate. Spaces are converted to non-breaking spaces (<nbsp>) if escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are converted to &lt; entities and greater than signs to &gt; entities.

Parameters:
s - The string to escape
escapeSpaces - True to replace ' ' with nonbreaking space
Returns:
The escaped string

escapeMarkup

public static CharSequence escapeMarkup(String s,
                                        boolean escapeSpaces,
                                        boolean convertToHtmlUnicodeEscapes)
Converts a Java String to an HTML markup String by replacing illegal characters with HTML entities where appropriate. Spaces are converted to non-breaking spaces (<nbsp>) if escapeSpaces is true, tabs are converted to four non-breaking spaces, less-than signs are converted to &lt; entities and greater-than signs to &gt; entities.

Parameters:
s - The string to escape
escapeSpaces - True to replace ' ' with nonbreaking space
convertToHtmlUnicodeEscapes - True to convert non-7 bit characters to unicode HTML (&#...)
Returns:
The escaped string

findLastNumber

public static String findLastNumber(String s)
hardcore try to find the last number in a string

Parameters:
s - the string with the number
Returns:
only the digits

findNumber

public static String findNumber(String s)
hardcore try to find a number in a string

Parameters:
s - the string with the number
Returns:
only the digits

firstLetterToUpperCase

public static String firstLetterToUpperCase(String string)

formatNumber

public static String formatNumber(Locale locale,
                                  double param,
                                  int digits)
Format a given number (Visitor (locale) specific)

Parameters:
param - the input number
digits - number of digits
Returns:
the formatted number

formatNumber

public static String formatNumber(Locale locale,
                                  double param,
                                  String digits)
Format a given number (Visitor (locale) specific)

Parameters:
param - the input number
digits - number of digits
Returns:
the formatted number

formatNumber

public static String formatNumber(Locale locale,
                                  Object param,
                                  String digits)
Format a given number (Visitor (locale) specific)

Parameters:
param - the input number
digits - number of digits
Returns:
the formatted number

formatNumber

public static String formatNumber(Locale locale,
                                  String param,
                                  String digits)
Format a given number (Visitor (locale) specific)

Parameters:
param - the input number
digits - number of digits
Returns:
the formatted number

formatTime

public static String formatTime(int msec,
                                String format)
Format a given number of miliseconds in a formatted time Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone)

Parameters:
msec - the miliseconds (current time can be get by 'new java.util.Date().getTime()')
format - the display format (format used from java.text.SimpleDateFormat!)
Returns:
the formatted time
See Also:
SimpleDateFormat

formatTime

public static String formatTime(long msec,
                                String format)
Format a given number of miliseconds in a formatted time Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone)

Parameters:
msec - the miliseconds (current time can be get by 'new java.util.Date().getTime()')
format - the display format
Returns:
the formatted time
See Also:
SimpleDateFormat

getAsBoolean

public static boolean getAsBoolean(Object o)
Try to parse the given object as a boolean

Parameters:
o - the object (Boolean, Number, String, ...) to parse
Returns:
the boolean (false if the parse doesn't succeed)

getAsBoolean

public static boolean getAsBoolean(String s)
Try to parse the given string as a boolean

Parameters:
s - the string to parse
Returns:
the boolean (false if the parse doesn't succeed)

getAsDouble

public static double getAsDouble(Object o)
Try to parse the given object as a double

Parameters:
o - the object (Number, String, ...) to parse
Returns:
the parsed double - or 0 (zero) if the parse doesn't succeed

getAsDouble

public static double getAsDouble(Object o,
                                 boolean throwOnFail)
Try to parse the given object as a double

Parameters:
o - the object (Number, String, ...) to parse
throwOnFail - whether or not to throw a RuntimeException on failure
Returns:
the parsed double - or 0 (zero) if the parse doesn't succeed and throwOnFail is false

getAsDouble

public static double getAsDouble(String s)
Try to parse the given string as a double

Parameters:
s - the string to parse
Returns:
the parsed double - or 0 (zero) if the parse doesn't succeed

getAsDouble

public static double getAsDouble(String s,
                                 boolean throwOnFail)
Try to parse the given string as a double

Parameters:
s - the string to parse
throwOnFail - whether or not to throw a RuntimeException on failure
Returns:
the parsed double - or 0 (zero) if the parse doesn't succeed and throwOnFail is false

getAsFloat

public static float getAsFloat(Object o)
Try to parse the given object as a float

Parameters:
o - the object (Number, String, ...) to parse
Returns:
the parsed float or 0 (zero) if the parse doesn't succeed

getAsFloat

public static float getAsFloat(Object o,
                               boolean throwOnException)
Try to parse the given object as a float

Parameters:
o - the object (Number, String, ...) to parse
throwOnException - whether or not to throw a RuntimeException on failure
Returns:
the parsed float - or 0 (zero) if the parse doesn't succeed and throwOnException is false

getAsFloat

public static float getAsFloat(String s)
Try to parse the given string as a float

Parameters:
s - the string to parse
Returns:
the float - or 0 (zero) if the parse doesn't succeed

getAsFloat

public static float getAsFloat(String s,
                               boolean throwOnException)
Try to parse the given string as a float

Parameters:
s - the string to parse
throwOnException - whether or not to throw a RuntimeException on failure
Returns:
the float - or 0 (zero) if the parse doesn't succeed if throwOnException is false

getAsInteger

public static int getAsInteger(Object o)
Try to parse the given object as an integer

Parameters:
o - the object (Number, String, ...) to parse
Returns:
the parsed integer - or 0 (zero) if the parse doesn't succeed

getAsInteger

public static int getAsInteger(Object o,
                               boolean throwOnException)
Try to parse the given object as an integer

Parameters:
o - the object (Number, String, ...) to parse
throwOnException - whether to throw an exception if parsing failed
Returns:
the parsed integer - or 0 (zero) if the parse doesn't succeed and throwOnException is false

getAsInteger

public static int getAsInteger(String s)
Try to parse the given string as an integer

Parameters:
s - the string to parse
Returns:
the parsed integer - or 0 (zero) if the parse doesn't succeed

getAsInteger

public static int getAsInteger(String s,
                               boolean throwOnException)
Try to parse the given string as an integer

Parameters:
s - the string to parse
throwOnException - whether or not to throw a RuntimeException on failure
Returns:
the parsed integer - or 0 (zero) if the parse doesn't succeed and throwOnException is false
Throws:
RuntimeException - in case of trouble when throwOnException is true

getAsLong

public static long getAsLong(Object o)
Try to parse the given object as a long

Parameters:
o - the object (String, Number, ...) to parse
Returns:
the parsed long - or 0 (zero) if the parse doesn't succeed

getAsLong

public static long getAsLong(Object o,
                             boolean throwOnException)
Try to parse the given object as a long

Parameters:
o - the Object (Number, String, ...) to parse
throwOnException - whether or not to throw a RuntimeException on failure
Returns:
the parsed long - or 0 (zero) if the parse doesn't succeed and throwOnException is false

getAsLong

public static long getAsLong(String s)
Try to parse the given string as a long

Parameters:
s - the string to parse
Returns:
the parsed long - or 0 (zero) if the parse doesn't succeed

getAsLong

public static long getAsLong(String s,
                             boolean throwOnException)
Try to parse the given string as a long

Parameters:
s - the string to parse
throwOnException - whether or not to throw a RuntimeException on failure
Returns:
the parsed long - or 0 (zero) if the parse doesn't succeed and throwOnException is false

getFileContent

public static byte[] getFileContent(File f)

getTXTFileContent

public static String getTXTFileContent(File f)

getURLContent

public static byte[] getURLContent(String url)

indexOf

public static int indexOf(String string,
                          int ch,
                          int escape)

indexOf

public static int indexOf(String string,
                          int ch,
                          int escape,
                          int beginIndex)

invokeLater

public static void invokeLater(com.servoy.j2db.IEventDelegator delegator,
                               List runnables)

isAppleMacOS

public static boolean isAppleMacOS()

longToHexString

public static String longToHexString(long n,
                                     int digits)

main

public static void main(String[] args)

mapToNullIfUnmanageble

public static Object mapToNullIfUnmanageble(Object value)

Matcher_quoteReplacement

public static String Matcher_quoteReplacement(String s)
Borrowed from java.util.regex.Matcher (since jre 1.5). Remove when we no longer have to support jre 1.4.


parseDate

public static Timestamp parseDate(String datetime,
                                  String format)
Create a Date (time) from a String, returns null on failure
Example: Timestamp t = Utils.parseDate("23-06-1975 6:08 AM", "dd-MM-yyyy hh:mm a");

Parameters:
datetime - the date as formatted string
format - the format to be used
Returns:
the Timestamp object
See Also:
SimpleDateFormat

readerWriterCopy

public static int readerWriterCopy(Reader is,
                                   Writer os)
                            throws IOException
Throws:
IOException

releaseConnection

public static void releaseConnection(Connection connection)

rollback

public static void rollback(Connection connection)

split

public static String[] split(String string,
                             int separator,
                             int escape)

streamCopy

public static int streamCopy(InputStream is,
                             OutputStream os)
                      throws IOException
Throws:
IOException

streamCopy

public static int streamCopy(InputStream is,
                             OutputStream os,
                             ActionListener l,
                             int actionSize)
                      throws IOException
Throws:
IOException

stringEquals

public static boolean stringEquals(String left,
                                   String right)
Compares two strings no matter if they are null

Parameters:
left - string
right - string
Returns:
true if they are the same

stringIsEmpty

public static boolean stringIsEmpty(String param)
tells whether a given String is an empty String

Parameters:
param -
Returns:
true if the parameter is null or empty

stringJoin

public static String stringJoin(Iterator iterator,
                                char separator)

Joins the elements of the provided Iterator into a single String containing the provided elements.

No delimiter is added before or after the list. Null objects or empty strings within the iteration are represented by empty strings.

Parameters:
iterator - the Iterator of values to join together, may be null
separator - the separator character to use
Returns:
the joined String, null if null iterator input

stringJoin

public static String stringJoin(Object[] array,
                                char separator)

stringReplace

public static String stringReplace(String org,
                                   String source,
                                   String destination)

stringReplace

public static String stringReplace(String org,
                                   String source,
                                   String destination,
                                   int replaceOccurence)

stringReplace

public static String stringReplace(String org,
                                   String source,
                                   String destination,
                                   int replaceOccurence,
                                   boolean mustExact,
                                   boolean caseInsensitiveSearch)

stringReplaceCaseInsensitiveSearch

public static String stringReplaceCaseInsensitiveSearch(String org,
                                                        String source,
                                                        String destination)

stringReplaceExact

public static String stringReplaceExact(String org,
                                        String source,
                                        String destination)
Method for replacing part of a string with a string

Parameters:
org - the orginal string
source - the string to search
destination - the string to replace
Returns:
the result

stringReplaceRecursive

public static String stringReplaceRecursive(String org,
                                            String source,
                                            String destination)

toMultilineMarkup

public static CharSequence toMultilineMarkup(CharSequence s)
Converts a String to multiline HTML markup by replacing newlines with line break entities (<br/>) and multiple occurrences of newline with paragraph break entities (<p>).

Parameters:
s - String to transform
Returns:
String with all single occurrences of newline replaced with <br/> and all multiple occurrences of newline replaced with <p>.

unescape

public static String unescape(String string,
                              int escape)

Copyright © 2009 Servoy B.V.