Class LoggedFormat<T>

  • All Implemented Interfaces:
    Serializable, Cloneable

    public class LoggedFormat<T>
    extends Format
    Wraps a Format object in order to either parse fully a string, or log a warning. This class provides a parse(java.lang.String) method which performs the following tasks:

    • Checks if the string was fully parsed and log a warning if it was not. This is different than the default parseObject(String) behavior which check only if the begining of the string was parsed and ignore any remaining characters.
    • Ensures that the parsed object is of some specific class specified at construction time.
    • If the string can't be fully parsed or is not of the expected class, logs a warning.
    Since:
    2.4
    Author:
    Martin Desruisseaux
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class Format

        Format.Field
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected LoggedFormat​(Format format, Class<T> type)
      Creates a new format wrapping the specified one.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      StringBuffer format​(Object value, StringBuffer toAppendTo, FieldPosition position)
      Formats the specified object.
      AttributedCharacterIterator formatToCharacterIterator​(Object value)
      Formats the specified object.
      static String formatUnparsable​(String text, int index, int errorIndex, Locale locale)
      Formats an error message for an unparsable string.
      static LogRecord formatUnparsable​(String text, int index, int errorIndex, Locale locale, Level level)
      Formats a log record for an unparsable string.
      static <T> LoggedFormat<T> getInstance​(Format format, Class<T> type)
      Creates a new format wrapping the specified one.
      protected Locale getWarningLocale()
      Returns the locale to use for formatting warnings.
      protected void logWarning​(LogRecord warning)
      Logs a warning.
      T parse​(String text)
      Parses the specified string.
      Object parseObject​(String text)
      Parses text from a string to produce an object.
      Object parseObject​(String text, ParsePosition position)
      Parses text from a string to produce an object.
      void setCaller​(Class<?> caller, String method)
      Sets the source class name and source method name for the warnings eventually emitted by the parse(java.lang.String) method.
      void setLevel​(Level level)
      Sets the logger level for the warnings eventually emitted by the parse(java.lang.String) method.
      void setLogger​(String logger)
      Sets the logger where to send the warnings eventually emitted by the parse(java.lang.String) method.
      String toString()
      Returns a string representation for debugging purpose.
      • Methods inherited from class Format

        clone, format
      • Methods inherited from class Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • LoggedFormat

        protected LoggedFormat​(Format format,
                               Class<T> type)
        Creates a new format wrapping the specified one.
        Parameters:
        format - The format to use for parsing and formatting.
        type - The expected type of parsed values.
    • Method Detail

      • getInstance

        public static <T> LoggedFormat<T> getInstance​(Format format,
                                                      Class<T> type)
        Creates a new format wrapping the specified one.
        Parameters:
        format - The format to use for parsing and formatting.
        type - The expected type of parsed values.
      • setLogger

        public void setLogger​(String logger)
        Sets the logger where to send the warnings eventually emitted by the parse(java.lang.String) method.
        Parameters:
        logger - The logger where to log warnings, or null if none.
      • setLevel

        public void setLevel​(Level level)
        Sets the logger level for the warnings eventually emitted by the parse(java.lang.String) method. The default value is Level.WARNING.
        Parameters:
        level - The new logging level.
        Since:
        2.5
      • setCaller

        public void setCaller​(Class<?> caller,
                              String method)
        Sets the source class name and source method name for the warnings eventually emitted by the parse(java.lang.String) method.
        Parameters:
        caller - The class to declare as the warning emitter, or null if none.
        method - The method to declare as the warning emitter, or null if none.
      • parse

        public T parse​(String text)
        Parses the specified string. If the string can't be parsed, then this method returns null. If it can be parsed at least partially and is of the kind specified at construction time, then it is returned. If the string has not been fully parsed, then a log record is prepared and logged.
        Parameters:
        text - The text to parse, or null.
        Returns:
        The parsed object, or null if text was null or can't be parsed.
      • parseObject

        public Object parseObject​(String text)
                           throws ParseException
        Parses text from a string to produce an object. This method delegates the work to the format specified at construction time. This method to not perform any logging.
        Overrides:
        parseObject in class Format
        Parameters:
        text - The text to parse.
        Returns:
        An object parsed from the string.
        Throws:
        ParseException - if parsing failed.
      • parseObject

        public Object parseObject​(String text,
                                  ParsePosition position)
        Parses text from a string to produce an object. This method delegates the work to the format specified at construction time. This method to not perform any logging.
        Specified by:
        parseObject in class Format
        Parameters:
        text - The text to parse.
        position - Index and error index information.
        Returns:
        An object parsed from the string, or null in case of error.
      • format

        public StringBuffer format​(Object value,
                                   StringBuffer toAppendTo,
                                   FieldPosition position)
        Formats the specified object. This method delegates the work to the format specified at construction time.
        Specified by:
        format in class Format
        Parameters:
        value - The object to format.
        toAppendTo - The buffer where the text is to be appended.
        position - Identifies a field in the formatted text.
        Returns:
        The string buffer passed in with formatted text appended.
      • formatToCharacterIterator

        public AttributedCharacterIterator formatToCharacterIterator​(Object value)
        Formats the specified object. This method delegates the work to the format specified at construction time.
        Overrides:
        formatToCharacterIterator in class Format
        Parameters:
        value - The object to format.
        Returns:
        The character iterator describing the formatted value.
      • logWarning

        protected void logWarning​(LogRecord warning)
        Logs a warning. This method is invoked automatically by the parse method when a text can't be fully parsed. The default implementation logs the warning to the logger specified by the last call to the setLogger method. Subclasses may override this method if they want to change the log record before the logging.
        Parameters:
        warning - The warning to log.
      • getWarningLocale

        protected Locale getWarningLocale()
        Returns the locale to use for formatting warnings. The default implementation returns the default locale.
      • formatUnparsable

        public static String formatUnparsable​(String text,
                                              int index,
                                              int errorIndex,
                                              Locale locale)
        Formats an error message for an unparsable string. This method performs the same work that formatUnparsable(..., Level), except that the result is returned as a String rather than a LogRecord. This is provided as a convenience method for creating the message to give to an exception constructor.
        Parameters:
        text - The unparsable string.
        index - The parse position. This is usually ParsePosition.getIndex().
        errorIndex - The index where the error occured. This is usually ParsePosition.getErrorIndex().
        locale - The locale for the message, or null for the default one.
        Returns:
        A formatted error message.
        Since:
        2.5
      • formatUnparsable

        public static LogRecord formatUnparsable​(String text,
                                                 int index,
                                                 int errorIndex,
                                                 Locale locale,
                                                 Level level)
        Formats a log record for an unparsable string. This method is invoked by the parse method for formatting the log record to be given to the logWarning(java.util.logging.LogRecord) method. It is made public as a convenience for implementors who wish to manage loggings outside this LoggedFormat class.
        Parameters:
        text - The unparsable string.
        index - The parse position. This is usually ParsePosition.getIndex().
        errorIndex - The index where the error occured. This is usually ParsePosition.getErrorIndex().
        locale - The locale for the log message, or null for the default one.
        level - The log record level.
        Returns:
        A formatted log record.
        Since:
        2.5
      • toString

        public String toString()
        Returns a string representation for debugging purpose.
        Overrides:
        toString in class Object