类 SentenceFactory

java.lang.Object
net.sf.marineapi.nmea.parser.SentenceFactory

public class SentenceFactory extends Object
Factory for creating sentence parsers.

Custom parsers may be implemented and registered in the factory at runtime by following these steps:

  1. Define a sentence interface by extending the Sentence interface (e.g. com.acme.XYZSentence).
  2. Implement the interface in a class that extends SentenceParser, (e.g. com.acme.XYZParser).
  3. Use the protected getters and setters in SentenceParser to read and write sentence data.
  4. Add a constructor in XYZParser with String parameter, i.e. the sentence to be parsed. Pass this parameter to SentenceParser(String, String) with expected sentence type (e.g. "XYZ").
  5. Add another constructor with TalkerId parameter. Pass this parameter to SentenceParser(net.sf.marineapi.nmea.sentence.TalkerId, String, int) with sentence type and the expected number of data fields.
  6. Register XYZParser in SentenceFactory by using the registerParser(String, Class) method.
  7. Use createParser(String) or createParser(net.sf.marineapi.nmea.sentence.TalkerId, String) to obtain an instance of your parser. In addition, SentenceReader will now dispatch instances of XYZSentence when "XYZ" sentences are read from the data source.

Notice that there is no need to compile the whole library and the added parser source code may be located in your own codebase. Additionally, it is also possible to override any existing parsers of the library as needed.

作者:
Kimmo Tuukkanen, Gunnar Hillert
  • 方法详细资料

    • createParser

      public Sentence createParser(String nmea)
      Creates a parser for specified NMEA 0183 sentence String. The parser implementation is selected from registered parsers according to sentence type. The returned instance must be cast in to correct sentence interface, for which the type should first be checked by using the Sentence.getSentenceId() method.
      参数:
      nmea - NMEA 0183 sentence String
      返回:
      Sentence parser instance for specified sentence
      抛出:
      IllegalArgumentException - If there is no parser registered for the given sentence type
      IllegalStateException - If parser is found, but it does not implement expected constructors or is otherwise unusable.
    • createParser

      public Sentence createParser(TalkerId talker, SentenceId type)
      Creates a parser for specified talker and sentence type. The returned instance needs to be cast to corresponding sentence interface.
      参数:
      talker - Sentence talker id
      type - Sentence type
      返回:
      Sentence parser of requested type.
      抛出:
      IllegalArgumentException - If talker id is null or if there is no parser registered for given sentence type.
      IllegalStateException - If parser instantiation fails.
    • createParser

      public Sentence createParser(TalkerId talker, String type)
      Creates a parser for specified talker and sentence type. This method is mainly intended to be used when custom parsers have been registered in the factory. The returned instance needs to be cast to corresponding sentence interface.
      参数:
      talker - Talker ID to use in parser
      type - Type of the parser to create
      返回:
      Sentence parser for requested type
      抛出:
      IllegalArgumentException - If talker id is null or if there is no parser registered for given sentence type.
      IllegalStateException - If parser is found, but it does not implement expected constructors or is otherwise unusable.
    • hasParser

      public boolean hasParser(String type)
      Tells if the factory is able to create parser for specified sentence type. All SentenceId enum values should result returning true at all times.
      参数:
      type - Sentence type id, e.g. "GLL" or "GGA".
      返回:
      true if type is supported, otherwise false.
    • listParsers

      public List<String> listParsers()
      Returns a list of currently parseable sentence types.
      返回:
      List of sentence ids
    • registerParser

      public void registerParser(String type, Class<? extends SentenceParser> parser)
      Registers a sentence parser to the factory. After registration, createParser(String) method can be used to obtain instances of registered parser.

      Sentences supported by the library are registered automatically, but they can be overridden simply be registering a new parser implementation for chosen sentence type. That is, each sentence type can have only one parser registered at a time.

      参数:
      type - Sentence type id, e.g. "GGA" or "GLL".
      parser - Class of parser implementation for given type.
    • unregisterParser

      public void unregisterParser(Class<? extends SentenceParser> parser)
      Unregisters a parser class, regardless of sentence type(s) it is registered for.
      参数:
      parser - Parser implementation class for type.
      另请参阅:
    • getInstance

      public static SentenceFactory getInstance()
      Returns the singleton instance of SentenceFactory.
      返回:
      SentenceFactory instance
    • reset

      public void reset()
      Resets the factory in it's initial state, i.e. restores and removes all parsers the have been either removed or added.