Stephen Downey on March 22nd, 2006
Exadel have just released a JSF editor with AJAX support.
JSF, a Web component framework, is the only standard Java Web framework,” said Richard Monson-Haefel, senior analyst for Burton Group. “AJAX, which makes Web sites more responsive, is enjoying an avalanche of grass roots support because of its portability and seamless integration with HTML. Together, JSF and AJAX offer the benefits of standardization with a rich internet experience; a combination that will be attractive to many organizations
Unfortunately, it is a bit pricey at $799 per annual subscription license. I am going to download the trial at the weekend and have a look. I have been using the exadel Studio for eclipse and found the it very good. (Thanks Colin for the recommendation on this one.) I had been looking for a good JSP visual editor plugin for eclipse for quiet a while and this seems to be one of the better ones.
On a different note, I have decided to change my hosting service to InterAdvantage. I have got a pretty good package and will be transferring over this blog to Wordpress as soon as the domain transfer goes through. I will post all new RSS feeds here before the change over.
Stephen Downey on March 1st, 2006
This Java tip relates to functionality that has been around for quite a while but a lot of people are still not aware of it.
There are many occasions when you may need to order a list of data objects. Luckily the Java Collections Framework provides utilites to allow objects to be easily ordered.
The Comparable interface declares one interface:
public int compareTo(Object o);
The data object that you wish to compare must implement this interface. This method should return either -1, 0, or 1.
When the two objects are compared, the compareTo() method is called, passing the object that you wish to compare it to.
If the object is greater a 1 is returned, -1 will indicated that it is less and 0 will indicate that they are equal. In the ComparablePerson class below we have implemented the compareTo method. We will persume that all getter and setter methods are created.
public class ComparablePerson implements Comparable
{
private int age;
private String firstName = "";
private String secondName = "";
public ComparablePerson(String firstName, String secondName, int age)
{
this.firstName = firstName;
this.secondName = secondName;
this.age = age;
}
public int compareTo(Object o)
{
int result = -1;
if (o instanceof ComparablePerson)
{
ComparablePerson compareObj = (ComparablePerson) o;
// Compare by Age
result = new Integer(this.getAge()).compareTo(new Integer(
compareObj.getAge()));
if (result == 0)
{
result = this.getSecondName().compareTo(
compareObj.getSecondName());
if (result == 0)
{
result = this.getFirstName().compareTo(
compareObj.getFirstName());
}
}
}
return result;
}
}
....
In the compareTo method the we first check to see that the object that we wish to compare a ComparablePerson object. If it is not then we cannot compare the properties.
We first compare based on the age. There is no point writing a method to compare two int values as this is already implemented in the Integer compareTo method.
At this stage if the the objects are still equal we continue to compare based on the secondName property. We use the compareTo method of the String class to perform this check.
Finally if they are still equal we should check the firstName property.
This object can now be compared to another ComparablePerson object.
The Collections API provides methods that will sort or reverse a list of compareable objects. The CompareTest class will create a list of ComparablePerson objects and then sort the list.
public class CompareTest
{
public static void main(String[] args)
{
ComparablePerson john = new ComparablePerson(”John”, “Doe”, 31);
ComparablePerson paul = new ComparablePerson(”Paul”, “Doe”, 31);
ComparablePerson stephen = new ComparablePerson(”Stephen”, “Downey”, 27);
ArrayList peopleList = new ArrayList();
peopleList.add(john);
peopleList.add(paul);
peopleList.add(stephen);
System.out.println(peopleList);
Collections.sort(peopleList);
System.out.println(peopleList);
Collections.reverse(peopleList);
System.out.println(peopleList);
}
}
…
The output is as follows:
[John Doe 31, Paul Doe 31, Stephen Downey 27]
[Stephen Downey 27, John Doe 31, Paul Doe 31]
[Paul Doe 31, John Doe 31, Stephen Downey 27]
Stephen Downey on February 23rd, 2006
Ever since I studied a module in Concurrent programming in my Masters I have been fascinated by thread programming. Growing up many moons ago as a junior programmer I was always lead to believe that threads are a bad thing and can lead to some very serious problems.
This can be true if you are not aware of some of the principles of thread programming but in the right hands they can be a very useful tool in a programmers tool kit.
One argument that keeps raising it’s head is that Synchronization is a big hit on performance. Brian Goetz has discussed this in an article of his entitled Java theory and practice: Urban performance legends. The first urban legend that he discuses, is this very topic. It is interesting to see that some programmers may compromise their programs thread safety for what they perceive to be faster code.
I have been looking on Blogger for a way to categories my blog entries, if anyone knows how to do this with Blogger can you let me know, via the comments.
Thanks
Stephen
Stephen Downey on July 29th, 2005
As I mentioned before, every Java programmer must own Effective Java by Joshua Bloch. Here is one of the tips from the book. It discusses is "Minimizing the scope of local variables" (Item 29). Part of this tip, that might be of interest to developers here is: Iteration of List's In his book, Bloch discusses the use of For loops over While loops when iterating through the contents of a list. This will minimize the scope of variables assuming that the variables are not needed later in the method. For Example,
for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
}
To see why this for loop preferable to the more obvious while loop, consider the following code fragment, which contains two while loops and one bug:
Iterator i = c.iterator(); while (i.hasNext() )
{
doSomething(i.next());
}
Iterator i2 = c2.iterator();
while (i.hasNext() )
{
//BUG! doSomethingElse(i2.next());
}
The second loop contains a cut and paste error: It initiates a new loop variable, i2, but uses the old one, i. This will compile with out error and also run with error. The second loop will always terminate immediately and look like c2 does not contain anything. If the same cut and paste were done with the for loop, the loop variable from the first loop would not be in scope in the second loop and it would cause a compile error:
for (Iterator i = c.iterator(); i.hasNext(); )
{
doSomething(i.next() );
……
}
//Compile-time error- the symbol i cannot be resolved
for (Iterator i2 = c2.iterator(); i.hasNext(); )
{
doSomething(i2.next() );
……
}
Here is another tip for faster access to the loop when using random access List implementations such as ArrayList and Vector: for (int i =0, n = list.size(); i )The use of the second variable is essential to the performance of the idion. Without it, the loop would have to call the size method oncer per iteration. This is very useful if you know that there may be a large amount of elements in the list and will save vital time when iterating through the list. One note that Bloch make on this is: "Using this idiom is acceptable when you are sure the list really does provide random access: otherwise it displays quadratic performance". Hope this helps when working with Lists or Arraylists. Stephen For more details on this tip see Effective Java by Joshua Bloch
Stephen Downey on August 9th, 2004
A colleague of mine gave me this link to Java tips which is pretty good, I also have the book recommended
Effective Java and it is a great source of Java advice. I would really recommend reading it as a source for code refactoring tips.
I applied for the part time masters I am planning on starting this year.
It has the advantage of completing it in a calendar year. Can’t wait to get back into study mode again.