Jan 14

No, I haven’t put X2J aside. I can tell you that in just a few minutes I will open up IDEA and the X2J project and continue writing some code into it. Hopefully a release will be out soon for all of you to download (Even more hopefully you will actually download it..)

That said, a new blogging project has stumbled into my path. A friend of mine wishes to learn Java but feels sick with the non-interactiveness of Tutorials. He suggested a blog-structured page where an apprentice will be asking questions an instructor, or an instructor giving tasks to the apprentice.

The resulting code will be inspected the remarks, if appropriate, will be posted in the blog. Hopefully, as time advances, both my friend and myself (playing the instructor) will both learn something from it all and help others having the same problem.

I find it quite embarassing to take an instructor’s role, and that leaves me puzzled. In less than 10 months I’m going start the new consultant job. For the last couple of years, at my current job, I have been advising many on code and design. But somehow, taking it to a world-wide web page makes me feel the embarassment of being inspected myself.

Well, we’ll have to wait and see. In the meantime, If anyone wants to chip in by offering tools or design for the new blog, or just a few words of support, mail me at avah [at] crazyredpanda [dot] com or comment here.

Thanks!

Share
Jan 01

Lately I’ve realised that the Analyser code in X2J is messy, and wanted to treat it to a better design. For those numerous people out there who do not know the X2J code (yet!), I will offer some explanation:

The Analyser’s task in X2J is to take a Plain Old Java Object (POJO) and analyse it into a ClassInfo instance. This instance represents an xs:complexType in XSD terms, and contains information regarding the type.

This information includes two collections: One for ElementInfo instances, and one for AttributeInfo instances (representing the xs:element and xs:attribute respectively).

These XXX-Info instances have only getter methods for their properties; They are much like the Class instances Java provides, in a way.

The problem with Analyser is that all the XXX-Info creation logic is located inside it; Whenever new properties are added to ClassInfo it would require changing Analyser itself.

An idea comes to mind and it is called Visitors. Visitors allow separation between the object’s inspection and the object’s creation. The problem is, once an XXX-Info is created, it can’t be changed (getters only, remember?)

My proposition, and what I use in X2J, is something I call a “Mould”. Internally, it’s nothing more than a Properties instance.

What makes it special is that it is accompanied by a set of Visitors which change the mould information as they inspect the analysed class, and a Factory class which takes a mould and creates an XXX-Info instance from it.

What more is that the Factory contains a “default mould”, so that if visitors did not change certain values they will have a default value set in a pretty convinient way, and outside the Factory‘s logic.

Any remarks? Did I miss anything? I’d love to hear responses!

Share
Dec 21

We all know what a Thread is and what Static means, and how they collide. For a quick example: I have an event listener method where I want it to have a stopping condition for a suspected recursion. What I want to do is use a Collection of the items I’ve already received, and check for each call of the listener method if the calling item is already inside the Collection.

I can’t add a Collection to the method signature because I can’t change it (it came from a listener interface), so using a static Collection in my implemented handler comes to mind. However, what if the event was called from different threads at the same time?

This is, like most of my posts, not a new thing. It’s just a nice tool I decided to share its simple code with; Enjoy it or diss it. Here goes:








public class ThreadStaticCollection<T> implements Collection<T>{

  private static Map<Thread, Collection<T>> threadMap = 

    new TreeMap<Thread, Collection<T>>();

  private synchronized Collection<T> get() {

    Collection<T> res = threadMap.get(Thread.currentThread());

    

    if (res == null) { 

      res = new LinkedList<T>();

      threadsMap.put(Thread.currentThread(), res);

    }

    

    return res;

  }

  

  private synchronized void purge() {

    if (get().isEmpty()) {

      thradMap.remove(get());

    }  

  }

  public boolean add(Object item) {

    return get().add(item);

  }

  // Goes on..

  public boolean remove(Object item) {

    boolean b = get().remove(item);

    purge();

    return b;

  }

  // Goes on..

}



Java2html

The get() method will fetch or create the Collection currently available for the current Thread. The Collection interface is implemented by delegating all tasks to what is returned from get().

If items are removed from the Collection, a call to purge() should occur so that the Collection instance could be removed from the static Map. Also notice that both the purge and the get are synchronized for obvious reasons.

Share