package weiss.util; /** * Balanced search tree implementation of the Map. */ public class TreeMap extends MapImpl { /** * Construct an empty TreeMap with default comparator. */ public TreeMap( ) { super( new TreeSet>( ) ); } /** * Construct a TreeMap using comparator. * @param comparator the comparator. */ public TreeMap( final Comparator comparator ) { super( new TreeSet>( ) ); keyCmp = comparator; } /** * Construct a TreeMap with same key/value pairs * and comparator as another map.. * @param other the other map. */ public TreeMap( Map other ) { super( other ); } /** * Gets the comparator; returns null if default. * @return the comparator or if null if default is used. */ public Comparator comparator( ) { return keyCmp; } protected Map.Entry makePair( KeyType key, ValueType value ) { return new Pair( key, value ); } protected Set makeEmptyKeySet( ) { return new TreeSet( keyCmp ); } protected Set> clonePairSet( Set> pairSet ) { return new TreeSet>( pairSet ); } private final class Pair extends MapImpl.Pair implements Comparable> { public Pair( KeyType k, ValueType v ) { super( k, v ); } public int compareTo( Map.Entry other ) { if( keyCmp != null ) return keyCmp.compare( getKey( ), other.getKey( ) ); else return (( Comparable) getKey( ) ).compareTo( other.getKey( ) ); } } private Comparator keyCmp; }