Testers

The package org.tm4j.topicmap.utils.testers contains a selection of classes which can be used to test whether or not a specific object meets some criteria. Each Tester can be invoked by calling its fn() method with the object to be tested as the only parameter. Some Tester classes require some initialisation (for example the TypesTester must be initialised with the topics or subject indicators for the types which are allowed). All testers implement the UnaryPredicate interface defined in the package uk.co.jezuk.mango. The Mango library provides a number of algorithms and binary functions which can be combined with these testers. For example, you can use the class uk.co.jezuk.mango.unarypredicates.Not in combination with the TypesTester to produce a tester which returns true when the input object type is not one of the types specified to the tester object. Additionally, you can use other classes to apply the tests to collections. For example the class uk.co.jezuk.mango.iterators.PredicatedIterator defines an Iterator object which only iterates over those objects in a collection which match a UnaryPredicate.

Example 7.1. Using The PredicatedIterator class

The following code snippet shows how to use the PredicatedIterator. In this example, the iterator iterates over only roles of a specific type.

TypesTester tester = new TypesTester();
tester.addAllowedType(roleType);
Iterator it = new PredicatedIterator(t.getRolesPlayed().iterator(), 
                                     tester);
// Now use it to iterate over only the roles typed by the topic roleType.

      

Changing the code above to iterate over all roles played except for those of type roleType is as simple as wrapping the tester in a Not function:

TypesTester tester = new TypesTester();
tester.addAllowedType(roleType);
Iterator it = new PredicatedIterator(t.getRolesPlayed().iterator(), 
                                     new NotTester(tester));
// Now can iterate over only the roles not typed by the topic roleType.

      

For more information on what testers are provided with TM4J and what additional parameters each tester takes, please refer to the Javadoc documentation.