views:

26

answers:

1

For a project at university I plan to build an annotation based tool to visualize/play around with data structures.

Here's my idea:

Students which want to try out their self-written data structures need to:

  • mark the type of their data structures using some sort of marker annotation e.g.

    @List public class MyList { ... }

    so that I know how to represent the data structure

  • need to provide an iterator so that I can retrieve the elements in the right order

  • need to annotate methods for insertion and removal, e.g.

    @add public boolean insert(E e) { ... }

    so that I can "bind" that method to some button.

Do similar applications exist? I googled a little bit around but didn't find anything like that.

+2  A: 

I do not really understand why do you need to use Annotations for such a task? You will end up with some weird methods that your framework would not be able to parse, or will parse it the wrong way - bringing you to making up a set of rules how to properly write that methods so your framework could understand them.

I think it is much better solution to use interfaces instead. Create your own interface called StudentList or something, with methods like getIterator, getIndex, etc. and make students implement this interface. Then you will be able to load any of that classes and use it based on the interface they implement.

Also, you could just reuse avialable interfaces like List or Collection, however, this could make students write a lot of unused code to implement that interfaces. So I would rather go with writing my own interface.

Example of such interface would be:

interface StudentList<T> {
    public Iterator<T> getIterator();
    public T get(int index);
    public void add(T element);
    public void remove(T element);
}

Students would implement it like this (this example just wraps around ArrayList):

class MyList<T> implements StudentList<T> {
    private ArrayList<T> realList;

    public MyList() {
        realList = new ArrayList<T>();
    }

    public void add(T element) {
        realList.add(element);
    }

    .......
}

And you would test it like this:

void testList(StudentList<String> list) {
   list.add(5);
   list.add(10);
   list.get(20);
}

testList(new MyList<String>());

Everything is clear both for students and for you this way.

Max
The idea with using annotations was: 1) You can annotate older classes without rewriting the class (hopefully) 2) You can annotate only the methods you want (e.g. if you only want to test the add() method). But the idea with the interface seems indeed the best way...
Helper Method