[Neo] Neo indexed collection
Mattias Persson
mattias at neotechnology.com
Wed Sep 3 14:27:57 CEST 2008
There has been a request for a sorted neo collection where you can use
your own Comparator. There has been neo collections in neo-utils for a
while, but none of them sorted.
Now there's a IndexedNeoCollection (https://trac.neo4j.org/ticket/103)
in neo-utils which does just that. Here's an example of usage. Note
that the collection takes <T extends NodeWrapper> and NodeWrapperImpl
is just a standard implementation of that interface. Many other
collections and the Link/LinkImpl classes in neo-utils also uses
NodeWrapper.
public class MyObject extends org.neo4j.util.NodeWrapperImpl
{
// For now you must expose a public Constructor with Node as argument
public MyObject( Node node )
{
super( node );
}
public void setName( String name )
{
getUnderlyingNode().setProperty( "name", name );
}
public String getName()
{
return ( String ) getUnderlyingNode().getProperty( "name" );
}
}
public class MyComparator implements Comparator<MyObject>
{
public int compare( MyObject o1, MyObject o2 )
{
return o1.getName().compareTo( o2.getName() );
}
}
// And now, usage
Node collectionRootNode = neo.createNode();
Collection<MyObject> collection = new IndexedNeoCollection(
neo, collectionRootNode, new MyComparator(), MyObject.class );
Node objectNode = neo.createNode();
MyObject myObject = new MyObject( objectNode );
myObject.setName( "Mattias Persson" );
collection.add( myObject );
for ( MyObject object : collection )
{
System.out.println( object.getName() );
}
collection.remove( myObect );
objectNode.delete();
// The collection will create its own sub-root which will need
// to get deleted before the 'collectionRootNode' is deleted.
( ( IndexedNeoCollection ) collection ).delete();
collectionRootNode.delete();
Hopefully this collection will work to most peoples satisfaction for now, enjoy!
/ Mattias
More information about the User
mailing list