« Return to Thread: Problem with a "find ."

Problem with a "find ."

by Endre Stølsvik-8 :: Rate this Message:

Reply to Author | View in Thread

I can't seem to get this to work as I hope for.

The following test method "relationshipAfter()" fails (while
relationshipBefore() works), and I would both want it not to, and also
expected it not to!

Btw, is there a better way to accomplish what I'm trying to do?

--- cut ---
package com.example;

import java.util.Collections;

import org.junit.Assert;
import org.junit.Test;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.CollectionList;
import ca.odell.glazedlists.CompositeList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.CollectionList.Model;
import ca.odell.glazedlists.event.ListEventAssembler;
import ca.odell.glazedlists.event.ListEventPublisher;
import ca.odell.glazedlists.util.concurrent.LockFactory;
import ca.odell.glazedlists.util.concurrent.ReadWriteLock;

/**
 * A GlazedLists version of "find ."
 *
 * @author <a href="http://stolsvik.com/">Endre Stølsvik</a>
 */
public class ZTestRecursedEventLists {

    @Test
    public void relationshipFirst() {
        test(true);
    }

    @Test
    public void relationshipAfter() {
        test(false);
    }

    public void test(boolean relationshipFirst) {
        Directory people = new Directory();
        Directory marianne = new Directory();

        if (relationshipFirst) {
            people._children.add(marianne);
        }

        EventList<String> l_people = people.findAll();

        if (!relationshipFirst) {
            people._children.add(marianne);
        }

        marianne._files.add("Marianne");

        Assert.assertEquals(Collections.singletonList("Marianne"), l_people);
    }

    static class Directory {
        private static final ListEventPublisher _publisher =
ListEventAssembler.createListEventPublisher();
        private static final ReadWriteLock _readWriteLock =
LockFactory.DEFAULT.createReadWriteLock();

        EventList<Directory> _children = new
BasicEventList<Directory>(_publisher, _readWriteLock);
        EventList<String> _files = new
BasicEventList<String>(_publisher, _readWriteLock);

        EventList<String> findAll() {
            // :: Get Descendants, by recursion using a CollectionList
with obvious recursion model
            Model<Directory, String> model = new Model<Directory, String>() {
                @Override
                public EventList<String> getChildren(Directory parent) {
                    // NOTE: The CollectionList.dispose() disposes the
member lists.
                    return parent.findAll();
                }
            };
            // NOTE: The CollectionList.dispose() disposes the source
list, just as we needed!
            CollectionList<Directory, String> allDescendantFiles = new
CollectionList<Directory, String>(_children,
                    model);

            // Add in my own files, using a CompositeList between the
descendants (above) and my files
            // NOTE: The CompositeList is-a CollectionList, and hence
also dispose the source lists upon dispose.
            CompositeList<String> allMyAndDescendantFiles = new
CompositeList<String>(_publisher, _readWriteLock);
            allMyAndDescendantFiles.addMemberList(allDescendantFiles);
            allMyAndDescendantFiles.addMemberList(_files);

            return allMyAndDescendantFiles;
        }
    }
}
--- /cut ---

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: Problem with a "find ."