X

Geertjan's Blog

  • July 19, 2010

How Evil is "instanceof"? (Part 2)

Geertjan Wielenga
Product Manager
Josch Rittner provided a clean solution for the problem I posed in How Evil is "instanceof"?

The result of the code that follows, which completely replaces the LibraryNode class in the previous blog entry, is as follows:

And here's the code received from Josch, which is really quite pretty:

public class LibraryNode extends AbstractNode {
public LibraryNode(Library library) {
super(Children.create(new LibraryFactory(library), true));
setDisplayName(library.getName());
}
private static class LibraryFactory extends ChildFactory<String> {
private final Map<String, Node> categories = new HashMap<String, Node>();
private LibraryFactory(Library library) {
categories.put("Books", new BooksNode(library));
categories.put("Borrowers", new BorrowersNode(library));
}
@Override
protected boolean createKeys(List<String> list) {
list.addAll(categories.keySet());
return true;
}
@Override
protected Node createNodeForKey(String key) {
return categories.get(key);
}
}
}
class BooksNode extends AbstractNode {
public BooksNode(Library library) {
super(Children.create(new BooksFactory(library), true));
setDisplayName("Books");
}
private static class BooksFactory extends ChildFactory<Book> {
private final Library library;
private BooksFactory(Library library) {
this.library = library;
}
@Override
protected boolean createKeys(List<Book> list) {
EList<Book> books = library.getBooks();
for (Book book : books) {
list.add(book);
}
return true;
}
@Override
protected Node createNodeForKey(Book key) {
BeanNode node = null;
try {
node = new BeanNode(key);
node.setDisplayName(key.getTitle());
node.setIconBaseWithExtension("org/library/viewer/book.png");
} catch (IntrospectionException ex) {
Exceptions.printStackTrace(ex);
}
return node;
}
}
}
class BorrowersNode extends AbstractNode {
public BorrowersNode(Library library) {
super(Children.create(new BorrowersFactory(library), true));
setDisplayName("Borrowers");
}
private static class BorrowersFactory extends ChildFactory<Borrower> {
private final Library library;
private BorrowersFactory(Library library) {
this.library = library;
}
@Override
protected boolean createKeys(List<Borrower> list) {
EList<Borrower> borrowers = library.getBorrowers();
for (Borrower borrower : borrowers) {
list.add(borrower);
}
return true;
}
@Override
protected Node createNodeForKey(Borrower key) {
BeanNode node = null;
try {
node = new BeanNode(key);
node.setDisplayName(key.getLastName());
node.setIconBaseWithExtension("org/library/viewer/borrower.png");
} catch (IntrospectionException ex) {
Exceptions.printStackTrace(ex);
}
return node;
}
}
}

Thanks Josch!

Join the discussion

Comments ( 4 )
  • Aljoscha Rittner Monday, July 19, 2010

    Hi!

    I wasn't aware about the Collection-Type of EList. So I can simplyfy the code:

    For the books

    @Override

    protected boolean createKeys(List<Book> list) {

    list.addAll (library.getBooks());

    return true;

    }

    and for the borrowers

    @Override

    protected boolean createKeys(List<Borrower> list) {

    list.addAll (library.getBorrowers());

    return true;

    }

    br, josh.


  • silhanek Sunday, July 25, 2010

    I have not read it yet (I have just returned from holiday). But I heard or read somewhere (Yarda Tulach, I think) the opinion: If you use the "instanceof" operator something is wrong in the analysis. What do you say? Milos


  • Eduardo Costa Monday, July 26, 2010

    Assuming your customer didn't like a "books" subnode, I guess you could solve this using BeanInfo.


  • zefi Saturday, August 7, 2010

    Is it ok to do this?

    =============================

    private static class LibraryFactory extends ChildFactory<Node> {

    private final List<Node> nodes = new ArrayList<Node>();

    private LibraryFactory(Library library) {

    nodes.add(new BooksNode(library));

    nodes.add(new BorrowersNode(library));

    }

    @Override

    protected boolean createKeys(List<Node> list) {

    list.addAll(nodes);

    return true;

    }

    @Override

    protected Node createNodeForKey(Node key) {

    return key;

    }

    }


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha