Problem with a "find ."

View: New views
3 Messages — Rating Filter:   Alert me  

Problem with a "find ."

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

Reply to Author | View Threaded | Show Only this Message

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@...


Re: Problem with a "find ."

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

Reply to Author | View Threaded | Show Only this Message

By changing the use of CompositeList to CollectionList with a Model
that just returns the supplied "parent" (exactly as CompositeList
does), I got this stuff to work as I assumed _iff_ I add the two
"member lists" (to use CompositeList nomenclature) to the "composite
source list" _before_ I construct the CollectionList - not if I add
them to this source list after the CollectionList is constructed, as
one is bound to do when using the CompositeList. What is strange is
that in the latter case, the "composite model"'s getChildren() is just
never invoked.

This might have something to do with the related subjects stuff??
Since I still have no idea how that works, I'm out of luck there.

Here's a new test case. Only one of the four method crashes, the
/relationshipAfter_AddToConcatenationsAfterCreation()/ method.

-- cut --
package com.example;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

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

import ca.odell.glazedlists.AbstractEventList;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.CollectionList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.CollectionList.Model;
import ca.odell.glazedlists.event.ListEventAssembler;
import ca.odell.glazedlists.event.ListEventListener;
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_AddToConcatenationsBeforeCreate() {
        test(true, true);
    }

    @Test
    public void relationshipAfter_AddToConcatenationsBeforeCreate() {
        test(false, true);
    }

    @Test
    public void relationshipFirst_AddToConcatenationsAfterCreate() {
        test(true, false);
    }

    @Test
    public void relationshipAfter_AddToConcatenationsAfterCreate() {
        test(false, false);
    }

    public void test(boolean relationshipFirst, boolean
addToConcatenationsBeforeCreate) {
        System.out.println("......");
        Directory people = new Directory(addToConcatenationsBeforeCreate);
        Directory marianne = new Directory(addToConcatenationsBeforeCreate);

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

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

        System.out.println("People._children: [" +
getListEventListeners(people._children) + "].");
        System.out.println("People._files: [" +
getListEventListeners(people._files) + "].");
        System.out.println("Marianne._children: [" +
getListEventListeners(marianne._children) + "].");
        System.out.println("Marianne._files: [" +
getListEventListeners(marianne._files) + "].");

        if (!relationshipFirst) {
            people._children.add(marianne);
            System.out.println("..after");
            System.out.println("People._children: [" +
getListEventListeners(people._children) + "].");
            System.out.println("People._files: [" +
getListEventListeners(people._files) + "].");
            System.out.println("Marianne._children: [" +
getListEventListeners(marianne._children) + "].");
            System.out.println("Marianne._files: [" +
getListEventListeners(marianne._files) + "].");
        }

        marianne._files.add("Marianne");

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

    public static <E> List<ListEventListener<E>>
getListEventListeners(EventList<E> from)
            throws IllegalArgumentException {
        Field updatesField;
        try {
            updatesField = AbstractEventList.class.getDeclaredField("updates");
        }
        catch (NoSuchFieldException e) {
            throw new IllegalArgumentException("The EventList [" +
from + "] does not contain an \"updates\" field.");
        }
        updatesField.setAccessible(true);
        ListEventAssembler<E> updates;
        try {
            updates = (ListEventAssembler<E>) updatesField.get(from);
        }
        catch (IllegalAccessException e) {
            throw new AssertionError(e);
        }
        return updates.getListEventListeners();
    }

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

        private final boolean _addToConcatenationsBeforeCreation;

        public Directory(boolean addBeforeCreatingConcatenationList) {
            _addToConcatenationsBeforeCreation =
addBeforeCreatingConcatenationList;
        }

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

        EventList<String> findAll() {
            // Get descendants' files.
            Model<Directory, String> model = new Model<Directory, String>() {
                @Override
                public EventList<String> getChildren(Directory parent) {
                    EventList<String> ret = parent.findAll();
                    System.out.println("Child-recursing, getting
children from [" + parent + "] - children are [" + ret
                            + "].");
                    return ret;
                }
            };
            CollectionList<Directory, String> allDescendantFiles = new
CollectionList<Directory, String>(_children,
                    model);

            // Concatenate my own files with descendants' files.
            Model<EventList<String>, String> passthrough = new
Model<EventList<String>, String>() {
                @Override
                public EventList<String> getChildren(EventList<String> parent) {
                    System.out.println("Descendants+MyOwn
concatenation - this is "
                            + (parent == _files ? "My own" :
"Descendants'") + " files.");
                    return parent;
                }
            };
            EventList<EventList<String>> concatenations = new
BasicEventList<EventList<String>>(_publisher, _readWriteLock);
            if (_addToConcatenationsBeforeCreation) {
                concatenations.add(allDescendantFiles);
                concatenations.add(_files);
            }
            CollectionList<EventList<String>, String>
allMyAndDescendantFiles = new CollectionList<EventList<String>,
String>(
                    concatenations, passthrough);
            if (!_addToConcatenationsBeforeCreation) {
                concatenations.add(allDescendantFiles);
                concatenations.add(_files);
            }

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


2009/4/24 Endre Stølsvik <java@...>:

> 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@...


Re: Problem with a "find ."

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

Reply to Author | View Threaded | Show Only this Message

Ping?

.. both what I assume is a bug somehow, and possibly if there are better ways to achieve what I'm trying to do?

Endre.

2009/4/25 Endre Stølsvik <java@...>
By changing the use of CompositeList to CollectionList with a Model
that just returns the supplied "parent" (exactly as CompositeList
does), I got this stuff to work as I assumed _iff_ I add the two
"member lists" (to use CompositeList nomenclature) to the "composite
source list" _before_ I construct the CollectionList - not if I add
them to this source list after the CollectionList is constructed, as
one is bound to do when using the CompositeList. What is strange is
that in the latter case, the "composite model"'s getChildren() is just
never invoked.

This might have something to do with the related subjects stuff??
Since I still have no idea how that works, I'm out of luck there.

Here's a new test case. Only one of the four method crashes, the
/relationshipAfter_AddToConcatenationsAfterCreation()/ method.

-- cut --
package com.example;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

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

import ca.odell.glazedlists.AbstractEventList;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.CollectionList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.CollectionList.Model;
import ca.odell.glazedlists.event.ListEventAssembler;
import ca.odell.glazedlists.event.ListEventListener;
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_AddToConcatenationsBeforeCreate() {
       test(true, true);
   }

   @Test
   public void relationshipAfter_AddToConcatenationsBeforeCreate() {
       test(false, true);
   }

   @Test
   public void relationshipFirst_AddToConcatenationsAfterCreate() {
       test(true, false);
   }

   @Test
   public void relationshipAfter_AddToConcatenationsAfterCreate() {
       test(false, false);
   }

   public void test(boolean relationshipFirst, boolean
addToConcatenationsBeforeCreate) {
       System.out.println("......");
       Directory people = new Directory(addToConcatenationsBeforeCreate);
       Directory marianne = new Directory(addToConcatenationsBeforeCreate);

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

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

       System.out.println("People._children: [" +
getListEventListeners(people._children) + "].");
       System.out.println("People._files: [" +
getListEventListeners(people._files) + "].");
       System.out.println("Marianne._children: [" +
getListEventListeners(marianne._children) + "].");
       System.out.println("Marianne._files: [" +
getListEventListeners(marianne._files) + "].");

       if (!relationshipFirst) {
           people._children.add(marianne);
           System.out.println("..after");
           System.out.println("People._children: [" +
getListEventListeners(people._children) + "].");
           System.out.println("People._files: [" +
getListEventListeners(people._files) + "].");
           System.out.println("Marianne._children: [" +
getListEventListeners(marianne._children) + "].");
           System.out.println("Marianne._files: [" +
getListEventListeners(marianne._files) + "].");
       }

       marianne._files.add("Marianne");

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

   public static <E> List<ListEventListener<E>>
getListEventListeners(EventList<E> from)
           throws IllegalArgumentException {
       Field updatesField;
       try {
           updatesField = AbstractEventList.class.getDeclaredField("updates");
       }
       catch (NoSuchFieldException e) {
           throw new IllegalArgumentException("The EventList [" +
from + "] does not contain an \"updates\" field.");
       }
       updatesField.setAccessible(true);
       ListEventAssembler<E> updates;
       try {
           updates = (ListEventAssembler<E>) updatesField.get(from);
       }
       catch (IllegalAccessException e) {
           throw new AssertionError(e);
       }
       return updates.getListEventListeners();
   }

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

       private final boolean _addToConcatenationsBeforeCreation;

       public Directory(boolean addBeforeCreatingConcatenationList) {
           _addToConcatenationsBeforeCreation =
addBeforeCreatingConcatenationList;
       }

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

       EventList<String> findAll() {
           // Get descendants' files.
           Model<Directory, String> model = new Model<Directory, String>() {
               @Override
               public EventList<String> getChildren(Directory parent) {
                   EventList<String> ret = parent.findAll();
                   System.out.println("Child-recursing, getting
children from [" + parent + "] - children are [" + ret
                           + "].");
                   return ret;
               }
           };
           CollectionList<Directory, String> allDescendantFiles = new
CollectionList<Directory, String>(_children,
                   model);

           // Concatenate my own files with descendants' files.
           Model<EventList<String>, String> passthrough = new
Model<EventList<String>, String>() {
               @Override
               public EventList<String> getChildren(EventList<String> parent) {
                   System.out.println("Descendants+MyOwn
concatenation - this is "
                           + (parent == _files ? "My own" :
"Descendants'") + " files.");
                   return parent;
               }
           };
           EventList<EventList<String>> concatenations = new
BasicEventList<EventList<String>>(_publisher, _readWriteLock);
           if (_addToConcatenationsBeforeCreation) {
               concatenations.add(allDescendantFiles);
               concatenations.add(_files);
           }
           CollectionList<EventList<String>, String>
allMyAndDescendantFiles = new CollectionList<EventList<String>,
String>(
                   concatenations, passthrough);
           if (!_addToConcatenationsBeforeCreation) {
               concatenations.add(allDescendantFiles);
               concatenations.add(_files);
           }

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


2009/4/24 Endre Stølsvik <java@...>:
> 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 ---
>