Merge pull request #248 from jentfoo/SortedMap_Interface

Added SortedMap interface
This commit is contained in:
Joel Dice 2014-04-29 13:57:15 -06:00
commit 548d70b5e4
2 changed files with 49 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package java.util;
public interface SortedMap<K, V> extends Map<K, V> {
public Comparator<? super K> comparator();
public K firstKey();
public K lastKey();
public SortedMap<K, V> headMap(K toKey);
public SortedMap<K, V> tailMap(K fromKey);
public SortedMap<K, V> subMap(K fromKey, K toKey);
}

View File

@ -14,7 +14,7 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TreeMap<K,V> implements Map<K,V> {
public class TreeMap<K,V> implements SortedMap<K,V> {
private final Comparator<K> comparator;
private transient TreeSet<MyEntry<K,V>> set;
@ -45,6 +45,39 @@ public class TreeMap<K,V> implements Map<K,V> {
return avian.Data.toString(this);
}
@Override
public Comparator<? super K> comparator() {
return comparator;
}
@Override
public K firstKey() {
return set.first().key;
}
@Override
public K lastKey() {
return set.last().key;
}
@Override
public SortedMap<K, V> headMap(K toKey) {
// TODO - this should be implemented, the trick is making the returned SortedMap backed by this TreeSet
throw new UnsupportedOperationException();
}
@Override
public SortedMap<K, V> tailMap(K fromKey) {
// TODO - this should be implemented, the trick is making the returned SortedMap backed by this TreeSet
throw new UnsupportedOperationException();
}
@Override
public SortedMap<K, V> subMap(K fromKey, K toKey) {
// TODO - this should be implemented, the trick is making the returned SortedMap backed by this TreeSet
throw new UnsupportedOperationException();
}
public V get(Object key) {
MyEntry<K,V> e = set.find(new MyEntry(key, null));
return e == null ? null : e.value;