Wednesday, March 27, 2013

Java :: Count the number of times a method is called

How to find how many times a method is used?

A general answer to this question would be something like this:

   public class MyClass {

     static int count = 0;

     public void myMethod() {


Using static int would seem a perfect solution. Static will share the instance variable count among all instances of MyClass.

However, problem occurs in a multi threaded environment, as the count incrementation is not synchronised.

Tuesday, March 26, 2013

Java :: How to convert primitive char to String in Java

Char is 16 bit unsigned data type in Java used to store characters and String is an immutable array of char. In Java you cannot cast a primitive char element to String.

Below I have given five methods to convert a char to String. Also I have included common mistakes that gives compile time errors.

Thursday, March 21, 2013

Java :: Check whether two Strings are equal without using the equality(==) operator or the equals() method.

You can exploit the uniqueness property of Set Collection to accomplish this.

Observe the following code:

 * Returns true if the given strings are equal. else returns false.
public boolean isEquals(String one, String two) {
    Set<String> temp = new HashSet<String>();

    return (temp.size() == 1);

The size of set will be greater than 1 only if the given strings are different. Ofcourse the add method will internally use equals() and hashCode() methods.

Thursday, March 7, 2013

Spring Roo :: Adjust text box size/width

Spring Roo uses Dojo's dijit.form.ValidationTextbox widget by default. This means only way to adjust textbox size is to adjust the width of widget. And this has to be done with widget attributes using Spring.addDecoration(); available in spring-js.

        Spring.addDecoration(new Spring.ElementDecoration({
            elementId : '_${sec_field}_id',
            widgetType : 'dijit.form.ValidationTextBox',
            widgetAttrs : {
                  promptMessage: '${sec_field_validation}',
                  invalidMessage: '${sec_field_invalid}',
                  required : ${required},
                  style: 'width: ${width}',
                  ${sec_validation_regex} missingMessage : '${sec_field_required}'

Here I have used a new attribute width, which is added to input.tagx.


Spring Roo input.tagx with adjustable width attribute

You may use the above gist as is or with modification

Related Posts Plugin for WordPress, Blogger...