Thursday, October 3, 2013

IP address validation

Any IP address has to fall into [0-255].[0-255].[0-255].[0-255] format criteria.

We can achieve this by using following 2 ways.


Using split method of String: The method described above can also be written in a simple way as follows


public boolean validateIPAddress( String ipAddress ) {

String[] tokens = ipAddress.split("\\.");
if (tokens.length != 4) {
return false;
}
for (String str : tokens) {
int i = 0;
try {
  i = Integer.parseInt( s );
  } catch (NumberFormatException e) { //if any character data is entered.
   return false;
  }
if ((i < 0) || (i > 255)) {
return false;
}
}
return true;
}


Using regular expression: The following method with return true is the IP Address is valid, else it returns false.


public boolean validateIPAddress( String ipAddress ){

final Pattern ipAdd= Pattern.compile("b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
+ "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)b");
return ipAdd.matcher(ipAddress).matches();
}


If it's is false, we can show error message as "IP Address must be in the in the format of:  [0-255].[0-255].[0-255].[0-255]"


Wednesday, October 2, 2013

Eclipse bundle states

Below diagram explains in which state your eclipse bundle is in.



To understand each state of these, please follow the below link





Tuesday, September 24, 2013

How do I stop the error log view popping up in eclipse

In general will log lot of information and errors into the error during the job execution, if that is a long running job it will keep on posting current execution messages to the error log. While posting the message to the error log view, if the view is not focused job will bring the focus to the view.
Sometimes this would be disgusting, if you are working with other view just next to it, beacause focus will always goes back to the error log view, if any message is posted into it by currently running job. To avoid this kind of issue follow the below approach.

In error log view, you can find the drop down menu (small triangle on the top - right of the view), deselect "Activate on new events".

This will avoid the view popping issue when any error or messages are pushed into error log.



Saturday, September 7, 2013

Three rules for effective exception handling in java

If we are dealing with exceptions basically it has to tell you the following things.

1. What went wrong ?
2. Where did it go wrong ?
3. Why did it go wrong ?

so, to address above points we need follow 3 golden rules of exception handling.

1.  Be specific on exception type  -> It should tell what went wrong, ex: file not found, connection not found

2. Throw early - > ex: if you are passing null file name to FileInputStream it throws null pointer exception, but this is not sufficient to the user, instead check whether it is null or not ,then throw an exception that "File name can't be empty".

3. Catch late- > Catch where you want to show it the user immediately. Don't log and catch at the API level or lower level, instead throw from api or lower level and catch at the UI layer or at the top layer.


https://today.java.net/pub/a/today/2003/12/04/exceptions.html




Friday, September 6, 2013

NLS bind with single quote messages

Ex:
NLS.bind("Deleting projects from Account '{0}'",  dataProvider.getCurrentAccount().getName())

This will not give you the desired output.

Output: Deleting projects from Account '{0}'

As per API, Text appearing within single quotes is treated as a literal. A single quote is escaped by a preceeding single quote.

Modify as below:
NLS.bind("Deleting projects from Account ' '{0}' ' ",  dataProvider.getCurrentAccount().getName())

output: Deleting projects from Account KondalKolipaka