logo
Browsing < A TMAPI Tutorial < < Home 

PrevUpNext

Browsing

At some stage - for instance when dumping the topic content into a user interface - you will want to work through all topics, all their components, or through associations. For all of this TMAPI offers iterators which are created outside a loop and use methods such as hasNext() and next() to visit every element:

Iterator i1 = tMap.getTopics().iterator();
while ( i1.hasNext() ) {
       Topic t = (Topic) i1.next();
       ...
}
Elegantly, as Java is, we have to use a type-casting to Topic. Once we have a topic t in our hands, we might be interested in all the names. Again, we use an iterator for this:
       Iterator i2 = t.getTopicNames().iterator();
       while (i2.hasNext()) {
            TopicName tn = (TopicName) i2.next();
            System.out.print( tn.getValue() ); // not exactly pretty printing
       }
The references the iterator gives us here have to be casted as TopicNames, so that we can access the string value later.

In analogous ways you can drill into associations, occurrences and other parts of the map.

TMAPI also offers more sophisticated ways to retrieve particular information, especially when speed is an issue. So, for instance, to extract all associations of a particular type tMyType from the map one would use

Collection as = idx.getAssociationsByType ( tMyType );
on an index object idx which, obviously, has to be created first:
import org.tmapi.index.core.AssociationIndex;
AssociationIndex 
           idx = (AssociationIndex) tMap.getHelperObject ( AssociationIndex.class );
idx.open();

Collection as  = idx.getAssociationsByType ( tMyType );

idx.close();
Different fast access methods need different kinds of indices, all of which you can find documented in the Guide (no, unfortunately no friendly letters "NO PANIC" at the back).


PrevUpNext