Pages

Tuesday, June 14, 2011

Three IoC frameworks with JSR-330 annotations

So this post - is very simple illustration of how to make a dependency-injected application with one of three options:
All of them have slightly different goals, but in some ways all of them can handle beans annotated with the annotations from the JSR-330 (Dependency Injection for Java) standardization effort.

Thursday, June 2, 2011

ContainsAny method from apache commons

There was and there is now such method in Apache Commons Collections to define is there any single intersection between two collections. In the version 2.* it was:


  public static <E> boolean containsAny1(Collection<E> collection, Collection<E> elements) {
    for (E e : elements) {
      if (collection.contains(e)) {
        return true;
      }
    }
    return false;
  }
I have added "1" to the name to distinguish.

And in version 3.* it was optimized and now looks like this:

  public static <E> boolean containsAny2(final Collection<E> coll1, final Collection<E> coll2) {
    if (coll1.size() < coll2.size()) {
      for (Iterator<E> it = coll1.iterator(); it.hasNext();) {
        if (coll2.contains(it.next())) {
          return true;
        }
      }
    } else {
      for (Iterator<E> it = coll2.iterator(); it.hasNext();) {
        if (coll1.contains(it.next())) {
          return true;
        }
      }
    }
    return false;
  }
Here is a benchmark, of whether this optimization was worth doing (omitting methods themselves):