From 568475490875982dbd4fe8cd59ba9aaa5406acb2 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Thu, 11 Dec 2003 22:55:25 +0000 Subject: [PATCH] Implement BoundedMap interface git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131429 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/map/FixedSizeMap.java | 16 ++++- .../collections/map/FixedSizeSortedMap.java | 64 ++++++++++++++++--- .../commons/collections/map/LRUMap.java | 9 ++- 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/commons/collections/map/FixedSizeMap.java b/src/java/org/apache/commons/collections/map/FixedSizeMap.java index 33031f37a..ffddd3a8f 100644 --- a/src/java/org/apache/commons/collections/map/FixedSizeMap.java +++ b/src/java/org/apache/commons/collections/map/FixedSizeMap.java @@ -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 @@ -62,6 +62,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +import org.apache.commons.collections.BoundedMap; import org.apache.commons.collections.collection.UnmodifiableCollection; import org.apache.commons.collections.set.UnmodifiableSet; @@ -79,12 +80,13 @@ import org.apache.commons.collections.set.UnmodifiableSet; * is not always unsupported. * * @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 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. @@ -146,5 +148,13 @@ public class FixedSizeMap extends AbstractMapDecorator implements Map { Collection coll = map.values(); return UnmodifiableCollection.decorate(coll); } + + public boolean isFull() { + return true; + } + + public int maxSize() { + return size(); + } } diff --git a/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java b/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java index 4b379e26e..360974049 100644 --- a/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java +++ b/src/java/org/apache/commons/collections/map/FixedSizeSortedMap.java @@ -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 @@ -57,9 +57,16 @@ */ 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 org.apache.commons.collections.BoundedMap; +import org.apache.commons.collections.collection.UnmodifiableCollection; +import org.apache.commons.collections.set.UnmodifiableSet; + /** * Decorates another SortedMap to fix the size blocking add/remove. *

@@ -74,12 +81,13 @@ import java.util.SortedMap; * is not always unsupported. * * @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 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. @@ -112,18 +120,46 @@ public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap { } //----------------------------------------------------------------------- - public Object firstKey() { - return getSortedMap().firstKey(); + public Object put(Object key, Object value) { + 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() { - return getSortedMap().lastKey(); + public void putAll(Map mapToCopy) { + 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() { - return getSortedMap().comparator(); + public void clear() { + 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) { SortedMap map = getSortedMap().subMap(fromKey, toKey); return new FixedSizeSortedMap(map); @@ -139,4 +175,12 @@ public class FixedSizeSortedMap extends FixedSizeMap implements SortedMap { return new FixedSizeSortedMap(map); } + public boolean isFull() { + return true; + } + + public int maxSize() { + return size(); + } + } diff --git a/src/java/org/apache/commons/collections/map/LRUMap.java b/src/java/org/apache/commons/collections/map/LRUMap.java index e9f214107..9eebbafc6 100644 --- a/src/java/org/apache/commons/collections/map/LRUMap.java +++ b/src/java/org/apache/commons/collections/map/LRUMap.java @@ -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 @@ -63,6 +63,8 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Map; +import org.apache.commons.collections.BoundedMap; + /** * A Map implementation with a fixed maximum size which removes * the least recently used entry if an entry is added when full. @@ -81,13 +83,14 @@ import java.util.Map; * ResettableIterator and calling reset(). * * @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 Morgan Delagrange * @author Stephen Colebourne */ -public class LRUMap extends AbstractLinkedMap implements Serializable, Cloneable { +public class LRUMap extends AbstractLinkedMap + implements BoundedMap, Serializable, Cloneable { /** Serialisation version */ static final long serialVersionUID = -612114643488955218L;