commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 34171] New: - [PATCH]: LazyList: implement set(int index, Object element)
Date Fri, 25 Mar 2005 00:33:03 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=34171>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=34171

           Summary: [PATCH]: LazyList: implement set(int index, Object
                    element)
           Product: Commons
           Version: Nightly Builds
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: plegato@nks.net


This patch enhances LazyList so that it will automatically grow larger when
set(int index, Object element) is called if the underlying List supports it
(set() is an optional method in the List interface.) Previously, LazyList only
grew larger when get(int index) was called.

--- LazyList.java.orig  2005-03-24 18:45:20.000000000 -0500
+++ LazyList.java       2005-03-24 19:04:03.000000000 -0500
@@ -29,6 +29,10 @@
  * object from the factory. Thus this list is unsuitable for storing null
  * objects.
  * <p>
+ * If {@link #set(int)} is called with an index greater than the size of the list,
+ * the list will automatically grow in size to the specified index, with the
gap filled
+ * by null.
+ * <p>
  * For instance:
  *
  * <pre>
@@ -54,6 +58,7 @@
  * @author Stephen Colebourne
  * @author Arron Bates
  * @author Paul Jack
+ * @author Paul Legato
  */
 public class LazyList extends AbstractSerializableListDecorator {

@@ -127,6 +132,35 @@
         }
     }

+    //-----------------------------------------------------------------------
+    /**
+     * Decorate the set method to perform the lazy behaviour.
+     * <p>
+     * If the requested index is greater than the current size, the list will
+     * grow to the new size.
+     * Indexes in-between the old size and the requested size are left with a
+     * null placeholder that is replaced with a factory object when requested.
+     *
+     * @param index  the index to set
+     * @param element the object to set at index
+     * @return Returns the object previously at that index (or null, if there
was nothing at that index)
+     * @throws UnsupportedOperationException if the underlying list doesn't
implement set()
+     * @throws ClassCastException if the underlying list doesn't like the class
ofelement
+     * @throws IllegalArgumentException if the underlying list doesn't like
some aspect of the specified element
+     */
+    public Object set(int index, Object element)
+     {
+        int size = getList().size();
+        if (index >= size) {
+            // we have to grow the list
+            for (int i = size; i <= index; i++) {
+                getList().add(null);
+            }
+        }
+
+       // set object at specified index and return value previously at that
location
+       return getList().set(index, element);
+    }

     public List subList(int fromIndex, int toIndex) {
         List sub = getList().subList(fromIndex, toIndex);

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message