Sunday 27 May 2012

Using "Assert" in java

_________________________________________________


An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

Each assertion contains a Boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. (oracle docs)

Use:



 assert Expression1 ;

where Expression1 is a Boolean expression. When the system runs the assertion, like

assert(Expression1);

 it evaluates Expression1 and if 'Expression1' is false, it will throws an error 'AssertionError' with no detail message.

If you want to print some message if Assertion Error occuers, use the following format.

The second form of the assertion statement is:

    assert Expression1 : Expression2 ;

where:

    Expression1 is a Boolean expression.
    Expression2 is an expression that has a value. (It may simply be a string used as detail message).

Example:



int number = 9;

assert ( number != 10) :  "Number is not equal to 10";

// it will be throwing AssertionError with the message "Number is not equal to 10".

Tuesday 22 May 2012

JQuery : Extracting URL parameters using regular expression


____________________________________________________________

A normal URL contains a number of parameters.


for example: http://books.google.com.pk/bkshp?hl=en&tab=wp


this link contains a parameter ( &tab=wp)


Extract all parameters from a URL, the following code may help you.




$.urlParam = function(name){
   var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
   if(results != null)
   return results[1] || 0;


   
}
var bookTab = $.urlParam(' tab ');


where  bookTab  is javascript variable which store the value of url parameter named "tab".