Tuesday, July 27, 2010

Java : singleton with example

A singleton is a class that can be instantiated once, and only once. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM). Often in designing a system, we want to control how an object is used, and prevent others (ourselves included) from making copies of it or creating new instances. For example, a central configuration object that stores setup information should have one and one only instance - a global copy accessible from any part of the application, including any threads that are running.
public class SingletonObject {
   //marked as private so that class can't be directly instantiated
   private SingletonObject() {
      // no code req'd
   }

   //add synchronized keyword to prevent thread making a new copy
   public static synchronized SingletonObject getSingletonObject() {
      if (ref == null) {
         // it's ok, we can call this constructor
         ref = new SingletonObject();
      }
      return ref;
   }

   //overwrite default clone method so that this class can't be cloned
   public Object clone()
   throws CloneNotSupportedException {
      throw new CloneNotSupportedException();
      // that'll teach 'em
   }

   private static SingletonObject ref;
}

reference : http://www.javacoffeebreak.com/articles/designpatterns/index.html

Monday, July 26, 2010

Java enum switch/if-else statement example

1.using switch example
public class JavaEnumSwitchCaseExample {

   enum Margin
   {
      TOP, RIGHT, BOTTOM, LEFT
   }

   public static void main(String[] args)
   {
      System.out.println(getMarginValue(Margin.TOP));
   }

   public static String getMarginValue(Margin margin)
   {
      switch (margin) {
         case TOP: return "1em";
         case RIGHT: return "12px";
         case BOTTOM: return "1.5em";
         case LEFT: return "6px";
         default: return null;
      }
   }
} //end of class

2. using if-else example
public class JavaEnumIfThenExample {

   public enum Day
   {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
   }

   public static void main(String[] args)
   {
      Day theDay = Day.THURSDAY;
      printDayGreeting(theDay);
   }

   public static void printDayGreeting(Day day)
   {
      if (day == Day.FRIDAY)
         System.out.println("TGIF");
      else
         System.out.println("Some other day");
   }
} //end of class

reference : http://www.devdaily.com/java/using-java-enum-examples-tutorial

java : Enum - examples

public enum Status {
   Open("O"), Closed("C"), Reopened("R")
   final String code;
   Status(String code) {
      this.code = code;
   }
};

you can use the attribute as you would for any other class, eg:
Status[] values = Status.values();
for(Status statue : values) {
   System.out.println(status.code + " - " + status);
}

result:
O - Open
C - Closed
R - Reopened

1.enum String:
Open("O"), Closed("C"), Reopened("R")

2.enum range:
Low(0,100), Medium(101,1000), High(1000, 10000);

3. enum boolean:
Open(true), Closed(false), Reopened(true);

reference :http://java.dzone.com/blogs/mrjohnsmart/2008/05/19/a-short-primer-java-enums-part

JAVA: tips for single thread AND multi thread

1. Use Vector if there are multiple threads and ArrayList if there is only a single thread.
2. Use Hashtable if there are multiple threads and HashMap if there is only a single thread.
3. Use StringBuffer if there are multiple threads and StringBuilder if there is only a single thread.

Wednesday, July 21, 2010

CSS: how do I vertically-center something?

css:
#myoutercontainer { position:relative }
#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }

html:
Hey look! I'm vertically centered! How sweet is this?!

alternative way:
rules : You have only a single line of text that you want to center

css:
#myoutercontainer2 { line-height:4em }

html:
Hey, this is vertically centered. Yay!

Monday, July 5, 2010

CSS : create scrollbar when reach max height - IE and FF

.overflowdiv-dr {
   height: expression( this.scrollHeight > 449 ? "450px" : "auto" ); /* sets max-height for IE */
   max-height: 450px; /* sets max-height value for all standards-compliant browsers */
   overflow:auto;
}

apply to div,table or container etc.