Wednesday, 26 December 2012

Why need to use ‘serialVersionUID’ in Serializable class



Class SerializeTest , Here we serialize - deserialized the class.


Why need to use ‘serialVersionUID’.


If you doesn’t write serialVersionUID in your class if interface Serializable is implemented than Eclipse tool gives warning

The serializable class SerializeTest does not declare a static final  serialVersionUID field of type long

If you not add SerialVersionUID for you class serialization runtime add Id with associate class.

By java doc

 ‘’ Note - It is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected serialVersionUID conflicts during deserialization, causing deserialization to fail. ‘’



Now you understand why to add serialVersionUID while creating Serializable class for persist.


For Test Create a class to Serialize and then read the same class.



public class SerializeTest implements Serializable{

      private static final long serialVersionUID = 1L;
     
      private int test;
     
      public int getTest() {
            return test;
      }
     
      public void setTest(int test) {
            this.test = test;
      }
}



For Writing a Serializable class

SerializeTest t = new SerializeTest();
t.setTest(10);

try {
      OutputStream file = new FileOutputStream("serializeTest.ser");
      OutputStream buffer = new BufferedOutputStream(file);
      ObjectOutput output = new ObjectOutputStream(buffer);
      try {
            output.writeObject(t);
      } finally {
            output.close();
      }
} catch (IOException ex) {
      ex.printStackTrace();
}



For Reading a Serializable class


try {
InputStream file = new FileInputStream("serializeTest.ser");
      InputStream buffer = new BufferedInputStream(file);
      ObjectInput input = new ObjectInputStream(buffer);
      try {
SerializeTest t = (SerializeTest) input.readObject();
            System.out.println("SerializeTest.test : "+ t.getTest());        

      } finally {
            input.close();
      }
} catch (ClassNotFoundException ex) {
      ex.printStackTrace();
} catch (IOException ex) {
      ex.printStackTrace();
}


Expected Result is
SerializeTest.test : 10

Now, If you change the ‘serialVersionUID’ of class SerializeTest another value like
private static final long serialVersionUID = 2L;


and now try to read the same object using above read code.

You get the below result.

java.io.InvalidClassException: SerializeTest; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
     




Wednesday, 19 December 2012

Resolve jQuery Conflicts for diff jQuery Plugins

 
 
jQuery Provide " noConflict " to resolve conflict for different version of jQuery
 
For Example if  you app use ,
 
a plugins that have jquery Version 1.5
 
and another plugins that have jquery Version 1.8
 
Now both you use in same page ,it's create conflicts.
 
you can resolve it by  jQuery Method "noConflict"
 
 
Reference : 
 
1). http://api.jquery.com/jQuery.noConflict/ 

2). http://stackoverflow.com/questions/10978770/how-to-resolve-two-jquery-conflict
 

Thursday, 19 July 2012

html form validation using jquery


Dynamically the whole form can validate by jquery,
Just only add some custom attribute in html tag and call the javascript function,the function validate whole page in one call,you doesn't need to write extra script.
Also the script run for multi level form in html.



* Phone Number  
* Address  
* Password  
* Date  
* Birth Date  
 
* Employee   
* Resume  
* Password  
* Email  
* Sex
Male
Female
  
* Job Type  
* Sepcialization
Java
Php
  

Saturday, 26 May 2012

Index Sorting

Sorting Mechanism for Index

like "1" ,"1.1" , "1.2.1" and more


Sorting logic using Comparator


/**
     * this is only for integer type index
     * {"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2"}
     * kind of sorting
     */



private static Comparator<String> comparatorIndices = new Comparator<String>() {

            
        public int compare(String o1, String o2) {
            String[] c1 = o1.contains(".")?o1.split("\\."):new String[]{o1};
            String[] c2 = o2.contains(".")?o2.split("\\."):new String[]{o2};
          
            int c1l = c1.length;
            int c2l = c2.length;
          
            int length;
          
            if(c1l == c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return 0;
            }
          
            if(c1l < c2l){
                length = c1l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
            }
          
            if(c1l > c2l){
                length = c2l;
                for(int i=0;i<length;i++){
                    if(!c1[i].equalsIgnoreCase(c2[i])){
                        return Integer.valueOf(c1[i]).compareTo(Integer.valueOf(c2[i]));
                    }
                }
                return -1;
              
            }
            return 0;
        }
    };
   

You can also generate Tree using DefaultMutableTreeNod



public static void CreaateTree{

String[] str = new String[]{"1", "1.1", "1.1.1", "1.10", "1.10.1", "1.10.2", "1.11", "1.2", "1.2.1", "1.3", "1.3.1", "1.4", "1.4.1", "1.5", "1.5.1", "1.6", "1.6.1", "1.7", "1.7.1", "1.8", "1.8.1", "1.9", "1.9.1", "1.9.2", "2", "2.1", "2.1.1", "2.10", "2.10.1", "2.10.2", "2.11", "2.12", "2.13", "2.2", "2.2.1", "2.2.1.1", "2.2.1.1.1", "2.2.1.1.1.1", "2.2.1.2", "2.3", "2.3.1", "2.4", "2.4.1", "2.5", "2.5.1", "2.6", "2.6.1", "2.7", "2.7.1", "2.8", "2.8.1", "2.9", "2.9.1", "2.9.2", "3", "3.1", "3.1.1"};
        

Collections.sort(Arrays.asList(str), comparatorIndices);
       
        DefaultTreeModel model;
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("0",true);
        model = new DefaultTreeModel(top);
       
        for(String string:str){
            DefaultMutableTreeNode findTreeNode = findTreeNode(getParentId(string));
            if(null != findTreeNode){
                top = findTreeNode;
                top.add(new DefaultMutableTreeNode(string,true));
            }else{
                top.add(new DefaultMutableTreeNode(string,true));
            }
        }
}



public static String getParentId(String child){
        if(null != child && !"".equalsIgnoreCase(child)){
            if(-1 != child.lastIndexOf(".")){
                return child.substring(0, child.lastIndexOf("."));
            }else{
                return "0";
            }
        }
        return "";
    }
   
    public static DefaultMutableTreeNode findTreeNode(String nodeId) {
        DefaultMutableTreeNode rootNode =
                (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode node;
        String tmp;
        Enumeration nodes = rootNode.depthFirstEnumeration();
        while (nodes.hasMoreElements()) {
            node = (DefaultMutableTreeNode) nodes.nextElement();
            tmp = (String) node.getUserObject();
            if (nodeId.equals(String.valueOf(tmp))) {
                return node;
            }
        }
        return null;
    }





Infinidb _CpNoTf_ problem

infinidb table with a varchar column insert string as a '_CpNoTf_' while using Cpimport. The Problem is occured if inserted string ...