You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/10/15 14:28:15 UTC
svn commit: r321321 - in /jakarta/commons/proper/collections/trunk:
RELEASE-NOTES.html
src/java/org/apache/commons/collections/map/ListOrderedMap.java
src/test/org/apache/commons/collections/map/TestListOrderedMap.java
Author: scolebourne
Date: Sat Oct 15 05:28:08 2005
New Revision: 321321
URL: http://svn.apache.org/viewcvs?rev=321321&view=rev
Log:
values can now be accessed as a List using valueList()
additional list-like method, setValue(int,Object)
rfe 37015
Modified:
jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java
Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html?rev=321321&r1=321320&r2=321321&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original)
+++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Sat Oct 15 05:28:08 2005
@@ -71,6 +71,8 @@
<li>BlockingBuffer - now includes stack trace if InterupttedException occurs [33700]</li>
<li>BlockingBuffer - new methods that allow get and remove with a timeout [27691]</li>
<li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li>
+<li>ListOrderedMap - values can now be accessed as a List using valueList() [37015]</li>
+<li>ListOrderedMap - additional list-like method, setValue(int,Object)</li>
<li>PriorityBuffer - now Serializable [36163]</li>
</ul>
Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java?rev=321321&r1=321320&r2=321321&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java Sat Oct 15 05:28:08 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2003-2004 The Apache Software Foundation
+ * Copyright 2003-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
-import java.util.AbstractCollection;
+import java.util.AbstractList;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
@@ -228,18 +228,72 @@
}
//-----------------------------------------------------------------------
+ /**
+ * Gets a view over the keys in the map.
+ * <p>
+ * The Collection will be ordered by object insertion into the map.
+ *
+ * @see #keyList()
+ * @return the fully modifiable collection view over the keys
+ */
public Set keySet() {
return new KeySetView(this);
}
+ /**
+ * Gets a view over the keys in the map as a List.
+ * <p>
+ * The List will be ordered by object insertion into the map.
+ * The List is unmodifiable.
+ *
+ * @see #keySet()
+ * @return the unmodifiable list view over the keys
+ * @since Commons Collections 3.2
+ */
+ public List keyList() {
+ return UnmodifiableList.decorate(insertOrder);
+ }
+
+ /**
+ * Gets a view over the values in the map.
+ * <p>
+ * The Collection will be ordered by object insertion into the map.
+ * <p>
+ * From Commons Collections 3.2, this Collection can be cast
+ * to a list, see {@link #valueList()}
+ *
+ * @see #valueList()
+ * @return the fully modifiable collection view over the values
+ */
public Collection values() {
return new ValuesView(this);
}
+ /**
+ * Gets a view over the values in the map as a List.
+ * <p>
+ * The List will be ordered by object insertion into the map.
+ * The List supports remove and set, but does not support add.
+ *
+ * @see #values()
+ * @return the partially modifiable list view over the values
+ * @since Commons Collections 3.2
+ */
+ public List valueList() {
+ return new ValuesView(this);
+ }
+
+ /**
+ * Gets a view over the entries in the map.
+ * <p>
+ * The Set will be ordered by object insertion into the map.
+ *
+ * @return the fully modifiable set view over the entries
+ */
public Set entrySet() {
return new EntrySetView(this, this.insertOrder);
}
-
+
//-----------------------------------------------------------------------
/**
* Returns the Map as a string.
@@ -305,11 +359,23 @@
}
/**
+ * Sets the value at the specified index.
+ *
+ * @param index the index of the value to set
+ * @return the previous value at that index
+ * @throws IndexOutOfBoundsException if the index is invalid
+ * @since Commons Collections 3.2
+ */
+ public Object setValue(int index, Object value) {
+ Object key = insertOrder.get(index);
+ return put(key, value);
+ }
+
+ /**
* Removes the element at the specified index.
*
* @param index the index of the object to remove
- * @return the previous value corresponding the <code>key</code>,
- * or <code>null</code> if none existed
+ * @return the removed value, or <code>null</code> if none existed
* @throws IndexOutOfBoundsException if the index is invalid
*/
public Object remove(int index) {
@@ -326,17 +392,19 @@
* value of a list. This occurs because changing the key, changes when the
* mapping is added to the map and thus where it appears in the list.
* <p>
- * An alternative to this method is to use {@link #keySet()}.
+ * An alternative to this method is to use the better named
+ * {@link #keyList()} or {@link #keySet()}.
*
+ * @see #keyList()
* @see #keySet()
* @return The ordered list of keys.
*/
public List asList() {
- return UnmodifiableList.decorate(insertOrder);
+ return keyList();
}
//-----------------------------------------------------------------------
- static class ValuesView extends AbstractCollection {
+ static class ValuesView extends AbstractList {
private final ListOrderedMap parent;
ValuesView(ListOrderedMap parent) {
@@ -363,8 +431,20 @@
}
};
}
+
+ public Object get(int index) {
+ return this.parent.getValue(index);
+ }
+
+ public Object set(int index, Object value) {
+ return this.parent.setValue(index, value);
+ }
+
+ public Object remove(int index) {
+ return this.parent.remove(index);
+ }
}
-
+
//-----------------------------------------------------------------------
static class KeySetView extends AbstractSet {
private final ListOrderedMap parent;
Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java?rev=321321&r1=321320&r2=321321&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestListOrderedMap.java Sat Oct 15 05:28:08 2005
@@ -1,5 +1,5 @@
/*
- * Copyright 2001-2004 The Apache Software Foundation
+ * Copyright 2001-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -123,6 +123,33 @@
}
}
+ public void testSetValueByIndex() {
+ resetEmpty();
+ ListOrderedMap lom = (ListOrderedMap) map;
+ try {
+ lom.setValue(0, "");
+ } catch (IndexOutOfBoundsException ex) {}
+ try {
+ lom.setValue(-1, "");
+ } catch (IndexOutOfBoundsException ex) {}
+
+ resetFull();
+ lom = (ListOrderedMap) map;
+ try {
+ lom.setValue(-1, "");
+ } catch (IndexOutOfBoundsException ex) {}
+ try {
+ lom.setValue(lom.size(), "");
+ } catch (IndexOutOfBoundsException ex) {}
+
+ for (int i = 0; i < lom.size(); i++) {
+ Object value = lom.getValue(i);
+ Object input = new Integer(i);
+ assertEquals(value, lom.setValue(i, input));
+ assertEquals(input, lom.getValue(i));
+ }
+ }
+
public void testRemoveByIndex() {
resetEmpty();
ListOrderedMap lom = (ListOrderedMap) map;
@@ -154,25 +181,60 @@
assertEquals(false, lom.containsKey(key));
}
}
-
- public BulkTest bulkTestListView() {
- return new TestListView();
+
+ //-----------------------------------------------------------------------
+ public void testValueList_getByIndex() {
+ resetFull();
+ ListOrderedMap lom = (ListOrderedMap) map;
+ for (int i = 0; i < lom.size(); i++) {
+ Object expected = lom.getValue(i);
+ assertEquals(expected, lom.valueList().get(i));
+ }
}
-
- public class TestListView extends AbstractTestList {
-
- TestListView() {
- super("TestListView");
+
+ public void testValueList_setByIndex() {
+ resetFull();
+ ListOrderedMap lom = (ListOrderedMap) map;
+ for (int i = 0; i < lom.size(); i++) {
+ Object input = new Integer(i);
+ Object expected = lom.getValue(i);
+ assertEquals(expected, lom.valueList().set(i, input));
+ assertEquals(input, lom.getValue(i));
+ assertEquals(input, lom.valueList().get(i));
+ }
+ }
+
+ public void testValueList_removeByIndex() {
+ resetFull();
+ ListOrderedMap lom = (ListOrderedMap) map;
+ while (lom.size() > 1) {
+ Object expected = lom.getValue(1);
+ assertEquals(expected, lom.valueList().remove(1));
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ public BulkTest bulkTestKeyListView() {
+ return new TestKeyListView();
+ }
+
+ public BulkTest bulkTestValueListView() {
+ return new TestValueListView();
+ }
+
+ //-----------------------------------------------------------------------
+ public class TestKeyListView extends AbstractTestList {
+ TestKeyListView() {
+ super("TestKeyListView");
}
public List makeEmptyList() {
- return ((ListOrderedMap) TestListOrderedMap.this.makeEmptyMap()).asList();
+ return ((ListOrderedMap) TestListOrderedMap.this.makeEmptyMap()).keyList();
}
-
public List makeFullList() {
- return ((ListOrderedMap) TestListOrderedMap.this.makeFullMap()).asList();
+ return ((ListOrderedMap) TestListOrderedMap.this.makeFullMap()).keyList();
}
-
+
public Object[] getFullElements() {
return TestListOrderedMap.this.getSampleKeys();
}
@@ -193,6 +255,40 @@
}
}
+ //-----------------------------------------------------------------------
+ public class TestValueListView extends AbstractTestList {
+ TestValueListView() {
+ super("TestValueListView");
+ }
+
+ public List makeEmptyList() {
+ return ((ListOrderedMap) TestListOrderedMap.this.makeEmptyMap()).valueList();
+ }
+ public List makeFullList() {
+ return ((ListOrderedMap) TestListOrderedMap.this.makeFullMap()).valueList();
+ }
+
+ public Object[] getFullElements() {
+ return TestListOrderedMap.this.getSampleValues();
+ }
+ public boolean isAddSupported() {
+ return false;
+ }
+ public boolean isRemoveSupported() {
+ return true;
+ }
+ public boolean isSetSupported() {
+ return true;
+ }
+ public boolean isNullSupported() {
+ return TestListOrderedMap.this.isAllowNullKey();
+ }
+ public boolean isTestSerialization() {
+ return false;
+ }
+ }
+
+ //-----------------------------------------------------------------------
public String getCompatibilityVersion() {
return "3.1";
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org