Implement BoundedMap interface

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131429 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-12-11 22:55:25 +00:00
parent 95f17c0843
commit 5684754908
3 changed files with 73 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/FixedSizeMap.java,v 1.1 2003/11/16 00:05:45 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/FixedSizeMap.java,v 1.2 2003/12/11 22:55:25 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -62,6 +62,7 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.collections.BoundedMap;
import org.apache.commons.collections.collection.UnmodifiableCollection; import org.apache.commons.collections.collection.UnmodifiableCollection;
import org.apache.commons.collections.set.UnmodifiableSet; import org.apache.commons.collections.set.UnmodifiableSet;
@ -79,12 +80,13 @@ import org.apache.commons.collections.set.UnmodifiableSet;
* is not always unsupported. * is not always unsupported.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/16 00:05:45 $ * @version $Revision: 1.2 $ $Date: 2003/12/11 22:55:25 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
*/ */
public class FixedSizeMap extends AbstractMapDecorator implements Map { public class FixedSizeMap extends AbstractMapDecorator
implements Map, BoundedMap {
/** /**
* Factory method to create a fixed size map. * Factory method to create a fixed size map.
@ -147,4 +149,12 @@ public class FixedSizeMap extends AbstractMapDecorator implements Map {
return UnmodifiableCollection.decorate(coll); return UnmodifiableCollection.decorate(coll);
} }
public boolean isFull() {
return true;
}
public int maxSize() {
return size();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java,v 1.1 2003/11/16 00:05:45 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java,v 1.2 2003/12/11 22:55:25 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -57,9 +57,16 @@
*/ */
package org.apache.commons.collections.map; package org.apache.commons.collections.map;
import java.util.Comparator; import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap; import java.util.SortedMap;
import org.apache.commons.collections.BoundedMap;
import org.apache.commons.collections.collection.UnmodifiableCollection;
import org.apache.commons.collections.set.UnmodifiableSet;
/** /**
* Decorates another <code>SortedMap</code> to fix the size blocking add/remove. * Decorates another <code>SortedMap</code> to fix the size blocking add/remove.
* <p> * <p>
@ -74,12 +81,13 @@ import java.util.SortedMap;
* is not always unsupported. * is not always unsupported.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/16 00:05:45 $ * @version $Revision: 1.2 $ $Date: 2003/12/11 22:55:25 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
*/ */
public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap { public class FixedSizeSortedMap extends AbstractSortedMapDecorator
implements SortedMap, BoundedMap {
/** /**
* Factory method to create a fixed size sorted map. * Factory method to create a fixed size sorted map.
@ -112,18 +120,46 @@ public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Object firstKey() { public Object put(Object key, Object value) {
return getSortedMap().firstKey(); if (map.containsKey(key) == false) {
throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
}
return map.put(key, value);
} }
public Object lastKey() { public void putAll(Map mapToCopy) {
return getSortedMap().lastKey(); for (Iterator it = mapToCopy.keySet().iterator(); it.hasNext(); ) {
if (mapToCopy.containsKey(it.next()) == false) {
throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
}
}
map.putAll(mapToCopy);
} }
public Comparator comparator() { public void clear() {
return getSortedMap().comparator(); throw new UnsupportedOperationException("Map is fixed size");
} }
public Object remove(Object key) {
throw new UnsupportedOperationException("Map is fixed size");
}
public Set entrySet() {
Set set = map.entrySet();
return UnmodifiableSet.decorate(set);
}
public Set keySet() {
Set set = map.keySet();
return UnmodifiableSet.decorate(set);
}
public Collection values() {
Collection coll = map.values();
return UnmodifiableCollection.decorate(coll);
}
//-----------------------------------------------------------------------
public SortedMap subMap(Object fromKey, Object toKey) { public SortedMap subMap(Object fromKey, Object toKey) {
SortedMap map = getSortedMap().subMap(fromKey, toKey); SortedMap map = getSortedMap().subMap(fromKey, toKey);
return new FixedSizeSortedMap(map); return new FixedSizeSortedMap(map);
@ -139,4 +175,12 @@ public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap {
return new FixedSizeSortedMap(map); return new FixedSizeSortedMap(map);
} }
public boolean isFull() {
return true;
}
public int maxSize() {
return size();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/LRUMap.java,v 1.3 2003/12/11 00:46:12 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/LRUMap.java,v 1.4 2003/12/11 22:55:25 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -63,6 +63,8 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import org.apache.commons.collections.BoundedMap;
/** /**
* A <code>Map</code> implementation with a fixed maximum size which removes * A <code>Map</code> implementation with a fixed maximum size which removes
* the least recently used entry if an entry is added when full. * the least recently used entry if an entry is added when full.
@ -81,13 +83,14 @@ import java.util.Map;
* <code>ResettableIterator</code> and calling <code>reset()</code>. * <code>ResettableIterator</code> and calling <code>reset()</code>.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.3 $ $Date: 2003/12/11 00:46:12 $ * @version $Revision: 1.4 $ $Date: 2003/12/11 22:55:25 $
* *
* @author James Strachan * @author James Strachan
* @author Morgan Delagrange * @author Morgan Delagrange
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public class LRUMap extends AbstractLinkedMap implements Serializable, Cloneable { public class LRUMap extends AbstractLinkedMap
implements BoundedMap, Serializable, Cloneable {
/** Serialisation version */ /** Serialisation version */
static final long serialVersionUID = -612114643488955218L; static final long serialVersionUID = -612114643488955218L;