Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

6.3 Auxiliary Interfaces

The auxiliary interfaces are used in conjunction with some of the class methods. The BidirectionalEnumeration interface, for example, is used to visit elements in some collections. The UnaryFunction interface is used to act on elements in some collections, the BinaryFunction interface to combine elements, and the Comparator interface to order elements.

6.3.1 The BidirectionalEnumeration Interface

This interface extends Enumeration to allow you to move both forward and backward within a collection.

An object implementing this interface should always be thought of as positioned between two elements of the collection. A call to nextElement() or previousElement() always returns the element that is passed over by the enumerator on the way to its subsequent position. A call to nextElement() followed immediately by a call to previousElement()returns the same element of the collection.

In addition to the elements() method of the Collection interface mentioned previously, many of the jtools library collection classes have a bidirectionalElements() method that returns a BidirectionalEnumeration.

6.3.2 The BinaryFunction and UnaryFunction Interfaces

The BinaryFunction interface provides for a binary function. Implementing this interface allows an object to act as a functor to use in methods such as Collection.reduce(). Similarly, the UnaryFunction interface provides for a unary function. Implementing this interface allows an object to act as a functor to use in methods such as Collection.apply() and Sequence.transform().

6.3.3 The Comparator Interface

This interface allows an object to act as a comparator for the purpose of ordering elements in sorted collections such as BTree and BinaryTree.

The method compare(Object a, Object b) must return a negative number if a is less than b, 0 if a compares equal to b, and a positive number if a is greater than b.

Two concrete comparator classes, StringCompare and NumericCompare, are included with this package. Class StringCompare, which implements the Comparator interface, is particularly suited for the String, StringBuffer, and Character classes from package java.lang. Comparison of objects is based on their string values; that is, objects are converted to Strings before being compared.

Class NumericCompare implements the Comparator interface for classes inheriting from java.lang.Number, such as Integer, Long, Float and Double. It is important to note that numbers are converted to values of type double before being compared, so this comparator is only suitable when used with values that can be fully represented by a double. Note that not all long values can be represented that way.


Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.