DuckDuckGo

While Google is obviously the only search engine for many people, it’s been lately spoiled by black SEO practice which causes a lot of spams in its search result.

Here comes a new search engine, which promises to filter spam websites and promises not to track its visitors. While it’s certainly not close enough to any major search engines out there, it has great promise and it is for us to see the future…

Removing vertical scrollbar from SyntaxHighlighter 3

An annoying bug in the new version of SyntaxHighlighter 3 shows vertical scrollbar on every code snippet.

To fix this, SSH to your hosting and change the code under syntaxhighlighter/syntaxhighlighter3/styles/shCore.css and change:

.syntaxhighlighter {
  ...
  overflow: auto !important;
  ...
}

to:

.syntaxhighlighter {
  ...
  overflow: hidden !important;
  ...
}

Proof: Did you see vertical scrollbar on my snippet?

Eclipse Tips: Open Implementation

Eclipse Helios has another subtle enhancement to the JDT. It is called as Open Implementation. So what’s that? Before we play around with the feature, let’s create some classes.

interface IProcessor {
	void process();
}
public class Processor1 implements IProcessor {

	@Override
	public void process() {
		// TODO Auto-generated method stub
	}

}
public class Processor2 implements IProcessor {

	@Override
	public void process() {
	}

}
public class Main {

	public static void main(String[] args) {
		new Processor1().process();
	}

	public static void a(IProcessor p) {
		p.process();
	}
}

Before Open Implementation is implemented, we have only Open Declaration with F3 as default keybinding. Try this on Eclipse before Helios. Put your text cursor on method process on statement p.process(); in class Main. Click F3. You’ll be brought to the process method in IProcessor.

But what we need often is not the declaration, we need the implementation of the method. Before Helios, what I do is to go the declaration, click Ctrl+T and pick the implementation I want.

Helios shortens this. Put your text cursor again on the method and click menu Navigate → Open Implementation. Now if you have more than one implementation of the method, you will get choice to pick which implementation to open.

By defining a keybinding on Preferences → General → Keys you can even use the feature easier, but before you do that, see if this shortcut is fast enough for you.

Click Ctrl and hold it. Now move your mouse over the same method. Tadam… you will get choice.

If you pick Open Implementation you’ll get the same choice as before.

So that’s it. Hopefully this tips helps you a lot.

Java Tips: Process Object Based On Its Type Without if-then-else Solution

I want to share my answer for this question on StackOverflow.

Say you want to process several objects with different types. Each type must be processed differently but some concerns are:

  1. You don’t want if-then-else solution which is obviously not great for long-term
  2. Configuration is also bad for the same reason

So how is the solution? This is the solution that is using library from Reflections.

public class A {

}
public class B {

}
import java.lang.reflect.ParameterizedType;

public abstract class Processor<T> {

	private final Class<T> processedClass;

	public Processor() {
		ParameterizedType parameterizedType =
			(ParameterizedType) getClass().getGenericSuperclass();
		processedClass =
			(Class<T>) parameterizedType.getActualTypeArguments()[0];
	}

	public Class<T> getProcessedClass() {
		return processedClass;
	}

	protected abstract void process(T message);

}
public class ProcessorA extends Processor<A> {

	@Override
	protected void process(A message) {
		System.out.println("Processing object A");
	}

}
public class ProcessorB extends Processor<B> {

	@Override
	protected void process(B message) {
		System.out.println("Processing object B");
	}

}
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.reflections.Reflections;

public class Adapter {

	private Map<Class<?>, Processor<Class<?>>> mapping
		= new HashMap<Class<?>, Processor<Class<?>>>();

	public Adapter() throws Exception {
		Reflections r = new Reflections("");
		Set<Class<? extends Processor>> subTypesOf =
			r.getSubTypesOf(Processor.class);

		for (Iterator iterator = subTypesOf.iterator();
                     iterator.hasNext();) {
			Class<? extends Processor> c =
				(Class<? extends Processor>) iterator.next();
			Constructor<? extends Processor> constructor =
				c.getConstructor();
			Processor p = constructor.newInstance();
			mapping.put(p.getProcessedClass(), p);
		}
	}

	public <T> Processor<T> getProcessor(
			Class<? extends T> c) {
		return (Processor<T>) mapping.get(c);
	}
}
public class Main {

	public static void main(String[] args)
			throws Exception {
		Adapter adapter = new Adapter();

		A a = new A();

		adapter.getProcessor(a.getClass()).process(a);

		B b = new B();

		adapter.getProcessor(b.getClass()).process(b);
	}

}

The console after running main method:

14:01:37.640 [main] INFO  org.reflections.Reflections - Reflections took 375 ms to scan 4 urls, producing 222 keys and 919 values
Processing object A
Processing object B

It’s kind of magic, isn’t it?

technology and stuffs