Thursday, November 19, 2009

Language Specific String Sorting

I have created an utility class for sorting strings on the basis of natural language. You can use this utility to sort array of strings, list of strings.



You can download StringSorter Utility

Saturday, November 14, 2009

What does Bytecode Verifier

1. Checks a classfile for validity:
    a. Code only has valid instructions & register use
    b. Code does not overflow/underflow stack
    c. Does not convert data types illegally or forge pointers
    d. Accesses objects as correct type
    e. Method calls use correct number & types of arguments
    f. References to other classes use legal names

2. Goal is to prevent access to underlying machine

Rules For Defining Immutable Objects

Below are some rule for creating Immutable Objects in Java, Immutable Objects are those objects: we can't change the state of the object after construction. Example of Immutable object is String Class.

1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
2. Make all fields final and private.
3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
5. If the instance fields include references to mutable objects, don't allow those objects to be changed:
Don't provide methods that modify the mutable objects.
Don't share references to the mutable objects. Never store references to external,
mutable objects passed to the constructor; if necessary, create copies, and store
references to the copies. Similarly, create copies of your internal mutable objects
when necessary to avoid returning the originals in your methods.


final public class ImmutableRGB {

//Values must be between 0 and 255.
final private int red;
final private int green;
final private int blue;
final private String name;

private void check(int red, int green, int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
}

public ImmutableRGB(int red, int green, int blue, String name) {
check(red, green, blue);
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
}


public int getRGB() {
return ((red << 16) | (green << 8) | blue);
}

public String getName() {
return name;
}

public ImmutableRGB invert() {
return new ImmutableRGB(255 - red, 255 - green, 255 - blue,
"Inverse of " + name);
}
}

Tuesday, November 10, 2009

Get Property names of a Bean


import java.io.Serializable;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;

public class GetBeanAttr implements Serializable {
private Long id;
private String name;
private String latinName;
private double price;

public GetBeanAttr() {
}

public static void main(String[] args) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(GetBeanAttr.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor pd : pds) {
String propertyName = pd.getName();

System.out.println("propertyName = " + propertyName);
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLatinName() {
return latinName;
}

public void setLatinName(String latinName) {
this.latinName = latinName;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}

Notify when Bean property changed

We want to know or to get notified when the bean property is changed. For that we create a Bean having a property named name with getter/setter method.

First we need a PropertyChangeSupport field to the bean, with this object we will fire the property change event. When we need to listen for the change we have to create an implementation of a PropertyChangeListener. In this example we'll just use the MyBean class as the listener.

The PropertyChangeListener has a method called propertyChange and inside this method we'll implementing the code to get the event fired by the PropertyChangeSupport.

Friday, November 6, 2009

Uses of Console class in Java

Java 6 introduce a new class Console in the family of java.io.
This Class is used to access the character-based console device, if any, associated with the current Java virtual machine.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If virtual machine has a console then it is represented by a unique instance of this class, which can be obtained by invoking
System.console().
If JVM does not recognize any console, then this function returns null.

The methods in this class are synchronized. You can read line of text by using the
readLine()
method. You can also read secure data such as Password by using
readPassword()
method. You can also format text by using
format()
. You can get PrintWriter and PrintReader by using
reader(), writer()
method.

Example: Get user name and password from the user.

public class IConsole
{
public static void main(String arg[])
{
Console console = System.console();
if(console!=null)
{
String userName = console.readLine("User Name:");
char [] password = console.readPassword("Password:");
console.printf("User Name is %1$s , Password is %1s",userName, String.valueOf(password));
}
}
}