Extractors

The extractor classes defined in the package org.tm4j.topicmaps.utils.extractors all implement the Mango library's UnaryFunction interface. This interface defines a single method, fn() which takes a single parameter and returns an Object. The extractor classes in TM4J encapsulate some of the more common operations on topic map data, and also provide some more advanced operations which can be used to abstract topic map data for presentation in a user interface. Some of the extractor methods allow additional parameters to be specified either by calling additional class methods or by using the BinaryFunction interface to pass the additional parameter directly into the fn() method. Check the individual class documentation for details.

Example 7.2. Using Extractors

The following code sample shows how the TM4J extractor classes can be used together to list all of the role players in an association:

  public void listRolePlayers(Association assoc)
  {
    Iterator members = MembersExtractor.fn(assoc);
    while (members.hasNext())
    {
      Member m = (Member)members.next();
      System.out.println(
        "Member: " + TopicNameExtractor.fn(TypesExtractor.fn(m)));
      Syste.out.println("Players:");
      Iterator players =
        TopicNameExtractor.fn(PlayersExtractor.fn(m)).iterator();

      // Adapt.Method() is part of the Mango library. It creates a 
      // UnaryFunction which invokes the named method on the specified
      // object.
      // ForEach applies the UnaryFunction to each item that the iterator
      // visits.
      // So, the following line prints out the names of each role player

      ForEach.execute(players, Adapt.Method(System.out, "println"));
    }
  }