Saturday, May 11, 2013

Turn off unlimited trailing whitespace in IntelliJ IDEA

By default, IntelliJ IDEA allows you to click any where on the editor and type. Some users find it annoying. If you are new to IntelliJ, you may find it a bit difficult to change the setting. This is available in the Virtual Space section of Editor Settings in IDEA preferences.

Turn it of by going to File menu -> Settings -> Editor -> Virtual Space and uncheck 'Allow placement of caret after end of line'.

Peace of mind.

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() {
       count++;
     }

  }

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.


   package com.deepumohan.tech.methodinvocationcount;

   public class MyClass {

     static int count = 0;

     public void myMethod() {
       synchronized(this) {
         count++;
       }
     }
  }

Now this works perfect in a multi-threaded environment.

Java provides a better option to use thread-safe increments through Atomic classes available in java.util.concurrent.atomic packag.

   
   package com.deepumohan.tech.methodinvocationcount;

   import java.util.concurrent.atomic.AtomicInteger;

   public class MyClass {

     AtomicInteger atomicInteger = new AtomicInteger(0);

     public int getMethodInvocationCount() {
         return atomicInteger.incrementAndGet();
     }

  }

Here is a good read about AtomicInteger vs static int: Java - using AtomicInteger vs Static int

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.

package com.deepumohan.tech.chartostring;

public class CharToString {

   public static void main(String[] args) {
     char x = 'x';
     System.out.println(concatBlankString(x));
     System.out.println(stringValueOf(x));
     System.out.println(characterToString(x));
     System.out.println(characterObjectToString(x));
     System.out.println(charArray(x));
   }
 
   // append a blank string
   public static String concatBlankString(char x) {
      return x + "";
   }

   // use String.valueOf(char) static function
   public static String stringValueOf(char x) {
      return String.valueOf(x);
   }

   // use Character.toString(char) static function
   public static String characterToString(char x) {
      return Character.toString(x);
   }

   // create new Character object from the given char and
   // then use object's toString() method
   public static String characterObjectToString(char x) {
      return new Character(x).toString();
   } 

   // create new char[] array from the char and pass it to
   // String constructor
   public static String charArray(char x) {  
      return new String(new char[]{x});
   }

/*
   // Compile time error
   // No suitable constructor found
   public static String noConstructor(char x) { 
      return new String(x);
   }

   // Compile time error
   // Inconvertible types
   public static String inconvertibleTypes(char x) {
      return (String) x;
   }
*/
}

I wonder why Java doesn't include String constructor that accept a single char as argument.

Also see

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>();
    temp.add(one);
    temp.add(two);

    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.

Usage:


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...

Read on Kindle!