values can now be accessed as a List using valueList()

additional list-like method, setValue(int,Object)
rfe 37015

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@321321 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-10-15 12:28:08 +00:00
parent 7de8ea1ed8
commit 5abcddb1d5
3 changed files with 200 additions and 22 deletions

View File

@ -71,6 +71,8 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
<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>

View File

@ -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.IOException;
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 @@ public class ListOrderedMap
}
//-----------------------------------------------------------------------
/**
* 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.
@ -304,12 +358,24 @@ public class ListOrderedMap
return insertOrder.indexOf(key);
}
/**
* 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 @@ public class ListOrderedMap
* 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 class ListOrderedMap
}
};
}
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;

View File

@ -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 class TestListOrderedMap extends AbstractTestOrderedMap {
}
}
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 @@ public class TestListOrderedMap extends AbstractTestOrderedMap {
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 TestListOrderedMap extends AbstractTestOrderedMap {
}
}
//-----------------------------------------------------------------------
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";
}