Wednesday, January 7, 2009

Java Important Tips

Hibernate Collection Sorting

Java Jobs

Java Papers

Servlet

Servlet Interview Questions


RoseIndia's Java


Sort Java (for sql query generated / except hibernate relational data) Collections


Full Example to sort Collection

Generic Sorts

If you write a sort for a List, e.g. ArrayList, with proper generics, it will work on collections of any type that supports Comparable or Comparator. To see how to pull it off, have a look at the source for any of my sorts, or Sun’s sort.

However, because of Java’s lack of orthogonality, your List sort won’t work for arrays of such Objects. You need to write very similar code to do that. Even that array version won’t sort an array of primitives such as long, int or byte. You have to write yet another slightly different version of the sort to handle each type of primitive.

Complete Example:

package com.mindprod.example;

import com.mindprod.palette.NamedColor.Alphabetically;
import com.mindprod.palette.NamedColor.ByRGB;

import java.awt.Color;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
* Demonstrate how to sort a Collection in Various orders using Sun's Collections.sort
* Created by IntelliJ IDEA.
*
* @author Roedy Green
*/
public class TestSort
{
// --------------------------- main() method ---------------------------

public static void main( String[] args )
{
// build ArrayList of Animals
ArrayList<Animal> animals = new ArrayList<Animal>( 5 );
animals.add( new Animal( "panther", Color.BLACK, 4 ) );
animals.add( new Animal( "frog", Color.GREEN, 4 ) );
animals.add( new Animal( "crab", Color.RED, 10 ) );
animals.add( new Animal( "elephant", Color.GRAY, 2 ) );
animals.add( new Animal( "spider", Color.BLACK, 8 ) );

out.println( "sort in Natural Order (by legs)" );
Collections.sort( animals );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}

out.println( "sort in Reverse Natural Order (by legs)" );
Collections.sort( animals, Collections.reverseOrder() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}

out.println( "sort alphabetically" );
Collections.sort( animals, new Animal.Alphabetically() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}

out.println( "sort reverse alphabetically" );
Collections.sort( animals, Collections.reverseOrder( new Animal.Alphabetically() ) );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}

out.println( "sort by color" );
Collections.sort( animals, new Animal.ByRGB() );
for ( Animal animal : animals )
{
out.println( animal.toString() );
}
// end main
}
}

/**
* objects we will sort, represent animals, not nested
*/
class Animal implements Comparable<Animal>
{

/**
* what color this animal is.
*/
final Color color;

/**
* name of the animal
*/
final String name;

/**
* how many legs this animal has
*/
final int legs;
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare by legs then by colour (numerically).
*
* @param o other animal to compare with
* @return +ve if this animal is bigger, -ve if smaller, 0 if equal
*/
public int compareTo( Animal o )
{
int diff = legs - o.legs;
if ( diff != 0 )
{
return diff;
}
return ( color.getRGB() & 0xffffff ) - ( o.color.getRGB() & 0xffffff );
}

/**
* what colour is the animal
*
* @return colour
*/
public Color getColor()
{
return color;
}

/**
* how many legs has this animal?
*
* @return number of legs
*/
public int getLegs()
{
return legs;
}

/**
* override usual ToString to give a debugging represenation.
*
* @return name, color, legs as a string.
*/
public String toString()
{
return name + " " + color + " " + legs;
}

// --------------------------- CONSTRUCTORS ---------------------------

/**
* constructor
*
* @param name name of the animal.
* @param color what colonr the animal is
* @param legs howw many legs the animal has
*/
Animal( String name, Color color, int legs )
{
this.name = name;
this.color = color;
this.legs = legs;
}


/**
* Comparator for sorting Animals by alphabetically, case insensitive
*/
static class Alphabetically implements Comparator<Animal>
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare two animal objects
*
* @param o1 first object
* @param o2 second object
* @return +ve if o1 is biggern, -ve if smaller, 0 if equal
*/
public int compare( Animal o1, Animal o2 )
{
return o1.name.compareToIgnoreCase( o2.name );
}
}

/**
* Comparator for sorting Animals by Color then legs
*/
static class ByRGB implements Comparator<Animal>
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* compare two animal objects
*
* @param o1 first object
* @param o2 second object
* @return +ve if o1 is biggern, -ve if smaller, 0 if equal
*/
public int compare( Animal o1, Animal o2 )
{
int diff = ( o1.color.getRGB() & 0xffffff ) - ( o2.color.getRGB() & 0xffffff );
if ( diff != 0 )
{
return diff;
}
return o1.legs - o2.legs;
}
}
}

Tuesday, January 6, 2009

Software Site Links

Dreamweaver Extensions

Css Validator

Cross Browser Compatible Useful Article

Make your site cross browser compatible in 5 steps

Make your site cross browser compatible in 5 steps

Make your site cross browser compatible in 5 steps

Making your site cross browser compatible is not easy, but with this tutorial you can make your site cross browser compatible in 5 simple steps.

Step 1: Validate

Lots of bugs are caused by invalid (X)HTML or CSS. If you have your site validated, it will be a lot easier to make it cross browser compatible. Validate your (X)HTML code with the W3C validator and your CSS file with the W3C CSS validator.

validhtml

Step 2: Check your Website

Now you have to check your website in all browsers. I always check my site in the browsers, I’ve installed on my computer (Opera, Safari, Internet Explorer and Firefox). Do it and make a note of the bugs.

Of course, checking your website in the browsers installed on your computer is not enough. You have to check the website in different browser versions on different operating systems. The solution is Browsershots.org. With this website you can take full-length screen shots of your website in a lot of browsers. Just type in your url, bookmark the page and come back 20 minutes later to view the screen shots.

browsers

Analyse the screen shots and look for bugs. Be sure to write them down.

Step 3: Is it worth my time?

If you notice bugs in very old or uncommon used browsers, ask yourself if it’s worth your time to fix the problems. Look at the browser statistics of w3schools.com, but don’t forget to look to your own stats (I recommend AWstats). If you have one or two visitors every month still browsing with IE5.5, you should really ask yourself if it’s necessary to make the changes.

browserstats

Step 4: Search the cause

Now you know about the bugs, you’ll have to look for the CSS attribute or HTML code that is causing this problem. This is often obvious. A good reference can be found on westciv.com. There is a complete list of css attributes with their support in different browsers. Too bad Firefox isn’t in the tables.

If you still cannot find the cause of the issue, search for topics on webmaster forums or post a topic yourself, with a screen shot and (a part of) your CSS and HTML code. A forum I visit often is Ozzu.

ozzu

Step 5: Fix the issues

Now we’re at the essential part of making your website cross browser compatible: fixing the issues. If you know the code that is causing the trouble, find a way to rewrite the code for the specific browser. There are loads of CSS hacks to do that, but I recommend the use of conditional comments. There is an article on 456 Berea St. explaining why you should use conditional comments instead of CSS hacks.

Conditional Comments.
With conditional comments you can link to separate style sheets for all versions of Internet Explorer. A complete reference can be found on quirksmode. For example: