Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Saturday, February 25, 2012

How to create a fixed list in Java using Apache Commons FixedSizeList class


There are many ways to create a fixed list in Java, one of them is by using the FixedSizeList class, found in the Apache org.apache.commons.collections package (http://commons.apache.org/collections/). FixedSizeList does not support the add, remove, clear and retain methods and the set method is allowed as far as it doesn’t change the list size:

package fixedsizelist.exemple;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections.list.FixedSizeList;

public class FixedSizeListExemple {

public static void main(String[] args) {

List fixedList = FixedSizeList.decorate(Arrays.asList(new String[5]));

fixedList.set(0, "some_name@yahoo.com");
fixedList.set(1, "some_other_name@gmail.com");
fixedList.set(2, "777_name@rocketmail.com");
fixedList.set(3, "bond_007@yahoo.com");
fixedList.set(4, "james99@yahoo.com");

System.out.println("\tDisplay list values...\n");
for (String obj : fixedList) {
System.out.println(obj.toString());
}

try {
System.out.println("\n\tTrying to modify our fixed list...\n");

fixedList.add("paul01_mac@yahoo.com");
fixedList.remove(3);
fixedList.clear();

} catch (Exception e) {
System.out.println
(" The add, remove, clear and retain operations are unsupported."
+ "\nThe set method is allowed (as it doesn't change the list size).\n");
}
}
}

And the output is:

Friday, February 24, 2012

How to allow duplicate key in Java Collections


Java Collections allows you to add one or more elements with the same key by using the MultiValueMap class, found in the Apache org.apache.commons.collections package (http://commons.apache.org/collections/):

package multihashmap.example;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.map.MultiValueMap;

public class MultiHashMapExample {

public static void main(String[] args) {

List list;
MultiValueMap map = new MultiValueMap();
map.put("A", 4);
map.put("A", 6);
map.put("B", 7);
map.put("C", 1);
map.put("B", 9);
map.put("A", 5);

Set entrySet = map.entrySet();
Iterator it = entrySet.iterator();
System.out.println(" Object key Object value");
while (it.hasNext()) {
Map.Entry mapEntry = (Map.Entry) it.next();
list = (List) map.get(mapEntry.getKey());
for (int j = 0; j < list.size(); j++) {
System.out.println("\t" + mapEntry.getKey() + "\t " + list.get(j));
}
}
}
}

Since Java Core does’t come with some solutions for supporting multiple keys, using the org.apache.commons.collections seems to be a proper way to deal with multiple keys.


And the output is:

Friday, February 10, 2012

Cropping, rotate and resizing images


This is FYI kind tip. Recently, I have looked for some cool Java libraries for cropping, rotating and resizing images. I was looking for something small, fast, flexible and very easy to use. And I found it at http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/. It main advantages are:

• 100% Java code based on Java2D, no native libraries to install
• Java that works in “headless” environments without special environment variable settings; it just works.
• Because there are no native libraries, you mitigate the risk of native libraries crashing the host VM (e.g. your app server) or thread-contention issues
• Hardware accelerated on supported platforms (uses the optimized Java2D rendering pipeline)
• Fast; not faster than a native-C solution, but still damn fast.
• Simple, simple, simple. A handful of static, 1-word methods you can call. No complex configurations, managers, encoders/decoders or any other nonsense. imgscalr is not a “graphics library”, it is a set of the most commonly used graphic operations, optimized and ready to do your bidding.

Well, per example a resize can be accomplished like this:

BufferedImage bimage = Scalr.resize(bimage, Method.QUALITY, Mode.FIT_EXACT, width,
height);

This is just one case, but as you can see in javadoc – http://www.thebuzzmedia.com/downloads/software/imgscalr/javadoc/index.html, this is exactly what you need for cropping, rotating and resizing images.