[JIRA] (GLAZEDLISTS-582) CompositeList writable: why not?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[JIRA] (GLAZEDLISTS-582) CompositeList writable: why not?

JIRA jira-no-reply@java.net
mauromol created GLAZEDLISTS-582:

             Summary: CompositeList writable: why not?
                 Key: GLAZEDLISTS-582
                 URL: https://java.net/jira/browse/GLAZEDLISTS-582
             Project: glazedlists
          Issue Type: Improvement
          Components: core
    Affects Versions: 1.9.1
            Reporter: mauromol
            Assignee: jessewilson

{{CompositeList}} is currently not writable.
I think it should be useful if it were writable (at least as an opt-in).

I wrote my {{CompositeList}} extension that returns {{true}} from {{isWritable()}} and implements {{add(int, E)}} in this way (it could probably be better optimized):

        public void add(final int index, final E value) {
                if (!isWritable())
                        throw new IllegalStateException(
                                        "Non-writable List cannot be modified");
                final int size = size();
                if (index < 0 || index > size)
                        throw new IndexOutOfBoundsException("Cannot add at " + index
                                        + " on list of size " + size);
                if (index >= size)
                        // add in the last position of the last list
                        source.get(size - 1).add(value);
                else {
                        // add to the appropriate list
                        int offset = 0;
                        for (final EventList<E> list : source) {
                                final int currentListSize = list.size();
                                if (index < offset + currentListSize)
                                        // the position hits this list
                                        list.add(index - offset, value);
                                        // go to next list
                                        offset += currentListSize;

I didn't write thorough tests, but made some with the simple cases I needed to handle in my project. I hope this can be a base for discussion.

This message was sent by Atlassian JIRA