开发者

Java object graph visitor library

Do you know a good java object graph visitor library?

I want to visit an object and its sub components and perform some actions when some conditions are matched.

Example usage:

  • on a huge domain object graph, reset each id to null
  • on a huge domain object graph, replace each Set with a TreeSet instance containing the same elements.

I want a library, not custom code because traversing an Objec开发者_开发知识库t graph can be tricky. You have to handle collections, arrays, proxies, and so on... I have think about reuse part of XStream to achieve this, but it doesn't look so easy: Xstream visitor is more oriented on object transformation than object self modification.


I've been looking for the same thing, and found this.

http://code.google.com/p/behaim/


As it happens, I have done such a thing. Not really a library, but it could grow into one easily.

But I stumbled upon this since I was looking for something better. I can't give it out, and it definetely isn't yet in a state where I would do this, but maybe such a thing should come to live as OS?

What I have lets me traverse and modify an object graph type-safe, instance-by-instance, optionally duplicating it such that the original remains untouched. Java BTW. What also works a bit is grasping relationships in the graph (the edges, if you want).

What I could envision is a clear-cut definition of operations (such as modify, extend, duplicate, collapse, traverse) and respective implemetations. Orthogonal aspects such as identifyig subgraphs would be properly factored out.

Anyone interested in such a project please respond, maybe we can get something started.


Why do you need a library in order to do that?

Given that you specify this is a domain object graph then why not define and implement relevant interfaces to allow your domain objects to be visited by different visitor implementations? One of the implementations could (as you specify) reset each ID to null.

Example

First define the interfaces to be implemented by objects that can be visited or act as visitors.

public interface Visitable {
  void visit(Visitor visitor);
}

public interface Visitor {
  void visitDomainObjectA(DomainObjectA obj);
  void visitDomainObjectB(DomainObjectB obj);
}

Now define two domain object classes, both of which can be visited.

public abstract class DomainObject implements Visitable {
  private Object id;

  public Object getId() { return this.id; }
  public void setId(Object id) { this.id = id; }
}

public class DomainObjectA extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectA(this);
  }
}

public class DomainObjectB extends DomainObject {
  public void visit(Visitor visitor) {
    visitor.visitDomainObjectB(this);
  }
}

Now define a concrete Visitor implementation that does something useful:

public class MyVisitor implements Visitor {
  public void visitDomainObjectA(DomainObjectA doa) {
    doa.setId(null);
  }

  public void visitDomainObjectB(DomainObjectB dob) {
    doa.setId(UUID.randomUUID());
  }
}


How about marshalling your object graph into XML and using some standard XML handling/manipulation library?


It might be worth trying an graph database like Neo4j or TitanDB. It will let you affect visitation by using queries to cross-cut your data set and explore relationships.

Both of these have extensive Java APIs to facilitate data loading and querying.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜