Class ObjectFormat

java.lang.Object
io.sf.jclf.text.format.ObjectFormat
All Implemented Interfaces:
Alignable
Direct Known Subclasses:
NumberFormat, StringFormat, TemplateDateFormat, TextFormat

public abstract class ObjectFormat extends Object implements Alignable
Base class for text formatting objects emulating popular C-Language printf function.

Synopsis:
ObjectFormat of; String formatted; of = ObjectFormat.create("6d"); formatted = of.format(Integer.valueOf(3527)); [...] of = ObjectFormat.create("-15s"); formatted = of.format("This is a test"); [...] of = ObjectFormat.create("-5T"); formatted = of.format("This is another test");

Current supported formats are:

  • d, i, u Integer formats
  • o Octal format
  • x Hexadecimal format
  • f, g, G, e, E floating-point formats
  • s, S text formats
  • T text format
  • D date format

The T text format is an extension that right- or left- justifies a given text. The D format uses a SimpleDateFormat format for a Date object. The usual flags and modifiers can be used, though full compatibility with typical C behavior is not guaranteed.

Subclasses must implement the setFormat method, and can implement its own setFlag method. Only in rare cases will need to override setFlags.

See Also:
  • Field Details

    • flags

      protected char[] flags
    • ralign

      protected boolean ralign
    • width

      protected int width
    • precision

      protected int precision
  • Constructor Details

    • ObjectFormat

      protected ObjectFormat()
  • Method Details

    • create

      public static ObjectFormat create(String format) throws SpecParsingException
      Creates the proper ObjectFormat object according to format.
      Parameters:
      format - the format
      Returns:
      the ObjectFormat object for format.
      Throws:
      SpecParsingException - if the format is not known.
    • create

      public static ObjectFormat create(Class<?> clase, int width, int precision) throws SpecParsingException
      Creates the proper ObjectFormat object according to parameters, with a different default align for text and anything else.
      Parameters:
      clase - the class of the object to be formatted.
      width - see class description.
      precision - see class description.
      Returns:
      the ObjectFormat object
      Throws:
      SpecParsingException - if the class to be formatted is not known.
    • create

      public static ObjectFormat create(Class<?> clase, int width, int precision, boolean right_align) throws SpecParsingException
      Creates the proper ObjectFormat object according to parameters.
      Parameters:
      clase - the class of the object to be formatted.
      width - see class description.
      precision - see class description.
      right_align - true to default right-align text and anything else, false otherwise.
      Returns:
      the ObjectFormat object
      Throws:
      SpecParsingException - if the class to be formatted is not known.
    • setFormat

      protected char setFormat(String format) throws SpecParsingException
      Sets the format of the object.
      Parameters:
      format - the format.
      Returns:
      the format type
      Throws:
      SpecParsingException - if the format contains unknown or wrong format specs.
    • setFlags

      protected void setFlags(char[] flags) throws SpecParsingException
      Throws:
      SpecParsingException
    • setFlag

      protected boolean setFlag(char flag, int i)
      Sets the flags specific to this Class.
      Parameters:
      flag - the flag
      i - the position of the flag in the flags String
      Returns:
      true if the flag was understood, false otherwise.
    • rightAppend

      protected final void rightAppend(StringBuilder sb, String s, char fillchar, int minchars, int maxchars)
    • expandLeftToWidth

      protected final String expandLeftToWidth(String field, char cjust)
      Parameters:
      field - the field to expand
      cjust - the character used to expand (generally ' ')
      Returns:
      the field expanded to current width
    • expandRightToWidth

      protected final String expandRightToWidth(String field)
      Parameters:
      field - the field to expand
      Returns:
      the field expanded to current width
    • align

      public void align(int i)
      Description copied from interface: Alignable
      Sets the alignment of the object.
      Specified by:
      align in interface Alignable
      Parameters:
      i - one of the public constants specifying the alignment.
    • getAlignment

      public int getAlignment()
      Description copied from interface: Alignable
      Gets the alignment.
      Specified by:
      getAlignment in interface Alignable
      Returns:
      the alignment.
    • format

      public abstract String format(Object o)
      Formats an object according to the format specification set by setFormat.

      Must be overriden by subclasses.

      Parameters:
      o - the object to be formatted.
      Returns:
      a string with the formatted object.
    • sample

      public abstract Object sample()
      Produce a sample of this format.
      Returns:
      a String with a (more or less) random object appropriate to be formatted according to current format specifications.