Package lazyj

Class Format

java.lang.Object
lazyj.Format

public final class Format extends Object
Utility class providing lots of useful or less useful functions.
Since:
2006-10-13
Author:
costing
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final String
    byteToHex(byte val)
    Get the 2 hexa digit representation of this byte
    static String
    decode(String text)
    Decode an URL-encoded string, using the UTF-8 charset.
    static String
    encode(String text)
    Produce an URL encoding of the given text, using the UTF-8 charset
    static String
    Transform a text into an HTML-safe string.
    static String
    escJS(String text)
    Create a JS-safe string representation.
    static String
    Create a JSON-safe string representation.
    static String
    escSQL(String text)
    Create a SQL-safe version of the given text, to be embedded into SQL queries
    static String
    From a full mail address (eg.
    static String
    From a full mail address (eg.
    static final String
    Put HTML line breaks in the place of normal text line breaks.
    static final String
    Reverse function of the formatBR(String), converts HTML line breaks into text line breaks
    static final char
    hexChar(int val)
    Get the hexa digit for this integer value
    static final Date
    Transform a string that represents a Date into a real Date object.
    static String
    point(double number)
    Show a nice number, with the number of decimal places chosen automatically depending on the number to format
    static String
    replace(String s, String sWhat, String sWith)
    Replace a sequence of text with another sequence in an original string
    static final String
    show0(int i)
    For date formatting, put a "0" in front of a single digit, or leave the number as it is if there are at least two digits already
    static final String
    Show the date and time in a nice human-readable format
    static final String
    Show a date in a dotted manner (dd.MM.yyyy)
    static final String
    showDottedDouble(double d)
    Show the value in 3 digit groups separated by commas, no period point
    static final String
    showDottedDouble(double d, int dotplaces)
    Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point
    static final String
    showDottedDouble(double dbl, int decimals, boolean aproximated)
    Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point.
    static final String
    Show the value in 3 digit groups separated by commas
    static final String
    Show the full month name (11 January 2006)
    static final String
    Show the abreviated month name (11 Jan 2006)
    static final String
    Get a nifty string representation for a date.
    static final String
    Show the time only (12:34)
    static String
    size(double dim)
    Nicely show the size of a file.
    static String
    size(double size, String unit)
    Nicely show a size, starting from a given measurement unit.
    static String
    size(long dim)
    Nicely show the size of a file.
    static String
    size(long size, String unit)
    Nicely show a size, starting from a given measurement unit.
    static final String
    Get the stack trace as String
    static String
    Convert a collection of numbers in a comma-separated list.
    static final String
    toInterval(long lInterval)
    Convert a time in milliseconds to a human readable interval display
    Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.
    toJSON(Object o, boolean newLines)
    Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Format

      public Format()
  • Method Details

    • size

      public static String size(long dim)
      Nicely show the size of a file. Something like 12.1KB, 0.75MB ...
      Parameters:
      dim - file size, in bytes
      Returns:
      the nice human-readable size
      See Also:
    • size

      public static String size(long size, String unit)
      Nicely show a size, starting from a given measurement unit.
      Parameters:
      size - file size, in the specified unit
      unit - unit. Can be nothing(==B) or one of the following: B, K, M, G, T, P
      Returns:
      nice human-readable size
      Since:
      1.0.7
    • size

      public static String size(double dim)
      Nicely show the size of a file. Something like 12.1KB, 0.75MB ...
      Parameters:
      dim - file size, in bytes
      Returns:
      the nice human-readable size
      Since:
      1.0.7
      See Also:
    • size

      public static String size(double size, String unit)
      Nicely show a size, starting from a given measurement unit.
      Parameters:
      size - file size, in the specified unit
      unit - unit. Can be nothing(==B) or one of the following: B, K, M, G, T, P
      Returns:
      nice human-readable size
      Since:
      1.0.5
    • point

      public static String point(double number)
      Show a nice number, with the number of decimal places chosen automatically depending on the number to format
      Parameters:
      number -
      Returns:
      nice floating point number representation
      Since:
      1.0.5
    • extractMailTitle

      public static String extractMailTitle(String address)
      From a full mail address (eg. "Full Name <account@server.com>") try to extract the full name. If it cannot extract the full name it will return the mail address.
      Parameters:
      address - full address
      Returns:
      the name of the person
    • extractAddress

      public static String extractAddress(String address)
      From a full mail address (eg. "Full Name" <account@server.com>) try to extract the email address.
      Parameters:
      address - full address
      Returns:
      the address part or null if there is no email part in it
    • escHtml

      public static String escHtml(String text)
      Transform a text into an HTML-safe string.
      Parameters:
      text - original text
      Returns:
      the HTML-safe version of the text
    • encode

      public static String encode(String text)
      Produce an URL encoding of the given text, using the UTF-8 charset
      Parameters:
      text - text to encode
      Returns:
      URL-safe version of the text
    • decode

      public static String decode(String text)
      Decode an URL-encoded string, using the UTF-8 charset.
      Parameters:
      text - text to decode
      Returns:
      plain text version
    • escSQL

      public static String escSQL(String text)
      Create a SQL-safe version of the given text, to be embedded into SQL queries
      Parameters:
      text - original text
      Returns:
      SQL-safe version of the text
    • escJS

      public static String escJS(String text)
      Create a JS-safe string representation. This is useful when you want to pass a text to a dynamic string variable in the final HTML document.
      Parameters:
      text - original text
      Returns:
      JS-string-safe version
    • escJSON

      public static String escJSON(String text)
      Create a JSON-safe string representation. This is useful when you want to pass a text to a dynamic string variable in the final HTML document.
      Parameters:
      text - original text
      Returns:
      JSON-string-safe version, as per http://json.org/
    • replace

      public static String replace(String s, String sWhat, String sWith)
      Replace a sequence of text with another sequence in an original string
      Parameters:
      s - original text
      sWhat - what to search and replace
      sWith - the new text to put in place
      Returns:
      the modified text
    • formatBR

      public static final String formatBR(String text)
      Put HTML line breaks in the place of normal text line breaks.
      Parameters:
      text - original text
      Returns:
      text with <BR&gr; instead of the \n in the original text
    • formatN

      public static final String formatN(String text)
      Reverse function of the formatBR(String), converts HTML line breaks into text line breaks
      Parameters:
      text - original text
      Returns:
      text with \n instead of <BR>
    • parseDate

      public static final Date parseDate(String s)
      Transform a string that represents a Date into a real Date object. It will try several formats, including date-only and time-only representations.
      Parameters:
      s - string to convert
      Returns:
      date representation, or null if the conversion was not possible.
    • show0

      public static final String show0(int i)
      For date formatting, put a "0" in front of a single digit, or leave the number as it is if there are at least two digits already
      Parameters:
      i - the number to format
      Returns:
      0-padded string representation
    • showNiceDate

      public static final String showNiceDate(Date d)
      Get a nifty string representation for a date. Something like "today", "yesterday", "21 May 2006" ...
      Parameters:
      d - date to convert
      Returns:
      nice human readable date
    • showDate

      public static final String showDate(Date d)
      Show the date and time in a nice human-readable format
      Parameters:
      d - date to represent
      Returns:
      date and time
    • showDottedDate

      public static final String showDottedDate(Date d)
      Show a date in a dotted manner (dd.MM.yyyy)
      Parameters:
      d - date to show
      Returns:
      the dotted date
    • showLongNamedDate

      public static final String showLongNamedDate(Date d)
      Show the full month name (11 January 2006)
      Parameters:
      d - date to show
      Returns:
      date with full month name
    • showNamedDate

      public static final String showNamedDate(Date d)
      Show the abreviated month name (11 Jan 2006)
      Parameters:
      d - date to show
      Returns:
      date with short month name
    • showTime

      public static final String showTime(Date d)
      Show the time only (12:34)
      Parameters:
      d - date to show
      Returns:
      the hours and minutes only
    • showDottedLong

      public static final String showDottedLong(long l)
      Show the value in 3 digit groups separated by commas
      Parameters:
      l - value to show
      Returns:
      nice string representation
    • showDottedDouble

      public static final String showDottedDouble(double d)
      Show the value in 3 digit groups separated by commas, no period point
      Parameters:
      d - value to show
      Returns:
      nice string representation
    • showDottedDouble

      public static final String showDottedDouble(double d, int dotplaces)
      Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point
      Parameters:
      d - value to show
      dotplaces - number of decimal places after the point
      Returns:
      nice string representation
    • showDottedDouble

      public static final String showDottedDouble(double dbl, int decimals, boolean aproximated)
      Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point.
      Parameters:
      dbl - value to show
      decimals - number of decimal places after the point
      aproximated - whether or not to use "millions" and "bilions" for very large numbers
      Returns:
      nice string representation
    • toInterval

      public static final String toInterval(long lInterval)
      Convert a time in milliseconds to a human readable interval display
      Parameters:
      lInterval - interval time in milliseconds
      Returns:
      human readable interval
    • hexChar

      public static final char hexChar(int val)
      Get the hexa digit for this integer value
      Parameters:
      val - value
      Returns:
      hexa digit
    • byteToHex

      public static final String byteToHex(byte val)
      Get the 2 hexa digit representation of this byte
      Parameters:
      val -
      Returns:
      the 2 hexa digit representation
    • stackTraceToString

      public static final String stackTraceToString(Throwable t)
      Get the stack trace as String
      Parameters:
      t - the exception to get the trace for
      Returns:
      the stack trace as String
    • toCommaList

      public static String toCommaList(Collection<Integer> values)
      Convert a collection of numbers in a comma-separated list.
      Parameters:
      values - values to export
      Returns:
      comma-separated list
    • toJSON

      public static Format.JSONFragment toJSON(Object o)
      Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them. The method deals with user classes in a primitive way, simply encapsulating their toString() output in JSON.
      Parameters:
      o - Object to serialize
      Returns:
      the JSON string
    • toJSON

      public static Format.JSONFragment toJSON(Object o, boolean newLines)
      Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them. The method deals with user classes in a primitive way, simply encapsulating their toString() output in JSON.
      Parameters:
      o - Object to serialize
      newLines - whether or not to spread the output in nice, one value per line format, or in compact form
      Returns:
      the JSON string