The following code is from: http://www.erik-rasmussen.com/blog/2008/01/18/the-filter-pattern-java-conditional-abstraction-with-iterables/
import java.util.Iterator;
import java.util.NoSuchElementException;
public abstract class Filter<T> {
public abstract boolean passes(T object);
public Iterator<T> filter(Iterator<T> iterator) {
return new FilterIterator(iterator);
}
public Iterable<T> filter(Iterable<T> iterable) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return filter(iterable.iterator());
}
};
}
private class FilterIterator implements Iterator<T> {
private Iterator<T> iterator;
private T next;
private FilterIterator(Iterator<T> iterator) {
this.iterator = iterator;
toNext();
}
public boolean hasNext() {
return next != null;
}
public T next() {
if (next == null)
throw new NoSuchElementException();
T returnValue = next;
toNext();
return returnValue;
}
public void remove() {
throw new UnsupportedOperationException();
}
private void toNext() {
next = null;
while (iterator.hasNext()) {
T item = iterator.next();
if (item != null && passes(item)) {
next = item;
break;
}
}
}
}
}
This blog is supposed to keep track of all the small fixes, good tutorials and cheat-sheets I encounter in my binary life so I won't have to seek them on the web again.
Wednesday, December 1, 2010
The Filter Pattern - Selective Iterators
Tuesday, November 30, 2010
A little insight on Java's 'type erasure' terminology
Monday, November 15, 2010
[Spring Framework] Constructor vs. Setter injection
"There are certain things that most people can agree upon: the fact that the sky
is blue, that Michael Jordan is the greatest player to touch a basketball, and that
Star Trek V should have never happened. And then there are those things that
should never be discussed in polite company, such as politics, religion, and the
eternal “tastes great/less filling” debates.
Likewise, the choice between constructor injection and setter injection stirs up
as much discourse as the arguments surrounding creamy versus crunchy peanut
butter. Both have their merits and their weaknesses. Which should you choose?
Those on the constructor-injection side of the debate will tell you that:
• Constructor injection enforces a strong dependency contract. In short, a
bean cannot be instantiated without being given all of its dependencies. It is
perfectly valid and ready to use upon instantiation. Of course, this assumes
that the bean’s constructor has all of the bean’s dependencies in its param-
eter list.
• Because all of the bean’s dependencies are set through its constructor,
there’s no need for superfluous setter methods. This helps keep the lines of
code at a minimum.
• By only allowing properties to be set through the constructor, you are, effec-
tively, making those properties immutable, preventing accidental change in
the course of application flow.
However, the setter injection-minded will be quick to respond with:
• If a bean has several dependencies, the constructor’s parameter list can be
quite lengthy.
• If there are several ways to construct a valid object, it can be hard to come
up with unique constructors, since constructor signatures vary only by the
number and type of parameters.
• If a constructor takes two or more parameters of the same type, it may be
difficult to determine what each parameter’s purpose is.
• Constructor injection does not lend itself readily to inheritance. A bean’s con-
structor will have to pass parameters to super() in order to set private
properties in the parent object.
Fortunately, Spring doesn’t take sides in this debate and will let you choose the
injection model that suits you best. In fact, you can even mix-and-match construc-
tor and setter injection in the same application... or even in the same bean."
Saturday, October 9, 2010
Simple Apache Lucene tutorial courtesy of "Java Code Geeks"
Thursday, September 16, 2010
Change the GRUB Menu Timeout on Ubuntu
When your Ubuntu system boots, you will see the GRUB menu if you hit the Esc key, or if you’ve enabled the menu to show by default.
The only issue with this is that the default timeout is only 3 seconds.
You may want to increase this amount… or you may even want to decrease
it. Either one is simple.
Open up the /boot/grub/menu.lst file in your favorite text editor. I’m using gedit:
sudo gedit /boot/grub/menu.lst
Now find the section that looks like this:
## timeout sec
# Set a timeout, in SEC seconds, before automatically booting the default entry
# (normally the first entry defined).
timeout 3
The timeout value is in seconds. Save the file, and when you reboot
you will have that many seconds to choose the menu item you want.
show the GRUB menu by default on Ubuntu
When Ubuntu boots, you normally briefly see a screen that says “GRUB loading. please wait… Press Esc to enter the menu…”
If you are hacking around your system and would prefer to always see
the GRUB menu (to enter command-line options, for instance), there’s an
easy fix.
Open up the /boot/grub/menu.lst file in your favorite text editor. I’m using gedit:
sudo gedit /boot/grub/menu.lst
Now find the section that looks like this:
## hiddenmenu
# Hides the menu by default (press ESC to see the menu)
hiddenmenu
Put a # before hiddenmenu to comment that line out:
## hiddenmenu
# Hides the menu by default (press ESC to see the menu)
#hiddenmenu
Save the file, and you should see the menu the next time you reboot.
SOURCE: www.howtogeek.com
Adding windows XP to grub menu after intalling this OS AFTER Ubuntu
As root:
# nano /boot/grub/menu.lst
Add the following lines in wherever you would like the entry to show up:
title MS Windows XP
root (hd0,0) [note below]
*
savedefault
makeactive
chainloader +1
*(hd0,0) means /dev/hda1
*(hd0,1) means /dev/hda2