Parser

Provides a mechanism for parsing and rendering string representations of objects.

Specifically, this interface embodies three related capabilities:

  • to parse a string representation and convert to an object.

  • to provide a string representation of the object, for use as its title.

  • to indicate the typical length of such a string representation.

For custom-written (as opposed to third-party) value types, the ability for the Parser to provide a title responsibilities overlap with other conventions for domain objects. Specifically, normally we write a title() method to return a title. In such cases a typical implementation of Parser would just delegate to the value type itself to obtain the title (ie invoking the title() method directly rather than having the framework do this).

For third-party value types, eg seeTime-and-Moneythere is no ability to write title() methods; so this is the main reason that this interface has to deal with titles and lengths.

This interface is used in two complementary ways:

  • As one option, it allows objects to take control of their own parsing, by implementing directly. However, the instance is used as a factory for itself. The framework will instantiate an instance, invoke the appropriate method method, and use the returned object. The instantiated instance itself will be discarded.

Whatever the class that implements this interface, it must also expose either a public no-arg constructor, or (for implementations that also are Facet s) a public constructor that accepts a single FacetHolder . This constructor allows the framework to instantiate the object reflectively.

API

Parser.java
interface Parser<T> {
  String parseableTextRepresentation(ValueSemanticsProvider.Context context, T value)     (1)
  T parseTextRepresentation(ValueSemanticsProvider.Context context, String text)     (2)
  int typicalLength()     (3)
  int maxLength()     (4)
  String getPattern(ValueSemanticsProvider.Context context)
}
1 parseableTextRepresentation(ValueSemanticsProvider_Context, T)

A title for the object that is valid but which may be easier to edit than the title provided by a TitleFacet . (bijective)

2 parseTextRepresentation(ValueSemanticsProvider_Context, String)

Parses a string to an instance of the object. (bijective)

3 typicalLength()

The typical length of objects that can be parsed.

4 maxLength()

The max length of objects that can be parsed (if any). A return type of -1 corresponds to unlimited.

Members

parseableTextRepresentation(ValueSemanticsProvider_Context, T)

A title for the object that is valid but which may be easier to edit than the title provided by a TitleFacet . (bijective)

The idea here is that the viewer can display a parseable title for an existing object when, for example, the user initially clicks in the field. So, a date might be rendered via a TitleFacet as May 2, 2007 , but its editable form might be 20070502 .

parseTextRepresentation(ValueSemanticsProvider_Context, String)

Parses a string to an instance of the object. (bijective)

typicalLength()

The typical length of objects that can be parsed.

maxLength()

The max length of objects that can be parsed (if any). A return type of -1 corresponds to unlimited.